Skip to content

Commit

Permalink
Criar repository para consultar o usuario por id (#46)
Browse files Browse the repository at this point in the history
* feat: add findById method to user repository

* test: add tests for findById method in user repository

* fix: resolve ESLint configuration issue

* feat: add name property to user model, update tests, and migrate changes

* chore: remove eslint-plugin-array-func from dependencies

* chore: remove migration file no longer needed

* fix: corrected schema.prisma syntax issue

* refactor: change id type from Int to String in repository and user mock

* test: update findById test

* test: update findById test

* feat: update findById method to user repository
  • Loading branch information
davidambz authored May 5, 2024
1 parent c4d1209 commit 4156468
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
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);

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

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

expect(result).toBeNull();
});
});
});
16 changes: 16 additions & 0 deletions src/features/user/repositories/user-repository/user-repository.ts
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: {
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

0 comments on commit 4156468

Please sign in to comment.