Skip to content

Commit

Permalink
Fix tests #181
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Jan 18, 2025
1 parent aa36f7b commit 459e577
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import { beforeAll, describe, it, expect, jest } from '@jest/globals';
import { beforeAll, beforeEach, describe, it, expect, jest } from '@jest/globals';
import SignupPostController from '@src/account/app/controllers/v1/auth/signupPostController';
import { CommandBus } from '@src/shared/domain/bus/commandBus/commandBus';
import { SignUpUserCommandMother } from '@test/unit/account/application/auth/command/signUpUserCommandMother';
import { IdentityProviderMock } from '@test/unit/shared/domain/services/IdentityProviderMock';

describe('Given a SignupPostController to handle', () => {
let sut: SignupPostController;
let identityProvider: IdentityProviderMock;
let commandBusMock: { dispatch: jest.Mock };

const prepareDependencies = () => {
identityProvider = new IdentityProviderMock();

commandBusMock = {
dispatch: jest.fn(),
};
};

const initHandler = () => {
sut = new SignupPostController(commandBusMock as CommandBus);
sut = new SignupPostController(commandBusMock as CommandBus, identityProvider);

jest.useFakeTimers();
};

const clean = () => {
identityProvider.clean();
};

beforeAll(() => {
prepareDependencies();
initHandler();
clean();
});

describe('When received a request', () => {
function startScenario() {
identityProvider.add('4a4df157-8ab8-50af-bb39-88e8ce29eb16');
}

beforeEach(startScenario);

it('should create an user', async () => {
const data = {
email: '[email protected]',
Expand All @@ -33,6 +48,7 @@ describe('Given a SignupPostController to handle', () => {
provider: 'google',
photo: '',
};

await sut.run(data);

expect(commandBusMock.dispatch).toHaveBeenCalledWith(
Expand All @@ -45,6 +61,7 @@ describe('Given a SignupPostController to handle', () => {
photo: data.photo,
}),
);

expect(commandBusMock.dispatch).toHaveBeenCalledTimes(1);
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/unit/shared/domain/services/IdentityProviderMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IdentityProvider } from '@src/shared/domain/services/IdentityProvider';

export class IdentityProviderMock implements IdentityProvider {
private value: string;

add(value: string): void {
this.value = value;
}

clean(): void {
this.value = '';
}

generate(): string {
return this.value;
}

generateFromValue(_value: string): string {
return this.value;
}
}

0 comments on commit 459e577

Please sign in to comment.