@@ -2,9 +2,12 @@ import { Test, TestingModule } from '@nestjs/testing';
22import { DbUsersService } from './dbusers.service' ;
33import { AuthService } from '../auth/auth.service' ;
44import { PrismaService } from '../prisma/prisma.service' ;
5+ import { UserLoginResponseDto } from './dto/user-login-response.dto' ;
6+ import { Role } from '@prisma/client' ;
57
6- describe ( 'UsersService ' , ( ) => {
8+ describe ( 'DbUsersService ' , ( ) => {
79 let service : DbUsersService ;
10+ let prismaService : PrismaService ;
811
912 beforeEach ( async ( ) => {
1013 const module : TestingModule = await Test . createTestingModule ( {
@@ -15,17 +18,64 @@ describe('UsersService', () => {
1518 useValue : {
1619 user : {
1720 findMany : jest . fn ( ) . mockResolvedValueOnce ( [ ] ) ,
21+ create : jest . fn ( ) ,
1822 } ,
1923 } ,
2024 } ,
21- { provide : AuthService , useValue : { } } ,
25+ {
26+ provide : AuthService ,
27+ useValue : {
28+ generateApiKey : jest . fn ( ( ..._ : any [ ] ) => 'generatedApiKey' ) ,
29+ encryptPassword : jest . fn ( ( ..._ : any [ ] ) => 'encryptedPassword' ) ,
30+ } ,
31+ } ,
2232 ] ,
2333 } ) . compile ( ) ;
2434
2535 service = module . get < DbUsersService > ( DbUsersService ) ;
36+ prismaService = module . get < PrismaService > ( PrismaService ) ;
2637 } ) ;
2738
2839 it ( 'should be defined' , ( ) => {
2940 expect ( service ) . toBeDefined ( ) ;
3041 } ) ;
42+
43+ it ( 'should create a new user' , async ( ) => {
44+ // Arrange
45+ const createUserDto = {
46+ 47+ firstName : 'John' ,
48+ lastName : 'Doe' ,
49+ password : 'password123' ,
50+ } ;
51+
52+ const prismaUserCreateMock = jest . spyOn ( prismaService . user , 'create' ) ;
53+ prismaUserCreateMock . mockResolvedValueOnce ( {
54+ id : '1' ,
55+ 56+ firstName : 'John' ,
57+ lastName : 'Doe' ,
58+ apiKey : 'generatedApiKey' ,
59+ password : 'encryptedPassword' ,
60+ isActive : true ,
61+ role : Role . editor ,
62+ updatedAt : new Date ( ) ,
63+ createdAt : new Date ( ) ,
64+ } ) ;
65+
66+ // Act
67+ const result = await service . create ( createUserDto ) ;
68+
69+ // Assert
70+ expect ( prismaUserCreateMock ) . toHaveBeenCalledWith ( {
71+ data : {
72+ 73+ firstName : 'John' ,
74+ lastName : 'Doe' ,
75+ apiKey : 'generatedApiKey' ,
76+ password : 'encryptedPassword' ,
77+ } ,
78+ } ) ;
79+ expect ( result ) . toEqual ( expect . any ( UserLoginResponseDto ) ) ;
80+ } ) ;
3181} ) ;
0 commit comments