Skip to content

Commit 11502c9

Browse files
authored
[feat-21] Rename afterCreate to afterBuild (#25)
1 parent ee0f8ac commit 11502c9

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ userFactory.build().fullName; // 'Alice Smith'
235235
236236
### Transient Params
237237
238-
Transient params are arguments that can be passed to the build method that are not merged into the returned object. They can be used to provide options to attribute builders and afterCreate hooks.
238+
Transient params are arguments that can be passed to the build method that are not merged into the returned object. They can be used to provide options to attribute builders and afterBuild hooks.
239239
240240
The `transient` method defines the default values for transient params. The types for transient params are inferred by the compiler and will be type-safe in the build method, just like regular attributes.
241241
@@ -251,7 +251,7 @@ const contactFactory = createFactory(factory =>
251251
}
252252
name: 'Alice',
253253
})
254-
.afterCreate(({ entity, transientParams }) => {
254+
.afterBuild(({ entity, transientParams }) => {
255255
if (transientParams.downcaseName) {
256256
entity.name = entity.name.toLowerCase();
257257
}
@@ -303,7 +303,7 @@ user.isAdmin; // true
303303
user.isActive; // false
304304
```
305305
306-
Traits can define their own transient params and after create hooks using the alternative builder syntax.
306+
Traits can define their own transient params and after build hooks using the alternative builder syntax.
307307
308308
```typescript
309309
import { postFactory } from './post-factory';
@@ -323,7 +323,7 @@ const userFactory = createFactory((factory) =>
323323
.attributes({
324324
type: 'author'
325325
})
326-
.afterCreate(({ entity, transientParams }) => {
326+
.afterBuild(({ entity, transientParams }) => {
327327
const { postCount } = transientParams;
328328
for (let i = 0; i < postCount; i++) {
329329
entity.posts.push(...postFactory.buildList(postCount))
@@ -350,7 +350,7 @@ const contactFactory = createFactory((factory) =>
350350
phone: '(555) 123,4567',
351351
name: 'Alice',
352352
})
353-
.afterCreate(({ entity, transientParams }) => {
353+
.afterBuild(({ entity, transientParams }) => {
354354
if (transientParams.upcaseName) {
355355
entity.name = entity.name.toUpperCase();
356356
}
@@ -368,7 +368,7 @@ contact.name; // 'ALICE'
368368
369369
### Extending Factories
370370
371-
Factories can extend from one or more parent factories. This is helpful for sharing logic between factories and modeling inheritance. Transient params, attributes, traits, and after create hooks defined on the parent will be inherited.
371+
Factories can extend from one or more parent factories. This is helpful for sharing logic between factories and modeling inheritance. Transient params, attributes, traits, and after build hooks defined on the parent will be inherited.
372372
373373
#### Sharing logic
374374

src/__tests__/factory.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('build', () => {
3131
.attributes({
3232
role: 'owner',
3333
})
34-
.afterCreate(({ entity, transientParams }) => {
34+
.afterBuild(({ entity, transientParams }) => {
3535
if (transientParams.skipOwnerHook) return;
3636
entity.hooks.push('owner');
3737
})
@@ -42,20 +42,20 @@ describe('build', () => {
4242
.attributes({
4343
type: 'bot',
4444
})
45-
.afterCreate(({ entity, transientParams }) => {
45+
.afterBuild(({ entity, transientParams }) => {
4646
if (transientParams.skipBotHook) return;
4747
entity.hooks.push('bot1');
4848
})
49-
.afterCreate(({ entity, transientParams }) => {
49+
.afterBuild(({ entity, transientParams }) => {
5050
if (transientParams.skipBotHook) return;
5151
entity.hooks.push('bot2');
5252
})
5353
)
54-
.afterCreate(({ entity, transientParams }) => {
54+
.afterBuild(({ entity, transientParams }) => {
5555
if (transientParams.skipGlobalHook) return;
5656
entity.hooks.push('global1');
5757
})
58-
.afterCreate(({ entity, transientParams }) => {
58+
.afterBuild(({ entity, transientParams }) => {
5959
if (transientParams.skipGlobalHook) return;
6060
entity.hooks.push('global2');
6161
})
@@ -151,12 +151,12 @@ describe('build', () => {
151151
expect(ownerUser.role).toBe('owner');
152152
});
153153

154-
it('runs global afterCreate hooks after trait afterCreate hooks', () => {
154+
it('runs global afterBuild hooks after trait afterBuild hooks', () => {
155155
const user = factory.build('owner', 'bot');
156156
expect(user.hooks).toEqual(['owner', 'bot1', 'bot2', 'global1', 'global2']);
157157
});
158158

159-
it('runs trait afterCreate hooks in same order as provided traits', () => {
159+
it('runs trait afterBuild hooks in same order as provided traits', () => {
160160
const user1 = factory.build('owner', 'bot', { skipGlobalHook: true });
161161
expect(user1.hooks).toEqual(['owner', 'bot1', 'bot2']);
162162

@@ -251,7 +251,7 @@ describe('buildList', () => {
251251

252252
describe('DSL', () => {
253253
describe('extends', () => {
254-
it('can inherit attributes, transientParams, traits, and afterCreate hooks', () => {
254+
it('can inherit attributes, transientParams, traits, and afterBuild hooks', () => {
255255
interface BaseContact {
256256
id: number;
257257
phone: string;
@@ -268,11 +268,11 @@ describe('DSL', () => {
268268
})
269269
.trait('invalidPhone', { phone: 'asdf' })
270270
.trait('withCountryCode', (trait) =>
271-
trait.transient({ countryCode: 1 }).afterCreate(({ entity }) => {
271+
trait.transient({ countryCode: 1 }).afterBuild(({ entity }) => {
272272
entity.phone = `+1 ${entity.phone}`;
273273
})
274274
)
275-
.afterCreate(({ entity }) => {
275+
.afterBuild(({ entity }) => {
276276
entity.phone = entity.phone + ' x123';
277277
})
278278
);
@@ -288,14 +288,14 @@ describe('DSL', () => {
288288
businessName: 'Mega Lo Mart',
289289
})
290290
.trait('invalidName', { businessName: '' })
291-
.afterCreate(({ entity, transientParams }) => {
291+
.afterBuild(({ entity, transientParams }) => {
292292
if (transientParams.upcaseName) {
293293
entity.businessName = entity.businessName.toUpperCase();
294294
}
295295
})
296296
);
297297

298-
// inherits attributes, transientParam defaults, and afterCreate hooks
298+
// inherits attributes, transientParam defaults, and afterBuild hooks
299299
const business1 = businessFactory.build();
300300
expect(business1.id).toBe(1);
301301
expect(business1.phone).toBe('(555) 123-4567 x123');
@@ -397,11 +397,11 @@ describe('DSL', () => {
397397
.trait('parentTrait', (trait) =>
398398
trait
399399
.transient({ parentTraitTransientParam: 1 })
400-
.afterCreate(({ entity }) => {
400+
.afterBuild(({ entity }) => {
401401
entity.hooks.push('parentTraitHook');
402402
})
403403
)
404-
.afterCreate(({ entity }) => {
404+
.afterBuild(({ entity }) => {
405405
entity.hooks.push('parentHook');
406406
})
407407
);
@@ -413,11 +413,11 @@ describe('DSL', () => {
413413
.trait('parentTrait', (trait) =>
414414
trait
415415
.attributes({ childOneAttribute: 2 })
416-
.afterCreate(({ entity }) => {
416+
.afterBuild(({ entity }) => {
417417
entity.hooks.push('childOneTraitHook');
418418
})
419419
)
420-
.afterCreate(({ entity }) => {
420+
.afterBuild(({ entity }) => {
421421
entity.hooks.push('childOneHook');
422422
})
423423
);
@@ -429,11 +429,11 @@ describe('DSL', () => {
429429
.trait('parentTrait', (trait) =>
430430
trait
431431
.attributes({ childTwoAttribute: 1 })
432-
.afterCreate(({ entity }) => {
432+
.afterBuild(({ entity }) => {
433433
entity.hooks.push('childTwoTraitHook');
434434
})
435435
)
436-
.afterCreate(({ entity }) => {
436+
.afterBuild(({ entity }) => {
437437
entity.hooks.push('childTwoHook');
438438
})
439439
);
@@ -632,15 +632,15 @@ describe('DSL', () => {
632632
expect(user2.name).toBe('odd');
633633
});
634634

635-
it('can use transientParams in afterCreate', () => {
635+
it('can use transientParams in afterBuild', () => {
636636
const factory = createFactory((factory) =>
637637
factory
638638
.transient({ globalTransientId: 10 })
639639
.attributes<{ id: number; name: string }>({ id: 1, name: 'name' })
640640
.trait('transient', (trait) =>
641641
trait
642642
.transient({ traitTransientName: 'transientNameDefault' })
643-
.afterCreate(({ entity, transientParams }) => {
643+
.afterBuild(({ entity, transientParams }) => {
644644
entity.id = transientParams.globalTransientId;
645645
entity.name = transientParams.traitTransientName;
646646
})
@@ -662,13 +662,13 @@ describe('DSL', () => {
662662
});
663663
});
664664

665-
describe('afterCreate', () => {
665+
describe('afterBuild', () => {
666666
it('can use transientParams', () => {
667667
const factory = createFactory((factory) =>
668668
factory
669669
.transient({ globalTransientId: 10 })
670670
.attributes<{ id: number }>({ id: 1 })
671-
.afterCreate(({ entity, transientParams }) => {
671+
.afterBuild(({ entity, transientParams }) => {
672672
entity.id = transientParams.globalTransientId;
673673
})
674674
);

src/factory.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ interface TraitBuilder<
8585
>
8686
): FinalBuilder<TraitBuilder<Entity, GlobalTransientParams, TransientParams>>;
8787

88-
afterCreate(
89-
afterCreateCallback: (params: {
88+
afterBuild(
89+
afterBuildCallback: (params: {
9090
entity: Entity;
9191
transientParams: GlobalTransientParams & TransientParams;
9292
}) => void
@@ -153,8 +153,8 @@ interface FactoryBuilder<
153153
>
154154
>;
155155

156-
afterCreate(
157-
afterCreateCallback: (params: {
156+
afterBuild(
157+
afterBuildCallback: (params: {
158158
entity: OuterEntity;
159159
transientParams: OuterGlobalTransientParams;
160160
}) => void
@@ -214,10 +214,10 @@ interface FactoryDefinition {
214214
{
215215
attributeDefaults: Record<string, any>;
216216
transientParamDefaults: Record<string, any>;
217-
afterCreateHooks: Array<(...args: Array<any>) => void>;
217+
afterBuildHooks: Array<(...args: Array<any>) => void>;
218218
}
219219
>;
220-
afterCreateHooks: Array<(...args: Array<any>) => void>;
220+
afterBuildHooks: Array<(...args: Array<any>) => void>;
221221
sequence: { count: number };
222222
}
223223

@@ -230,7 +230,7 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
230230
attributeDefaults: {},
231231
transientParamDefaults: {},
232232
traits: {},
233-
afterCreateHooks: [],
233+
afterBuildHooks: [],
234234
sequence: { count: -1 },
235235
};
236236

@@ -296,11 +296,11 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
296296
}
297297
}
298298

299-
// after create hooks
299+
// after build hooks
300300
for (let traitName of traitNames.reverse()) {
301301
const trait = definition.traits[traitName];
302-
for (let afterCreate of trait.afterCreateHooks) {
303-
afterCreate({
302+
for (let afterBuild of trait.afterBuildHooks) {
303+
afterBuild({
304304
entity,
305305
transientParams: {
306306
...trait.transientParamDefaults,
@@ -310,8 +310,8 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
310310
}
311311
}
312312

313-
for (let afterCreate of definition.afterCreateHooks) {
314-
afterCreate({ entity, transientParams });
313+
for (let afterBuild of definition.afterBuildHooks) {
314+
afterBuild({ entity, transientParams });
315315
}
316316

317317
return entity as Entity;
@@ -354,7 +354,7 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
354354
const trait = parentDefinition.traits[traitName];
355355
definition.traits[traitName] = { ...trait };
356356
}
357-
definition.afterCreateHooks.push(...parentDefinition.afterCreateHooks);
357+
definition.afterBuildHooks.push(...parentDefinition.afterBuildHooks);
358358
}
359359
return factoryBuilder;
360360
},
@@ -370,7 +370,7 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
370370
definition.traits[name] = {
371371
attributeDefaults: {},
372372
transientParamDefaults: {},
373-
afterCreateHooks: [],
373+
afterBuildHooks: [],
374374
};
375375
if (typeof traitBuilderArg === 'function') {
376376
const traitBuilder: TraitBuilder<any, any, any> = {
@@ -382,8 +382,8 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
382382
definition.traits[name].transientParamDefaults = params;
383383
return traitBuilder;
384384
},
385-
afterCreate(afterCreateCallback) {
386-
definition.traits[name].afterCreateHooks.push(afterCreateCallback);
385+
afterBuild(afterBuildCallback) {
386+
definition.traits[name].afterBuildHooks.push(afterBuildCallback);
387387
return traitBuilder;
388388
},
389389
};
@@ -393,8 +393,8 @@ export function createFactory<Entity, GlobalTransientParams, Traits>(
393393
}
394394
return factoryBuilder;
395395
},
396-
afterCreate(afterCreateCallback) {
397-
definition.afterCreateHooks.push(afterCreateCallback);
396+
afterBuild(afterBuildCallback) {
397+
definition.afterBuildHooks.push(afterBuildCallback);
398398
return factoryBuilder;
399399
},
400400
};

0 commit comments

Comments
 (0)