import { Test , TestingModule } from '@nestjs/testing' ; import { TodoService } from './todo.service' ; import { TodoRepository } from '../db/repositories/TodoRepository' ; import { UserRepository } from '../db/repositories/UserRepository' ; import { MockTodoRepository } from '../db/mock/MockTodoRepository' ; import { MockUserRepository } from '../db/mock/MockUserRepository' ; import createMockDB from '../db/mock/db' ; import { TodoStatus } from './entities/todo.entity' ; const { mockTodos , mockUsers } = createMockDB ( ) ; describe ( 'TodoService' , ( ) => { let mockTodoRepository ; let mockUserRepository ; let service ; beforeEach ( async ( ) => { mockUserRepository = new MockUserRepository ( mockUsers ) ; mockTodoRepository = new MockTodoRepository ( mockTodos ) ; const module = await Test . createTestingModule ( { providers : [ TodoService , { provide : TodoRepository , useValue : mockTodoRepository , } , { provide : UserRepository , useValue : mockUserRepository , } , ] , } ) . compile ( ) ; service = module . get < TodoService > ( TodoService ) ; } ) ; it ( 'create' , async ( ) => { expect ( service ) . toBeDefined ( ) ; const returnTodos = await service . create ( 99 , { title : 'title99' , description : 'desc99' , status : TodoStatus . TODO , } ) ; expect ( returnTodos . title ) . toEqual ( 'title99' ) ; expect ( returnTodos . description ) . toEqual ( 'desc99' ) ; expect ( returnTodos . status ) . toEqual ( TodoStatus . TODO ) ; } ) ; it ( 'findAll' , async ( ) => { expect ( service ) . toBeDefined ( ) ; const returnTodos = await service . findAll ( ) ; expect ( returnTodos ) . toEqual ( mockTodos ) ; } ) ; it ( 'findAllByUserId' , async ( ) => { expect ( service ) . toBeDefined ( ) ; jest . spyOn ( mockUserRepository , 'findOne' ) . mockImplementation ( async ( ) => { return mockUsers [ 0 ] ; } ) ; const returnTodos = await service . findAllByUserId ( 0 ) ; const [ firstTodo ] = returnTodos ; expect ( mockUserRepository . findOne ) . toBeCalled ( ) ; expect ( firstTodo . id ) . toEqual ( 0 ) ; expect ( firstTodo . title ) . toEqual ( 'todo1' ) ; expect ( firstTodo . description ) . toEqual ( 'desc1' ) ; } ) ; it ( 'findOne' , async ( ) => { expect ( service ) . toBeDefined ( ) ; const returnTodo = await service . findOne ( 0 ) ; expect ( returnTodo . id ) . toEqual ( 0 ) ; expect ( returnTodo . title ) . toEqual ( 'todo1' ) ; expect ( returnTodo . description ) . toEqual ( 'desc1' ) ; } ) ; it ( 'update' , async ( ) => { expect ( service ) . toBeDefined ( ) ; const allTodos = await service . findAll ( ) ; await service . update ( 0 , { title : 'todo99' , description : 'desc99' , } ) ; const targetTodo = allTodos . find ( ( todo ) => todo . id === 0 ) ; expect ( targetTodo . id ) . toEqual ( 0 ) ; expect ( targetTodo . title ) . toEqual ( 'todo99' ) ; expect ( targetTodo . description ) . toEqual ( 'desc99' ) ; } ) ; it ( 'remote' , async ( ) => { expect ( service ) . toBeDefined ( ) ; await service . remove ( 0 ) ; const allTodos = await service . findAll ( ) ; expect ( allTodos . length ) . toEqual ( 1 ) ; expect ( allTodos . find ( ( todo ) => todo . id === 0 ) ) . toBeUndefined ( ) ; } ) ; } ) ;