import { Test , TestingModule } from '@nestjs/testing' ; import { ArticleService } from './article.service' ; import { Neo4jModule , Neo4jService } from 'nest-neo4j/dist' ; import { User } from '../user/entity/user.entity' ; import { int } from 'neo4j-driver' ; import { Node } from 'neo4j-driver/lib/graph-types' ; import { Result } from 'neo4j-driver/lib/result' ; jest . mock ( 'neo4j-driver/lib/driver' ) ; import { mockNode , mockResult } from 'nest-neo4j/dist/test' ; describe ( 'ArticleService' , ( ) => { let service ; let neo4jService ; beforeEach ( async ( ) => { const module = await Test . createTestingModule ( { imports : [ Neo4jModule . forRoot ( { scheme : 'neo4j' , host : 'localhost' , port : 7687 , username : 'neo4j' , password : 'neox' } ) ] , providers : [ ArticleService ] , } ) . compile ( ) ; service = await module . resolve < ArticleService > ( ArticleService ) ; neo4jService = module . get ( Neo4jService ) ; } ) ; describe ( '::create()' , ( ) => { it ( 'should create a new article' , async ( ) => { expect ( service ) . toBeDefined ( ) ; expect ( neo4jService ) . toBeDefined ( ) ; const data = { title : 'title' , description : 'description' , body : 'body' , tagList : [ 'tag1' , 'tag2' ] } ; const favoritesCount = 100 ; const favorited = false ; const user = new User ( mockNode ( 'User' , { id : 'test-user' } ) ) ; Object . defineProperty ( service , 'request' , { value : { user } } ) ; const write = jest . spyOn ( neo4jService , 'write' ) . mockResolvedValue ( mockResult ( [ { u : user , a : mockNode ( 'Article' , { ... data , id : 'test-article-1' } ) , tagList : data . tagList . map ( name => mockNode ( 'Tag' , { name } ) ) , favoritesCount , favorited , } , ] ) ) ; const article = await service . create ( data . title , data . description , data . body , data . tagList ) ; const json = article . toJson ( ) ; expect ( json ) . toEqual ( { ... data , author : user . toJson ( ) , id : 'test-article-1' , favorited , favoritesCount , } ) ; } ) ; } ) ; } ) ;