Skip to content

Commit

Permalink
feat: ✨ add users service
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 8, 2024
1 parent 0629109 commit bd1047e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apps/users-application/src/users/users.service.spec.ts
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();
});
});
14 changes: 14 additions & 0 deletions apps/users-application/src/users/users.service.ts
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));
}
}

0 comments on commit bd1047e

Please sign in to comment.