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

Criar repository para consultar o usuario por id #46

Merged
merged 11 commits into from
May 5, 2024
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ describe('[Repositories] UserRepository', () => {

await repository.create(user);

const { id, ...userWithoutId } = user;

expect(prisma.user.create).toHaveBeenCalledWith({
data: user,
data: userWithoutId,
});
});

Expand All @@ -36,4 +38,43 @@ describe('[Repositories] UserRepository', () => {
await expect(response).rejects.toThrowError();
});
});

describe('findById', () => {
it('return user if found', async () => {
const { repository } = makeSut();

const user = UserMock.create();

prisma.user.findUnique.mockResolvedValue(user);

const result = await repository.findById(user.id);

expect(prisma.user.findUnique).toHaveBeenCalledWith({
select: expect.anything(),
where: {
id: user.id,
},
});

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

it('return null if user is not found', async () => {
const { repository } = makeSut();

// eslint-disable-next-line unicorn/no-null
davidambz marked this conversation as resolved.
Show resolved Hide resolved
prisma.user.findUnique.mockResolvedValue(null);
davidambz marked this conversation as resolved.
Show resolved Hide resolved

const result = await repository.findById(0);

expect(prisma.user.findUnique).toHaveBeenCalledWith({
select: expect.anything(),
where: {
id: 0,
},
});

expect(result).toBeNull();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ export class UserRepository {
},
});
}

async findById(id: string) {
const user = await database.user.findUnique({
select: expect.anything(),
davidambz marked this conversation as resolved.
Show resolved Hide resolved
where: {
id,
},
});

return user;
}
}
1 change: 1 addition & 0 deletions src/shared/test-helpers/mocks/user.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class UserMock {
public static create() {
return {
email: faker.internet.email(),
id: faker.string.numeric(),
name: faker.person.firstName(),
password: faker.internet.password(),
username: faker.internet.userName(),
Expand Down
Loading