diff --git a/src/features/account/repositories/account-repository/account-repository.test.ts b/src/features/account/repositories/account-repository/account-repository.test.ts new file mode 100644 index 0000000..e0d0477 --- /dev/null +++ b/src/features/account/repositories/account-repository/account-repository.test.ts @@ -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, + }, + }); + }); + }); +}); diff --git a/src/features/account/repositories/account-repository/account-repository.ts b/src/features/account/repositories/account-repository/account-repository.ts new file mode 100644 index 0000000..0ae1910 --- /dev/null +++ b/src/features/account/repositories/account-repository/account-repository.ts @@ -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; + } +} diff --git a/src/shared/test-helpers/mocks/account.mock.ts b/src/shared/test-helpers/mocks/account.mock.ts new file mode 100644 index 0000000..c7f3fde --- /dev/null +++ b/src/shared/test-helpers/mocks/account.mock.ts @@ -0,0 +1,20 @@ +import { faker } from '@faker-js/faker'; + +type Account = { + avatarUrl: string; + id: string; + socialMediaId: number; + userId: string; +}; + +export class AccountMock { + public static create(override?: Partial) { + return { + avatarUrl: faker.image.avatar(), + id: faker.string.numeric(), + socialMediaId: faker.number.int(), + userId: faker.string.numeric(), + ...override, + }; + } +}