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 getAccounts method to account repository #56

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AccountRepository } from './account-repository.js';
import { prisma } from 'mocks/prisma.js';
import { AccountMock } from '@/shared/test-helpers/mocks/account.mock.js';

const makeSut = () => {
const repository = new AccountRepository();

return { repository };
};

describe('[Repositories] AccountRepository', () => {
describe('getAccounts', () => {
it('should return user accounts if found', async () => {
const { repository } = makeSut();

const account = AccountMock.create();

const expectedResult = [
{
avatarUrl: account.avatarUrl,
createdAt: new Date(),
id: account.id,
socialMediaId: account.socialMediaId,
updatedAt: new Date(),
userId: account.userId,
},
];

prisma.account.findMany.mockResolvedValue(expectedResult);

const result = await repository.getAccounts(account.userId);

expect(result[0]).toEqual(expectedResult[0]);
expect(prisma.account.findMany).toHaveBeenCalledWith({
where: {
userId: account.userId,
},
});
});

it('should return an empty array if user accounts are not found', async () => {
const { repository } = makeSut();

const userId = 'non_existent_user_id';

prisma.account.findMany.mockResolvedValue([]);

const result = await repository.getAccounts(userId);

expect(result).toEqual([]);
expect(prisma.account.findMany).toHaveBeenCalledWith({
where: {
userId: userId,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { database } from '@/shared/infra/database/database.js';

export class AccountRepository {
async getAccounts(userId: string) {
const userAccounts = await database.account.findMany({
where: {
userId: userId,
},
});

return userAccounts;
}
}
12 changes: 12 additions & 0 deletions src/shared/test-helpers/mocks/account.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { faker } from '@faker-js/faker';

export class AccountMock {
public static create() {
return {
avatarUrl: faker.image.avatar(),
id: faker.string.numeric(),
socialMediaId: faker.number.int(),
userId: faker.string.numeric(),
};
}
}