-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 remove null type in getUser response
- Loading branch information
1 parent
9504bdb
commit 2a0a169
Showing
3 changed files
with
51 additions
and
18 deletions.
There are no files selected for viewing
48 changes: 40 additions & 8 deletions
48
libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,62 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UsersService } from '@users/application'; | ||
|
||
import { GetUserByIdUseCase } from '@users/application'; | ||
import { UsersResolver } from './users.resolver'; | ||
import { UserDto } from '../dto/user.dto'; | ||
|
||
describe('UsersResolver', () => { | ||
let resolver: UsersResolver; | ||
let service: UsersService; | ||
let getUserByIdUseCase: jest.Mocked<GetUserByIdUseCase>; | ||
|
||
beforeEach(async () => { | ||
getUserByIdUseCase = { | ||
execute: jest.fn(), | ||
} as any; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
UsersResolver, | ||
{ | ||
provide: UsersService, | ||
useValue: { | ||
findOneById: jest.fn(), | ||
}, | ||
provide: GetUserByIdUseCase, | ||
useValue: getUserByIdUseCase, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
resolver = module.get<UsersResolver>(UsersResolver); | ||
service = module.get<UsersService>(UsersService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(resolver).toBeDefined(); | ||
}); | ||
|
||
describe('getUser', () => { | ||
it('should return a UserDto when user is found', async () => { | ||
const mockUser = { | ||
id: '1', | ||
email: '[email protected]', | ||
firstName: 'John', | ||
lastName: 'Doe' | ||
}; | ||
getUserByIdUseCase.execute.mockResolvedValue(mockUser); | ||
|
||
const result = await resolver.getUser('1'); | ||
|
||
expect(result).toBeInstanceOf(UserDto); | ||
expect(result).toEqual(new UserDto( | ||
mockUser.id, | ||
mockUser.email, | ||
mockUser.firstName, | ||
mockUser.lastName | ||
)); | ||
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1'); | ||
}); | ||
|
||
it('should propagate errors from use case', async () => { | ||
const error = new Error('Test error'); | ||
getUserByIdUseCase.execute.mockRejectedValue(error); | ||
|
||
await expect(resolver.getUser('1')).rejects.toThrow(error); | ||
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); |
14 changes: 6 additions & 8 deletions
14
libs/users/interface-adapters/src/lib/resolver/users.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters