Skip to content

Commit

Permalink
Create UsersService test
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed May 14, 2024
1 parent dcd2453 commit 148dd38
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 0 additions & 1 deletion test/routes/server.$serverId.tags.colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('server.$serverId.tags.colors', () => {

beforeEach(() => {
tagsService = fromPartial<TagsService>({ updateTagColors });
vi.clearAllMocks();
});

it('updates tags in action and returns response', async () => {
Expand Down
1 change: 0 additions & 1 deletion test/servers/ServersService.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('ServersService', () => {
beforeEach(() => {
em = fromPartial<EntityManager>({ findOneBy });
service = new ServersService(em);
vi.clearAllMocks();
});

describe('getByPublicId', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ axe.configure({
],
});

// Clears all mocks after every test
afterEach(() => {
vi.clearAllMocks();
});

if (typeof HTMLCanvasElement !== 'undefined') {
HTMLCanvasElement.prototype.getContext = (() => {}) as any;
}
1 change: 0 additions & 1 deletion test/tags/TagsService.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('TagsService', () => {
beforeEach(() => {
tagsService = new TagsService(em);
transaction.mockImplementation((callback) => callback(em));
vi.clearAllMocks();
});

describe('tagColors', () => {
Expand Down
41 changes: 41 additions & 0 deletions test/users/UsersService.server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { EntityManager } from 'typeorm';
import { hashPassword } from '../../app/auth/passwords.server';
import type { User } from '../../app/entities/User';
import { UsersService } from '../../app/users/UsersService.server';

describe('UsersService', () => {
const findOneBy = vi.fn();
let em: EntityManager;
let usersService: UsersService;

beforeEach(() => {
em = fromPartial<EntityManager>({ findOneBy });
usersService = new UsersService(em);
});

describe('getUserByCredentials', () => {
it('throws when user is not found', async () => {
findOneBy.mockResolvedValue(null);
await expect(() => usersService.getUserByCredentials('foo', 'bar')).rejects.toEqual(
new Error('User not found with username foo'),
);
});

it('throws if password does not match', async () => {
findOneBy.mockResolvedValue(fromPartial<User>({ password: await hashPassword('the right one') }));
await expect(() => usersService.getUserByCredentials('foo', 'bar')).rejects.toEqual(
new Error('Incorrect password for user foo'),
);
});

it('returns user if password is correct', async () => {
const expectedUser = fromPartial<User>({ password: await hashPassword('bar') });
findOneBy.mockResolvedValue(expectedUser);

const result = await usersService.getUserByCredentials('foo', 'bar');

expect(result).toEqual(expectedUser);
});
});
});

0 comments on commit 148dd38

Please sign in to comment.