-
Notifications
You must be signed in to change notification settings - Fork 1
/
factory-t.test.ts
300 lines (269 loc) · 9.87 KB
/
factory-t.test.ts
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { factoryTBuilder, factoryT, FactoryT, fields } from 'factory-t';
describe(`${FactoryT.name}`, () => {
describe('item()', () => {
it('makes each new instance with incremented index', () => {
const factory = factoryT<{ strWithId: string; id: number }>({
id: fields.index(),
strWithId: ({ index }) => `id=${index}`,
});
expect(factory.item()).toStrictEqual({
id: 1,
strWithId: 'id=1',
});
expect(factory.item()).toStrictEqual({
id: 2,
strWithId: 'id=2',
});
});
it('recognize null as property value', () => {
const factory = factoryT<{ id: number | null }>({
id: fields.nullable<number>(null),
});
expect(factory.item()).toStrictEqual({
id: null,
});
});
it('recognize empty array as property value', () => {
const factory = factoryT<{ ids: number[] }>({
ids: [],
});
expect(factory.item()).toStrictEqual({
ids: [],
});
});
it('handle optional property value when field factory not provided', () => {
interface WithOptional {
name: string;
optional?: string;
}
const factory = factoryT<WithOptional>({
name: 'hello',
});
expect(factory.item()).toStrictEqual({
name: 'hello',
});
expect(factory.item({ optional: 'optional-value' })).toStrictEqual({
name: 'hello',
optional: 'optional-value',
});
});
it('resolve props dependencies', () => {
const factory = factoryTBuilder<{
C: string;
A: string;
B: string;
}>({
C: 'C:will be rewrite)',
A: 'A',
B: 'B:will be rewrite)',
})
.setFieldFactory('C', (ctx) => `C(${ctx.inject('A')},${ctx.inject('B')})`)
.setFieldFactory('B', (ctx) => `B(${ctx.inject('A')})`)
.factory();
expect(factory.item()).toStrictEqual({
A: 'A',
B: 'B(A)',
C: 'C(A,B(A))',
});
});
it('override prop values from passed object', () => {
const factory = factoryT<{ a: string; b: string }>({
a: 'a',
b: 'b',
});
expect(factory.item({ b: 'override b' })).toStrictEqual({
a: 'a',
b: 'override b',
});
});
it('works with nested objects/arrays passed directly', () => {
const factory = factoryT<{
nestedObj: { child: string };
nestedArray: number[];
}>({
nestedObj: { child: 'nested.child' },
nestedArray: [1, 2],
});
expect(factory.item()).toStrictEqual({
nestedObj: {
child: 'nested.child',
},
nestedArray: [1, 2],
});
});
it('works with nested objects using { value: nestedObj } config', () => {
const factory = factoryT<{ nested: { child: string } }>({
nested: { child: 'nested.child' },
});
expect(factory.item()).toStrictEqual({
nested: {
child: 'nested.child',
},
});
});
it('(example) use another FactoryT for nested object', () => {
interface DataWithNestedObj {
id: string;
nested: {
name: string;
};
}
const nestedFactory = factoryT<DataWithNestedObj['nested']>({
name: ({ index }) => 'nested-' + index,
});
const factory = factoryTBuilder<DataWithNestedObj>({
id: ({ index }) => 'parent-' + index,
nested: nestedFactory.item(),
})
.setFieldFactory('nested', (ctx) =>
nestedFactory.item({
name: `nested-object-of-${ctx.inject('id')}`,
}),
)
.factory();
expect(factory.item()).toStrictEqual({
id: 'parent-1',
nested: {
name: 'nested-object-of-parent-1',
},
});
});
it('throw error with clear message when circular dependency between fields detected', () => {
const factory = factoryTBuilder({
a: 'a',
b: 'b',
})
.setFieldFactory('a', (ctx) => ctx.inject('b'))
.setFieldFactory('b', (ctx) => ctx.inject('a'))
.factory();
expect(() => factory.item()).toThrow('circular');
});
});
describe('list(...)', () => {
it('creates array of instances of size provided by "count" input property', () => {
const factory = factoryT<{ id: number }>({
id: fields.index(),
});
expect(factory.list({ count: 3 })).toStrictEqual([{ id: 1 }, { id: 2 }, { id: 3 }]);
});
it('creates array of instances using array of "partials"', () => {
const factory = factoryT<{ id: number; name: string }>({
id: fields.index(),
name: 'default-name',
});
expect(
factory.list({ partials: [{ name: 'first' }, {}, { name: 'third' }] }),
).toStrictEqual([
{ id: 1, name: 'first' },
{ id: 2, name: 'default-name' },
{ id: 3, name: 'third' },
]);
});
it('throw error if "count" < "partials.length"', () => {
const factory = factoryT<{ id: number }>({
id: fields.index(),
});
expect(() =>
factory.list({
partials: [{ id: 3 }, { id: 2 }, { id: 1 }],
count: 2,
}),
).toThrow('assertion error');
});
it('throw error if "partials.length" === 0', () => {
const factory = factoryT<{ id: number }>({
id: fields.index(),
});
expect(() =>
factory.list({
partials: [],
}),
).toThrow('assertion error');
});
it(
'creates array of instances of size "count" using' +
' data from "partials" for first "partials.length" items',
() => {
const factory = factoryT<{ id: number }>({
id: fields.index(),
});
expect(
factory.list({
count: 3,
partials: [{ id: 100 }, { id: 200 }],
}),
).toStrictEqual([{ id: 100 }, { id: 200 }, { id: 3 }]);
},
);
it('creates empty array when "count=0"', () => {
const factory = factoryT<{ id: number }>({
id: fields.index(),
});
expect(factory.list({ count: 0 })).toStrictEqual([]);
});
});
describe('factoryBuilder.extends(...)', () => {
it('creates new factory that extends base factory', () => {
enum DataType {
One,
Two,
}
interface Data {
firstName: string;
enum: DataType;
union: 'one' | 'two';
lastName: string;
mayBeNull: number | null;
}
const partialFactoryBuilder = factoryTBuilder({
firstName: (ctx) => `hello-${ctx.index}`,
enum: DataType.One,
union: (ctx) => (ctx.index % 2 ? 'one' : 'two'),
});
const dataFactory = partialFactoryBuilder
.inheritedBuilder<Data>({
lastName: 'as string',
mayBeNull: fields.nullable(12),
})
.factory();
expect(dataFactory.item({ mayBeNull: null })).toStrictEqual({
firstName: 'hello-1',
enum: DataType.One,
union: 'one',
lastName: 'as string',
mayBeNull: null,
});
});
});
describe('use options to more flexible data generate', () => {
interface Data {
email: string;
}
interface Options {
variant: 'google' | 'yahoo';
}
function factoryWithOptions(): FactoryT<Data, Options> {
return factoryT<Data, Options>({
email: (ctx) => {
const mailVendor = ctx.options ? ctx.options.variant : 'unknown';
return `e@${mailVendor}`;
},
});
}
it('item({...}, options) reflected to passed options', () => {
const dataFactory = factoryWithOptions();
expect(dataFactory.item({}, { variant: 'google' })).toStrictEqual({
email: 'e@google',
});
expect(dataFactory.item({ email: '123@custom' }, { variant: 'google' })).toStrictEqual({
email: '123@custom',
});
});
it('list({...}, options) reflected to passed options', () => {
const dataFactory = factoryWithOptions();
expect(
dataFactory.list({ partials: [{ email: 'custom' }, {}] }, { variant: 'google' }),
).toStrictEqual([{ email: 'custom' }, { email: 'e@google' }]);
});
});
});