Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extend params feature #29

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,39 @@ export class Factory<Model, Attributes, Params = any, ReturnType = Model> {
);
}

/**
* Extends the current factory passing specific transient parameters. This is useful if you have a factory
* with transient parameters that defines the type of returned model, and you want to easily create variants of it.
* @param newParams The new transient parameters.
* @returns A new factory with the extended transient parameters.
* @example
* const userWithRolesFactory = userFactory.extend<{ roles: string[] }>(({ transientParams }) => {
* return {
* roles: transientParams.roles ?? [],
* }
* })
*
* const userWithAdminRoleFactory = userWithRolesFactory.extendParams({ roles: ['admin'] });
* const userWithAdminRole = await userWithAdminRoleFactory.build();
* // userWithAdminRole.roles === ['admin']
*/
extendParams<ExtendedParams extends Params = Params>(
newParams: ExtendedParams,
): Factory<Model, Attributes, ExtendedParams, ReturnType> {
const newDefaultAttributesFactory = async () => {
return await this.defaultAttributesFactory({
transientParams: newParams,
});
};
return new Factory(
newDefaultAttributesFactory,
this.model,
this._adapter,
this.afterCreateHooks,
this.afterBuildHooks,
);
}

/**
* Adds a hook that is called after the model is created.
* @param afterCreateHook A function that is called after the model is created.
Expand Down
28 changes: 28 additions & 0 deletions test/factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,34 @@ describe('Factory', () => {
email: 'user@company.com',
});
});

it('extends the factory and pass custom parameters', async () => {
// Arrange
type UserTransientParams = {
prefix: 'Mr.' | 'Mrs.';
};
const userFactoryWithTransientParams = FactoryGirl.define<
User,
User,
UserTransientParams
>(plainObject<User>(), ({ transientParams }) => ({
...buildUserAttributes(),
name: `${transientParams?.prefix} ${buildUserAttributes().name}`,
}));

const mrUserFactory = userFactoryWithTransientParams.extendParams({
prefix: 'Mr.',
});

// Act
const mrUser = await mrUserFactory.build();

// Assert
expect(mrUser).toEqual({
...buildUserAttributes(),
name: 'Mr. John Doe',
});
});
});

describe('associate', () => {
Expand Down
Loading