1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
import { isISBN } from 'validator';
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';
const NON_SEEDED_BASED_RUN = 5;
describe('commerce', () => {
seededTests(faker, 'commerce', (t) => {
t.itEach(
'department',
'productName',
'productAdjective',
'productMaterial',
'product',
'productDescription'
);
t.describe('price', (t) => {
t.it('noArgs')
.it('with min option', { min: 42 })
.it('with max option', { max: 1337 })
.it('with min and max option', { min: 50, max: 100 })
.it('with float min and float max option', { min: 1, max: 1.1 })
.it('with min and max and decimals option', {
min: 50,
max: 100,
dec: 4,
})
.it('with min and max and decimals and symbol option', {
min: 50,
max: 100,
dec: 4,
symbol: '$',
});
});
t.describe('isbn', (t) => {
t.it('noArgs')
.it('with variant 10', 10)
.it('with variant 13', 13)
.it('with variant 10 and space separators', {
variant: 10,
separator: ' ',
})
.it('with space separators', { separator: ' ' });
});
});
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe(`department()`, () => {
it('should return random value from department array', () => {
const department = faker.commerce.department();
expect(faker.definitions.commerce.department).toContain(department);
});
});
describe(`productName()`, () => {
it('should return random values from product arrays', () => {
const name = faker.commerce.productName();
expect(name.split(' ').length).toBeGreaterThanOrEqual(3);
const parts = name.split(' ');
expect(faker.definitions.commerce.product_name.adjective).toContain(
parts[0]
);
expect(faker.definitions.commerce.product_name.material).toContain(
parts[1]
);
expect(faker.definitions.commerce.product_name.product).toContain(
parts[2]
);
});
});
describe(`price()`, () => {
it('should use the default amounts when not passing arguments', () => {
const price = faker.commerce.price();
expect(price).toBeTruthy();
expect(price).toBeTypeOf('string');
expect(+price).toBeGreaterThan(0);
expect(+price).toBeLessThanOrEqual(1000);
});
it('should use the default decimal location when not passing arguments', () => {
const price = faker.commerce.price();
const decimal = '.';
const expected = price.length - 3;
const actual = price.indexOf(decimal);
expect(
actual,
`The expected location of the decimal is ${expected} but it was ${actual} amount ${price}`
).toBe(expected);
});
it('should not include a currency symbol by default', () => {
const amount = faker.commerce.price();
expect(
amount,
'The expected match should not include a currency symbol'
).toMatch(/^[0-9.]+$/);
});
it('should handle negative amounts, but return 0', () => {
const amount = faker.commerce.price({ min: -200, max: -1 });
expect(amount).toBeTruthy();
expect(amount, 'the amount should equal 0').toBe('0');
});
it('should handle argument dec', () => {
const price = faker.commerce.price({ min: 100, max: 100, dec: 1 });
expect(price).toBeTruthy();
expect(price, 'the price should equal 100.0').toBe('100.0');
});
it('should handle argument dec = 0', () => {
const price = faker.commerce.price({ min: 100, max: 100, dec: 0 });
expect(price).toBeTruthy();
expect(price, 'the price should equal 100').toBe('100');
});
it('should return decimal values between min and max', () => {
const result = faker.helpers.multiple(
() => faker.commerce.price({ min: 1, max: 1.1, dec: 2 }),
{ count: 50 }
);
for (const price of result) {
const parsedPrice = Number.parseFloat(price);
expect(parsedPrice).toBeLessThanOrEqual(1.1);
expect(parsedPrice).toBeGreaterThanOrEqual(1);
}
});
it('should return values with three decimal places between min and max', () => {
const result = faker.helpers.multiple(
() => faker.commerce.price({ min: 0.001, max: 0.009, dec: 3 }),
{ count: 50 }
);
for (const price of result) {
const parsedPrice = Number.parseFloat(price);
expect(parsedPrice).toBeLessThanOrEqual(0.009);
expect(parsedPrice).toBeGreaterThanOrEqual(0.001);
}
});
});
describe(`productAdjective()`, () => {
it('should return random value from product adjective array', () => {
const actual = faker.commerce.productAdjective();
expect(faker.definitions.commerce.product_name.adjective).toContain(
actual
);
});
});
describe(`productMaterial()`, () => {
it('should return random value from product material array', () => {
const actual = faker.commerce.productMaterial();
expect(faker.definitions.commerce.product_name.material).toContain(
actual
);
});
});
describe(`product()`, () => {
it('should return random value from product array', () => {
const actual = faker.commerce.product();
expect(faker.definitions.commerce.product_name.product).toContain(
actual
);
});
});
describe(`productDescription()`, () => {
it('should return a product description string', () => {
const actual = faker.commerce.productDescription();
expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
});
});
describe(`isbn()`, () => {
it('should return ISBN-13 with hyphen separators when not passing arguments', () => {
const isbn = faker.commerce.isbn();
expect(isbn).toBeTruthy();
expect(isbn).toBeTypeOf('string');
expect(
isbn,
'The expected match should be ISBN-13 with hyphens'
).toMatch(/^978-[01]-[\d-]{9}-\d$/);
expect(isbn).toSatisfy((isbn: string) => isISBN(isbn, 13));
});
it('should return ISBN-10 with hyphen separators when passing variant 10 as argument', () => {
const isbn = faker.commerce.isbn(10);
expect(
isbn,
'The expected match should be ISBN-10 with hyphens'
).toMatch(/^[01]-[\d-]{9}-[\dX]$/);
expect(isbn).toSatisfy((isbn: string) => isISBN(isbn, 10));
});
it('should return ISBN-13 with hyphen separators when passing variant 13 as argument', () => {
const isbn = faker.commerce.isbn(13);
expect(
isbn,
'The expected match should be ISBN-13 with hyphens'
).toMatch(/^978-[01]-[\d-]{9}-\d$/);
expect(isbn).toSatisfy((isbn: string) => isISBN(isbn, 13));
});
it('should return ISBN-10 with space separators when passing variant 10 and space separators as argument', () => {
const isbn = faker.commerce.isbn({ variant: 10, separator: ' ' });
expect(
isbn,
'The expected match should be ISBN-10 with space separators'
).toMatch(/^[01] [\d ]{9} [\dX]$/);
expect(isbn).toSatisfy((isbn: string) => isISBN(isbn, 10));
});
it('should return ISBN-13 with space separators when passing space separators as argument', () => {
const isbn = faker.commerce.isbn({ separator: ' ' });
expect(
isbn,
'The expected match should be ISBN-13 with space separators'
).toMatch(/^978 [01] [\d ]{9} \d$/);
expect(isbn).toSatisfy((isbn: string) => isISBN(isbn, 13));
});
});
}
);
});
|