import { Observable } from 'rxjs' ; import { QuestionService , BulkService } from 'shared-library/core/services' ; import { TestBed , async } from '@angular/core/testing' ; import * as BulkActions from '../actions' ; import { provideMockActions } from '@ngrx/effects/testing' ; import { Actions } from '@ngrx/effects' ; import { hot , cold } from 'jest-marbles' ; import { testData } from 'test/data' ; import { User , Question , BulkUploadFileInfo , BulkUpload } from 'shared-library/shared/model' ; import { BulkEffects } from './bulk.effects' ; import { StoreModule , MemoizedSelector , Store } from '@ngrx/store' ; import { provideMockStore , MockStore } from '@ngrx/store/testing' ; import { coreState , CoreState } from 'shared-library/core/store' ; describe ( 'BulkEffects:' , ( ) => { let effects ; let actions$ ; let questionService ; let bulkService ; let mockStore ; let mockCoreSelector ; const user = testData . userList [ 0 ] ; const bulkUploadFileInfo = new BulkUploadFileInfo ( ) ; const bulkUploadId = 'cce85s8e52x4ex' ; bulkUploadFileInfo . id = bulkUploadId ; bulkUploadFileInfo . categoryId = 1 ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { imports : [ StoreModule . forRoot ( { } ) ] , providers : [ { provide : QuestionService , useValue : { } } , { provide : BulkService , useValue : { } } , BulkEffects , provideMockStore ( { initialState : { 'core' : { user } } , selectors : [ { selector : coreState , value : { user } } ] } ) , provideMockActions ( ( ) => actions$ ) , ] , } ) ; mockStore = TestBed . get ( Store ) ; mockCoreSelector = mockStore . overrideSelector < CoreState , Partial < CoreState > > ( coreState , { user } ) ; effects = TestBed . get ( BulkEffects ) ; questionService = TestBed . get ( QuestionService ) ; bulkService = TestBed . get ( BulkService ) ; actions$ = TestBed . get ( Actions ) ; mockStore . refreshState ( ) ; } ) ) ; it ( 'loadBulkUpload' , ( ) => { const archive = true ; const completion = new BulkActions . LoadBulkUploadSuccess ( [ bulkUploadFileInfo ] ) ; const action = new BulkActions . LoadBulkUpload ( { user , archive } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : [ bulkUploadFileInfo ] } ) ; const expected = cold ( '--b' , { b : completion } ) ; bulkService . getBulkUpload = jest . fn ( ( ) => response ) ; expect ( effects . loadBulkUpload$ ) . toBeObservable ( expected ) ; } ) ; it ( 'LoadUserBulkUpload' , ( ) => { const archive = true ; const completion = new BulkActions . LoadUserBulkUploadSuccess ( [ bulkUploadFileInfo ] ) ; const action = new BulkActions . LoadUserBulkUpload ( { user , archive } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : [ bulkUploadFileInfo ] } ) ; const expected = cold ( '--b' , { b : completion } ) ; bulkService . getUserBulkUpload = jest . fn ( ( ) => response ) ; expect ( effects . loadUserBulkUpload$ ) . toBeObservable ( expected ) ; } ) ; it ( 'loadBulkUploadPublishedQuestions' , ( ) => { const questions = testData . questions . published ; const completion = new BulkActions . LoadBulkUploadPublishedQuestionsSuccess ( questions ) ; const action = new BulkActions . LoadBulkUploadPublishedQuestions ( { bulkUploadFileInfo } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : questions } ) ; const expected = cold ( '--b' , { b : completion } ) ; questionService . getQuestionsForBulkUpload = jest . fn ( ( ) => response ) ; expect ( effects . loadBulkUploadPublishedQuestions$ ) . toBeObservable ( expected ) ; } ) ; it ( 'addBulkQuestions' , ( ) => { const bulkUpload = new BulkUpload ( ) ; const action = new BulkActions . AddBulkQuestions ( { bulkUpload } ) ; actions$ = hot ( '--a-' , { a : action } ) ; const expected = cold ( '--#' ) ; expect ( effects . addBulkQuestions$ ) . toBeObservable ( expected ) ; } ) ; it ( 'updateBulkUpload' , ( ) => { const action = new BulkActions . UpdateBulkUpload ( { bulkUploadFileInfo } ) ; actions$ = hot ( '--a-' , { a : action } ) ; const expected = cold ( '--#' ) ; expect ( effects . updateBulkUpload$ ) . toBeObservable ( expected ) ; } ) ; it ( 'loadBulkUploadUnpublishedQuestions' , ( ) => { const questions = testData . questions . unpublished ; const completion = new BulkActions . LoadBulkUploadUnpublishedQuestionsSuccess ( questions ) ; const action = new BulkActions . LoadBulkUploadUnpublishedQuestions ( { bulkUploadFileInfo } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : questions } ) ; const expected = cold ( '--b' , { b : completion } ) ; questionService . getQuestionsForBulkUpload = jest . fn ( ( ) => response ) ; expect ( effects . loadBulkUploadUnpublishedQuestions$ ) . toBeObservable ( expected ) ; } ) ; it ( 'loadBulkUploadFileUrl' , ( ) => { const url = 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg' ; const completion = new BulkActions . LoadBulkUploadFileUrlSuccess ( url ) ; const action = new BulkActions . LoadBulkUploadFileUrl ( { bulkUploadFileInfo } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : url } ) ; const expected = cold ( '--b' , { b : completion } ) ; bulkService . getFileByBulkUploadFileUrl = jest . fn ( ( ) => response ) ; expect ( effects . loadBulkUploadFileUrl$ ) . toBeObservable ( expected ) ; } ) ; it ( 'archiveUserBulkUpload' , ( ) => { const completion = new BulkActions . ArchiveBulkUploadSuccess ( ) ; const action = new BulkActions . ArchiveBulkUpload ( { archiveArray : [ bulkUploadFileInfo ] , user } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : null } ) ; const expected = cold ( '--b' , { b : completion } ) ; bulkService . archiveBulkUpload = jest . fn ( ( ) => response ) ; expect ( effects . archiveUserBulkUpload$ ) . toBeObservable ( expected ) ; } ) ; it ( 'getBulkUpload' , ( ) => { const bulkId = '25145895252_bulk_id' ; const completion = new BulkActions . LoadBulkUploadFileSuccess ( bulkUploadFileInfo ) ; const action = new BulkActions . LoadBulkUploadFile ( { bulkId } ) ; actions$ = hot ( '-a---' , { a : action } ) ; const response = cold ( '-a|' , { a : bulkUploadFileInfo } ) ; const expected = cold ( '--b' , { b : completion } ) ; bulkService . getBulkUploadFile = jest . fn ( ( ) => response ) ; expect ( effects . getBulkUpload$ ) . toBeObservable ( expected ) ; } ) ; } ) ;