Skip to content

Commit

Permalink
Merge pull request #28 from thiagomini/feat/add-custom-adapter
Browse files Browse the repository at this point in the history
feat: allows creation of factory with specific adapter
  • Loading branch information
thiagomini authored Dec 10, 2023
2 parents d963ad9 + e93997a commit 5e5f0ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/factory-girl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class FactoryGirl {
* to work properly.
* @param model The model to define a factory for. It can be either a model class or an interface.
* @param defaultAttributesFactory A function that returns the default attributes for the model.
* @param adapter (optional) The adapter to use to build and save the model.
* @returns A factory for the model.
*/
static define<
Expand All @@ -56,11 +57,13 @@ export class FactoryGirl {
>(
model: ModelOrInterface,
defaultAttributesFactory: DefaultAttributesFactory<Attributes, Parameters>,
adapter?: ModelAdapter<ModelOrInterface, ReturnType>,
): Factory<ModelOrInterface, Attributes, Parameters, ReturnType> {
return new Factory<ModelOrInterface, Attributes, Parameters, ReturnType>(
defaultAttributesFactory,
model,
() => this.adapter as ModelAdapter<ModelOrInterface, ReturnType>,
() =>
adapter ?? (this.adapter as ModelAdapter<ModelOrInterface, ReturnType>),
);
}

Expand Down
45 changes: 31 additions & 14 deletions test/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FactoryGirl } from '@src/factory-girl';
import { plainObject } from '@src/utils';
import { ObjectAdapter, SequelizeAdapter } from '../lib';
import { ModelAdapter, ObjectAdapter, SequelizeAdapter } from '../lib';
import { Association, DeepPartialAttributes } from '../src';

type User = {
Expand Down Expand Up @@ -649,19 +649,6 @@ describe('Factory', () => {
});
});

it('extends the factory with new attributes', async () => {
// Act
const companyEmailUserFactory = userFactory.extend(() => ({
anotherAttribute: 'value',
}));

// Assert
await expect(companyEmailUserFactory.build()).resolves.toEqual({
...buildUserAttributes(),
anotherAttribute: 'value',
});
});

it('extends the factory with async attributes', async () => {
// Act
const companyEmailUserFactory = userFactory.extend(async () => ({
Expand Down Expand Up @@ -880,6 +867,36 @@ describe('Factory', () => {
// Assert
expect(user).toBeTruthy();
});

test('creates factory with given adapter', async () => {
// Arrange
class TestableAdapter implements ModelAdapter<User, User> {
build(): User {
throw new Error('Method not implemented.');
}
save(): Promise<User> {
throw new Error('Method not implemented.');
}
get<K extends keyof User>(): User[K] {
throw new Error('Method not implemented.');
}
}

// Act
const userFactory = FactoryGirl.define(
plainObject<User>(),
() => {
return {
name: 'John Doe',
email: '',
};
},
new TestableAdapter(),
);

// Assert
expect(userFactory.adapter).toBeInstanceOf(TestableAdapter);
});
});

describe('hooks', () => {
Expand Down

0 comments on commit 5e5f0ce

Please sign in to comment.