diff --git a/src/modules/user/interfaces/user-document.interface.ts b/src/modules/user/interfaces/user-document.interface.ts new file mode 100644 index 0000000..829041a --- /dev/null +++ b/src/modules/user/interfaces/user-document.interface.ts @@ -0,0 +1,25 @@ +import { Document, Schema as SchemaTypes } from 'mongoose'; + +export interface UserDoc extends Document { + first_name: string; + + last_name: string; + + email: string; + + phone: string; + + address?: string; + + description?: string; + + isAdmin: boolean; + + photoUrl?: string; + + score: number; + + coverPhotoUrl?: string; + + wishlist: SchemaTypes.Types.ObjectId[]; +} diff --git a/src/modules/user/interfaces/user.interface.ts b/src/modules/user/interfaces/user.interface.ts new file mode 100644 index 0000000..a297436 --- /dev/null +++ b/src/modules/user/interfaces/user.interface.ts @@ -0,0 +1,27 @@ +import { Schema as SchemaTypes } from 'mongoose'; + +export interface User { + first_name: string; + + last_name: string; + + email: string; + + phone: string; + + address?: string; + + description?: string; + + isAdmin: boolean; + + photoUrl?: string; + + coverPhotoUrl?: string; + + score: number; + + _id: string; + + wishlist: SchemaTypes.Types.ObjectId[]; +} diff --git a/src/modules/user/user.service.spec.ts b/src/modules/user/user.service.spec.ts index 0edc3ca..7f55822 100644 --- a/src/modules/user/user.service.spec.ts +++ b/src/modules/user/user.service.spec.ts @@ -1,9 +1,93 @@ import { getModelToken } from '@nestjs/mongoose'; import { Test, TestingModule } from '@nestjs/testing'; import { UserService } from './user.service'; +import { createMock } from '@golevelup/nestjs-testing'; +import { Model, Query } from 'mongoose'; +import * as mongoose from 'mongoose'; +import { User } from './interfaces/user.interface'; +import { UserDoc } from './interfaces/user-document.interface'; +import { UpdateUserDTO } from './dto/update-user.dto'; + +const mockUser = ( + wishlist: [], + isAdmin: false, + _id: '6079f573062890a5e2cad207', + first_name: 'John', + last_name: 'Doe', + email: 'john@example.com', + phone: '9909999099', + photoUrl: 'https://google.com/john', + address: 'Block C Amsterdam', + description: 'Aspiring Software Developer', + score: 0, + coverPhotoUrl: 'https://google.com/john', +): User => ({ + wishlist, + isAdmin, + _id, + first_name, + last_name, + email, + phone, + photoUrl, + address, + description, + score, + coverPhotoUrl, +}); + +const mockUserDoc = (mock?: Partial): Partial => ({ + wishlist: mock?.wishlist || [], + isAdmin: mock?.isAdmin || false, + _id: mock?._id || '6079f573062890a5e2cad207', + first_name: mock?.first_name || 'John', + last_name: mock?.last_name || 'Doe', + email: mock?.email || 'john@example.com', + phone: mock?.phone || '9909999099', + photoUrl: mock?.photoUrl || 'https://google.com/john', + address: mock?.address || 'Block C Amsterdam', + description: mock?.description || 'Aspiring Software Developer', + score: mock?.score || 0, + coverPhotoUrl: mock?.coverPhotoUrl || 'https://google.com/john', +}); + +const userArray = [ + mockUser( + [], + false, + '6079f573062890a5e2cad207', + 'John', + 'Doe', + 'john@example.com', + '9909999099', + 'https://google.com/john', + 'Block C Amsterdam', + 'Aspiring Software Developer', + 0, + 'https://google.com/john', + ), +]; + +const userDocArray = [ + mockUserDoc({ + wishlist: [], + isAdmin: false, + _id: '6079f573062890a5e2cad207', + first_name: 'John', + last_name: 'Doe', + email: 'john@example.com', + phone: '9909999099', + photoUrl: 'https://google.com/john', + address: 'Block C Amsterdam', + description: 'Aspiring Software Developer', + score: 0, + coverPhotoUrl: 'https://google.com/john', + }), +]; describe('UserService', () => { let service: UserService; + let model: Model; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -11,7 +95,17 @@ describe('UserService', () => { UserService, { provide: getModelToken('User'), - useValue: {}, + useValue: { + new: jest.fn().mockResolvedValue(mockUser), + constructor: jest.fn().mockResolvedValue(mockUser), + find: jest.fn(), + findOne: jest.fn(), + findById: jest.fn(), + update: jest.fn(), + create: jest.fn(), + remove: jest.fn(), + exec: jest.fn(), + }, }, { provide: getModelToken('Course'), @@ -25,15 +119,118 @@ describe('UserService', () => { }).compile(); service = module.get(UserService); + model = module.get>(getModelToken('User')); }); it('should be defined', () => { expect(service).toBeDefined(); }); + + afterEach(() => { + jest.clearAllMocks(); + }); + describe('Testing userService after mock', () => { //const id = new mongoose.Schema.Types.ObjectId('60bca010d17d463dd09baf9b'); - it('testing get all method', () => { - expect(typeof service.getAllUser).not.toEqual(null); + + it('should return all users', async () => { + jest.spyOn(model, 'find').mockReturnValue({ + exec: jest.fn().mockResolvedValueOnce(userDocArray), + } as any); + const users = await service.getAllUser(); + // console.log(users); + expect(users).toEqual(userArray); + }); + + it('should getOne user by id', async () => { + jest.spyOn(model, 'findById').mockReturnValueOnce( + createMock>({ + exec: jest.fn().mockResolvedValueOnce( + mockUserDoc({ + wishlist: [], + isAdmin: false, + _id: '6079f573062890a5e2cad207', + first_name: 'John', + last_name: 'Doe', + email: 'john@example.com', + phone: '9909999099', + photoUrl: 'https://google.com/john', + address: 'Block C Amsterdam', + description: 'Aspiring Software Developer', + score: 0, + coverPhotoUrl: 'https://google.com/john', + }), + ), + }), + ); + const findMockUser = mockUser( + [], + false, + '6079f573062890a5e2cad207', + 'John', + 'Doe', + 'john@example.com', + '9909999099', + 'https://google.com/john', + 'Block C Amsterdam', + 'Aspiring Software Developer', + 0, + 'https://google.com/john', + ); + const id = new mongoose.Schema.Types.ObjectId('22', 0, 'rtex'); + const foundUser = await service.findUserById(id); + expect(foundUser).toEqual(findMockUser); + }); + + it.skip('should update a User successfully', async () => { + jest.spyOn(model, 'findOneAndUpdate').mockReturnValueOnce( + createMock>({ + exec: jest.fn().mockResolvedValueOnce({ + wishlist: [], + isAdmin: false, + first_name: 'John', + last_name: 'Doe', + email: 'john@example.com', + phone: '9909999099', + photoUrl: 'https://google.com/john', + address: 'Block C Amsterdam', + description: 'Aspiring Software Developer', + score: 0, + coverPhotoUrl: 'https://google.com/john', + }), + }), + ); + const id = new mongoose.Schema.Types.ObjectId('22', 0, 'rtex'); + const updateUserdto: UpdateUserDTO = { + wishlist: [], + isAdmin: false, + first_name: 'John', + last_name: 'Doe', + email: 'john@example.com', + phone: '9909999099', + photoUrl: 'https://google.com/john', + address: 'Block C Amsterdam', + description: 'Aspiring Software Developer', + score: 0, + coverPhotoUrl: 'https://google.com/john', + }; + const updatedUser = await service.updateUser(id, updateUserdto); + expect(updatedUser).toEqual( + mockUser( + [], + false, + '6079f573062890a5e2cad207', + 'John', + 'Doe', + 'john@example.com', + '9909999099', + 'https://google.com/john', + 'Block C Amsterdam', + 'Aspiring Software Developer', + 0, + 'https://google.com/john', + ), + ); }); }); });