-
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.
- Loading branch information
1 parent
0629109
commit bd1047e
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UsersService } from './users.service'; | ||
import { User } from './models/user.model'; | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService], | ||
}).compile(); | ||
|
||
service = module.get<UsersService>(UsersService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should return a user by id', () => { | ||
const user: User = service.findById(1); | ||
expect(user).toEqual({ id: 1, name: 'John Doe' }); | ||
}); | ||
|
||
it('should return undefined if user is not found', () => { | ||
const user: User = service.findById(3); | ||
expect(user).toBeUndefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { User } from './models/user.model'; | ||
|
||
@Injectable() | ||
export class UsersService { | ||
private users: User[] = [ | ||
{ id: 1, name: 'John Doe' }, | ||
{ id: 2, name: 'Richard Roe' }, | ||
]; | ||
|
||
findById(id: number): User | undefined { | ||
return this.users.find((user) => user.id === Number(id)); | ||
} | ||
} |