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
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,53 @@ describe('[Repositories] UserRepository', () => {
await expect(response).rejects.toThrowError();
});
});

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

const user = UserMock.create();

const expectedResult = {
createdAt: new Date(),
deletedAt: null,
email: user.email,
id: user.id,
name: user.name,
password: user.password,
updatedAt: new Date(),
username: user.username,
};

prisma.user.findUnique.mockResolvedValue(expectedResult);

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

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

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

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

prisma.user.findUnique.mockResolvedValue(null);
davidambz marked this conversation as resolved.
Show resolved Hide resolved

const result = await repository.findById('non_existent_id');

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

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

async findById(id: string) {
const user = await database.user.findUnique({
select: {
davidambz marked this conversation as resolved.
Show resolved Hide resolved
email: true,
id: true,
name: true,
username: true,
},
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