import { checkAuth } from '@client/profile/profileActions' ; import { AppStore , createStore } from '@client/store' ; import { createTestComponent , mockUserResponse , resizeWindow } from '@client/tests/util' ; import { waitForElement } from '@client/tests/wait-for-element' ; import { GridTable } from '@opencrvs/components/lib/interface' ; import { readFileSync } from 'fs' ; import * as jwt from 'jsonwebtoken' ; import { merge } from 'lodash' ; import moment from 'moment' ; import * as React from 'react' ; import { ApprovalTab } from './approvalTab' ; import { GQLBirthEventSearchSet , GQLDeathEventSearchSet } from '@opencrvs/gateway/src/graphql/schema' ; import { formattedDuration } from '@client/utils/date-formatting' ; const validateScopeToken = jwt . sign ( { scope : [ 'validate' ] } , readFileSync ( '../auth/test/cert.key' ) , { algorithm : 'RS256' , issuer : 'opencrvs:auth-service' , audience : 'opencrvs:gateway-user' } ) ; const nameObj = { data : { getUser : { name : [ { use : 'en' , firstNames : 'Mohammad' , familyName : 'Ashraful' , __typename : 'HumanName' } , { use : 'bn' , firstNames : '' , familyName : '' , __typename : 'HumanName' } ] , role : 'DISTRICT_REGISTRAR' } } } ; const mockUserData = { id : 'e302f7c5-ad87-4117-91c1-35eaf2ea7be8' , type : 'Birth' , registration : { status : 'DECLARED' , contactNumber : '01622688231' , trackingId : 'BW0UTHR' , registrationNumber : null , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , duplicates : null , createdAt : '2018-05-23T14:44:58+02:00' , modifiedAt : '2018-05-23T14:44:58+02:00' } , dateOfBirth : '2010-10-10' , childName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] , child : { id : 'FAKE_ID' , name : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] , birthDate : '2010-10-10' } , deceased : { name : [ { use : '' , firstNames : '' , familyName : '' } ] , deceased : { deathDate : '' } } , informant : { individual : { telecom : [ { system : '' , use : '' , value : '' } ] } } , dateOfDeath : null , deceasedName : null , createdAt : '2018-05-23T14:44:58+02:00' } ; const userData = [ ] ; for ( let i = 0 ; i < 14 ; i ++ ) { userData . push ( mockUserData ) ; } merge ( mockUserResponse , nameObj ) ; const getItem = window . localStorage . getItem as jest . Mock ; describe ( 'RegistrationHome sent for approval tab related tests' , ( ) => { let store ; beforeEach ( async ( ) => { store = createStore ( ) . store ; getItem . mockReturnValue ( validateScopeToken ) ; await store . dispatch ( checkAuth ( { '?token' : validateScopeToken } ) ) ; } ) ; it ( 'renders all items returned from graphql query in sent for approval' , async ( ) => { const TIME_STAMP = '1544188309380' ; Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab registrarLocationId = { '2a83cf14-b959-47f4-8097-f75a75d1867f' } queryData = { { data : { totalItems : 2 , results : [ { id : 'e302f7c5-ad87-4117-91c1-35eaf2ea7be8' , type : 'Birth' , registration : { status : 'VALIDATED' , contactNumber : '01622688231' , trackingId : 'BW0UTHR' , registrationNumber : undefined , eventLocationId : undefined , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , duplicates : [ null ] , createdAt : TIME_STAMP , modifiedAt : TIME_STAMP } , dateOfBirth : '2010-10-10' , childName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLBirthEventSearchSet , { id : 'bc09200d-0160-43b4-9e2b-5b9e90424e95' , type : 'Death' , registration : { status : 'VALIDATED' , trackingId : 'DW0UTHR' , registrationNumber : undefined , contactNumber : undefined , eventLocationId : undefined , duplicates : [ '308c35b4-04f8-4664-83f5-9790e790cd33' ] , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , createdAt : TIME_STAMP , modifiedAt : undefined } , dateOfDeath : '2007-01-01' , deceasedName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLDeathEventSearchSet ] } } } />, store); testComponent . component . update ( ) ; const data = testComponent . component . find ( GridTable ) . prop ( 'content' ) ; const EXPECTED_DATE_OF_APPLICATION = formattedDuration ( moment ( moment ( TIME_STAMP , 'x' ) . format ( 'YYYY-MM-DD HH:mm:ss' ) , 'YYYY-MM-DD HH:mm:ss' ) ) ; expect ( data . length ) . toBe ( 2 ) ; expect ( data [ 0 ] . id ) . toBe ( 'e302f7c5-ad87-4117-91c1-35eaf2ea7be8' ) ; expect ( data [ 0 ] . eventTimeElapsed ) . toBe ( '8 years ago' ) ; expect ( data [ 0 ] . dateOfApproval ) . toBe ( EXPECTED_DATE_OF_APPLICATION ) ; expect ( data [ 0 ] . name ) . toBe ( 'Iliyas Khan' ) ; expect ( data [ 0 ] . trackingId ) . toBe ( 'BW0UTHR' ) ; expect ( data [ 0 ] . event ) . toBe ( 'Birth' ) ; expect ( data [ 0 ] . actions ) . toBeUndefined ( ) ; } ) ; it ( 'returns an empty array incase of invalid graphql query response' , async ( ) => { Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab queryData = { { data : { totalItems : 2 , results : [ ] } } } />, store); const data = ( await waitForElement ( testComponent . component , GridTable ) ) . prop ( 'content' ) ; expect ( data . length ) . toBe ( 0 ) ; } ) ; it ( 'should show pagination bar if pagination is used and items more than 11 in Approval Tab' , async ( ) => { Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab registrarLocationId = { '2a83cf14-b959-47f4-8097-f75a75d1867f' } queryData = { { data : { totalItems : 14 , results : [ ] } } } showPaginated = { true } / > , store ) ; expect ( testComponent . component . find ( '#pagination' ) . hostNodes ( ) ) . toHaveLength ( 1 ) ; testComponent . component . find ( '#pagination button' ) . last ( ) . hostNodes ( ) . simulate ( 'click' ) ; } ) ; it ( 'should show loadmore button if loadmore is used and items more than 11 in Approval Tab' , async ( ) => { Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab registrarLocationId = { '2a83cf14-b959-47f4-8097-f75a75d1867f' } queryData = { { data : { totalItems : 14 , results : [ ] } } } showPaginated = { false } / > , store ) ; expect ( testComponent . component . find ( '#load_more_button' ) . hostNodes ( ) ) . toHaveLength ( 1 ) ; testComponent . component . find ( '#load_more_button' ) . last ( ) . hostNodes ( ) . simulate ( 'click' ) ; } ) ; it ( 'renders expanded area for validated status' , async ( ) => { Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab queryData = { { data : { totalItems : 2 , results : [ { id : 'e302f7c5-ad87-4117-91c1-35eaf2ea7be8' , type : 'Birth' , registration : { status : 'VALIDATED' , contactNumber : '01622688231' , trackingId : 'BW0UTHR' , eventLocationId : undefined , registrationNumber : undefined , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , duplicates : [ null ] , createdAt : '2018-05-23T14:44:58+02:00' , modifiedAt : '2018-05-23T14:44:58+02:00' } , dateOfBirth : '2010-10-10' , childName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLBirthEventSearchSet , { id : 'bc09200d-0160-43b4-9e2b-5b9e90424e95' , type : 'Death' , registration : { status : 'VALIDATED' , trackingId : 'DW0UTHR' , registrationNumber : undefined , eventLocationId : undefined , contactNumber : undefined , duplicates : [ '308c35b4-04f8-4664-83f5-9790e790cd33' ] , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , createdAt : '2007-01-01' , modifiedAt : '2007-01-01' } , operationHistories : [ { operationType : 'VALIDATED' , operatedOn : '2019-12-12T15:23:21.280Z' , operatorRole : 'REGISTRATION_AGENT' , operatorName : [ { firstNames : 'Tamim' , familyName : 'Iqbal' , use : 'en' } , { firstNames : '' , familyName : null , use : 'bn' } ] , operatorOfficeName : 'Alokbali Union Parishad' , operatorOfficeAlias : [ 'আলোকবালী  ইউনিয়ন পরিষদ' ] } ] , dateOfDeath : '2007-01-01' , deceasedName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLDeathEventSearchSet ] } } } />, store); await new Promise ( ( resolve ) => { setTimeout ( resolve , 200 ) ; } ) ; testComponent . component . update ( ) ; const instance = testComponent . component . find ( GridTable ) . instance ( ) as any ; instance . toggleExpanded ( 'bc09200d-0160-43b4-9e2b-5b9e90424e95' ) ; await new Promise ( ( resolve ) => { setTimeout ( resolve , 100 ) ; } ) ; testComponent . component . update ( ) ; expect ( testComponent . component . find ( '#VALIDATED-0' ) . hostNodes ( ) . length ) . toBe ( 1 ) ; } ) ; } ) ; describe ( 'Tablet tests' , ( ) => { const { store } = createStore ( ) ; beforeAll ( async ( ) => { getItem . mockReturnValue ( validateScopeToken ) ; await store . dispatch ( checkAuth ( { '?token' : validateScopeToken } ) ) ; resizeWindow ( 800 , 1280 ) ; } ) ; afterEach ( ( ) => { resizeWindow ( 1024 , 768 ) ; } ) ; it ( 'redirects to detail page if item is clicked' , async ( ) => { jest . clearAllMocks ( ) ; const TIME_STAMP = '1544188309380' ; Date . now = jest . fn ( ( ) => 1554055200000 ) ; const testComponent = await createTestComponent ( < ApprovalTab queryData = { { data : { totalItems : 2 , results : [ { id : 'e302f7c5-ad87-4117-91c1-35eaf2ea7be8' , type : 'Birth' , registration : { status : 'VALIDATED' , contactNumber : '01622688231' , trackingId : 'BW0UTHR' , eventLocationId : undefined , registrationNumber : undefined , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , duplicates : [ null ] , createdAt : TIME_STAMP , modifiedAt : TIME_STAMP } , dateOfBirth : '2010-10-10' , childName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLBirthEventSearchSet , { id : 'bc09200d-0160-43b4-9e2b-5b9e90424e95' , type : 'Death' , registration : { status : 'VALIDATED' , trackingId : 'DW0UTHR' , registrationNumber : undefined , eventLocationId : undefined , contactNumber : undefined , duplicates : [ '308c35b4-04f8-4664-83f5-9790e790cd33' ] , registeredLocationId : '308c35b4-04f8-4664-83f5-9790e790cde1' , createdAt : TIME_STAMP , modifiedAt : undefined } , dateOfDeath : '2007-01-01' , deceasedName : [ { firstNames : 'Iliyas' , familyName : 'Khan' , use : 'en' } , { firstNames : 'ইলিয়াস' , familyName : 'খান' , use : 'bn' } ] } as GQLDeathEventSearchSet ] } } } />, store); testComponent . component . update ( ) ; const element = await waitForElement ( testComponent . component , '#row_0' ) ; element . hostNodes ( ) . simulate ( 'click' ) ; await new Promise ( ( resolve ) => { setTimeout ( resolve , 100 ) ; } ) ; testComponent . component . update ( ) ; expect ( window . location . href ) . toContain ( '/details/e302f7c5-ad87-4117-91c1-35eaf2ea7be8' ) ; } ) ; } ) ;