import { Test , TestingModule } from '@nestjs/testing' ; import { AbilityScore , AbilityScoreInput , AbilityScoreUpdate , } from '@tabletop-companion/api-interface' ; import { of } from 'rxjs' ; import { DatabaseService } from '../database/database.service' ; import { AbilityScoreService } from './ability-score.service' ; const charId = 'CHR-TEST' ; const abilityScores = [ { id : 'ABL-TEST1' , name : 'Strength' , value : 10 , characterId : charId , } , { id : 'ABL-TEST2' , name : 'Dexterity' , value : 12 , characterId : charId , } , { id : 'ABL-TEST3' , name : 'Constitution' , value : 8 , characterId : charId , } , { id : 'ABL-TEST4' , name : 'Intelligence' , value : 15 , characterId : charId , } , { id : 'ABL-TEST5' , name : 'Wisdom' , value : 18 , characterId : charId , } , { id : 'ABL-TEST6' , name : 'Charisma' , value : 16 , characterId : charId , } , ] ; const abilityScore = { id : 'ABL-TEST1' , name : 'Strength' , value : 10 , characterId : charId , } ; const abilityInsertReturn = { id : 'ABL-TEST1' , } ; const abilityScoreInput = { name : 'Strength' , value : 12 , characterId : charId , } ; const abilityScoreUpdate = { value : 10 , id : 'ABL-TEST1' , } ; const abilityScoresUpdate = [ { value : 10 , id : 'ABL-TEST1' , } , { value : 12 , id : 'ABL-TEST2' , } , { value : 16 , id : 'ABL-TEST4' , } , ] ; const abilityScoreObserver = ( done ) => ( { next ( value ) { expect ( value ) . toEqual ( abilityScore ) ; } , error ( error ) { throw error ; } , complete ( ) { done ( ) ; } , } ) ; const abilityScoresObserver = ( done ) => ( { next ( value ) { expect ( value ) . toEqual ( abilityScores ) ; } , error ( error ) { throw error ; } , complete ( ) { done ( ) ; } , } ) ; describe ( 'AbilityScoreService' , ( ) => { let service ; let db ; beforeEach ( async ( ) => { const module = await Test . createTestingModule ( { providers : [ AbilityScoreService , { provide : DatabaseService , useValue : { query : jest . fn ( ) , insert : jest . fn ( ) , update : jest . fn ( ) , updateMany : jest . fn ( ) , } , } , ] , } ) . compile ( ) ; service = module . get < AbilityScoreService > ( AbilityScoreService ) ; db = module . get < DatabaseService < AbilityScore > > ( DatabaseService ) ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; it ( 'should get the ability scores for one character' , ( done ) => { db . query = jest . fn ( ) . mockReturnValueOnce ( of ( abilityScores ) ) ; service . getAbilityScoresByCharId ( { id : charId } ) . subscribe ( abilityScoresObserver ( done ) ) . unsubscribe ( ) ; } ) ; it ( 'should get one ability score' , ( done ) => { db . query = jest . fn ( ) . mockReturnValueOnce ( of ( [ abilityScore ] ) ) ; service . getAbilityScoreById ( { id : 'ABL-TEST1' } ) . subscribe ( abilityScoreObserver ( done ) ) . unsubscribe ( ) ; } ) ; it ( 'should insert one ability score' , ( done ) => { db . insert = jest . fn ( ) . mockReturnValueOnce ( of ( [ abilityInsertReturn ] ) ) ; service . insertOneAbilityScore ( abilityScoreInput ) . subscribe ( { next ( abScore ) { expect ( abScore ) . toEqual ( { id : 'ABL-TEST1' , ... abilityScoreInput } ) ; } , error ( error ) { throw error ; } , complete ( ) { done ( ) ; } , } ) . unsubscribe ( ) ; } ) ; it ( 'should insert multiple ability scores' , ( done ) => { db . insert = jest . fn ( ) . mockReturnValueOnce ( of ( [ { id : 'ABL-TEST1' } , { id : 'ABL-TEST2' } , { id : 'ABL-TEST3' } ] ) ) ; service . insertManyAbilityScores ( [ abilityScoreInput , abilityScoreInput , abilityScoreInput , ] ) . subscribe ( { next ( abScores ) { expect ( abScores . length ) . toBe ( 3 ) ; expect ( abScores ) . toEqual ( [ { id : 'ABL-TEST1' , ... abilityScoreInput , } , { id : 'ABL-TEST2' , ... abilityScoreInput , } , { id : 'ABL-TEST3' , ... abilityScoreInput , } , ] ) ; } , error ( error ) { throw error ; } , complete ( ) { done ( ) ; } , } ) . unsubscribe ( ) ; } ) ; it ( 'should update one ability score' , ( done ) => { db . update = jest . fn ( ) . mockReturnValueOnce ( of ( [ abilityInsertReturn ] ) ) ; db . query = jest . fn ( ) . mockReturnValueOnce ( of ( [ abilityScore ] ) ) ; service . updateOneAbilityScore ( abilityScoreUpdate ) . subscribe ( abilityScoreObserver ( done ) ) . unsubscribe ( ) ; } ) ; it ( 'should update multiple ability scores' , ( done ) => { db . updateMany = jest . fn ( ) . mockReturnValueOnce ( of ( [ 'ABL-TEST1' , 'ABL-TEST2' , 'ABL-TEST4' ] ) ) ; db . query = jest . fn ( ) . mockReturnValueOnce ( of ( [ { id : 'ABL-TEST1' , value : 10 , name : 'Strength' , characterId : 'CHR-TEST1' , } , { id : 'ABL-TEST2' , value : 12 , name : 'Dexterity' , characterId : 'CHR-TEST1' , } , { id : 'ABL-TEST4' , name : 'Intelligence' , value : 16 , characterId : 'CHR-TEST1' , } , ] ) ) ; service . updateManyAbilityScores ( abilityScoresUpdate ) . subscribe ( { next ( scores ) { expect ( scores . length ) . toBe ( 3 ) ; expect ( scores ) . toContainEqual ( { id : 'ABL-TEST1' , value : 10 , name : 'Strength' , characterId : 'CHR-TEST1' , } ) ; } , error ( error ) { throw error ; } , complete ( ) { done ( ) ; } , } ) . unsubscribe ( ) ; } ) ; } ) ;