|
| 1 | +import { AllLocationListFlat } from '..'; |
| 2 | +import React from 'react'; |
| 3 | +import { store } from '@opensrp/store'; |
| 4 | +import { authenticateUser } from '@onaio/session-reducer'; |
| 5 | +import { QueryClientProvider, QueryClient } from 'react-query'; |
| 6 | +import nock from 'nock'; |
| 7 | +import { render, cleanup, waitForElementToBeRemoved, screen } from '@testing-library/react'; |
| 8 | +import { Router } from 'react-router'; |
| 9 | +import { createBrowserHistory } from 'history'; |
| 10 | +import { flatLocations } from '../../../ducks/tests/fixtures'; |
| 11 | +import { Provider } from 'react-redux'; |
| 12 | +import { RoleContext } from '@opensrp/rbac'; |
| 13 | +import { superUserRole } from '@opensrp/react-utils'; |
| 14 | +import { locationResourceType } from '../../../constants'; |
| 15 | + |
| 16 | +const history = createBrowserHistory(); |
| 17 | + |
| 18 | +const props = { |
| 19 | + fhirBaseURL: 'http://test.server.org', |
| 20 | +}; |
| 21 | + |
| 22 | +jest.mock('fhirclient', () => { |
| 23 | + return jest.requireActual('fhirclient/lib/entry/browser'); |
| 24 | +}); |
| 25 | + |
| 26 | +jest.setTimeout(10000); |
| 27 | + |
| 28 | +const queryClient = new QueryClient({ |
| 29 | + defaultOptions: { |
| 30 | + queries: { |
| 31 | + retry: false, |
| 32 | + cacheTime: 0, |
| 33 | + }, |
| 34 | + }, |
| 35 | +}); |
| 36 | + |
| 37 | +const AppWrapper = (props) => { |
| 38 | + return ( |
| 39 | + <Provider store={store}> |
| 40 | + <RoleContext.Provider value={superUserRole}> |
| 41 | + <Router history={history}> |
| 42 | + <QueryClientProvider client={queryClient}> |
| 43 | + <AllLocationListFlat {...props} /> |
| 44 | + </QueryClientProvider> |
| 45 | + </Router> |
| 46 | + </RoleContext.Provider> |
| 47 | + </Provider> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +describe('location-management/src/components/AllLocationListFlat', () => { |
| 52 | + beforeAll(() => { |
| 53 | + nock.disableNetConnect(); |
| 54 | + store.dispatch( |
| 55 | + authenticateUser( |
| 56 | + true, |
| 57 | + { |
| 58 | + |
| 59 | + name: 'Bobbie', |
| 60 | + username: 'RobertBaratheon', |
| 61 | + }, |
| 62 | + { api_token: 'hunter2', oAuth2Data: { access_token: 'hunter2', state: 'abcde' } } |
| 63 | + ) |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + afterEach(() => { |
| 68 | + nock.cleanAll(); |
| 69 | + cleanup(); |
| 70 | + }); |
| 71 | + |
| 72 | + afterAll(() => { |
| 73 | + nock.enableNetConnect(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('shows broken page', async () => { |
| 77 | + // __summary: 'count' |
| 78 | + nock(props.fhirBaseURL) |
| 79 | + .get(`/${locationResourceType}/_search`) |
| 80 | + .query({ |
| 81 | + _total: 'accurate', |
| 82 | + _include: 'Location:partof', |
| 83 | + _getpagesoffset: 0, |
| 84 | + _count: 20, |
| 85 | + }) |
| 86 | + .replyWithError({ |
| 87 | + message: 'something awful happened', |
| 88 | + code: 'AWFUL_ERROR', |
| 89 | + }); |
| 90 | + |
| 91 | + render(<AppWrapper {...props} />); |
| 92 | + await waitForElementToBeRemoved(document.querySelector('.ant-spin')); |
| 93 | + expect(screen.getByText(/failed, reason: something awful happened/)).toBeInTheDocument(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('shows no data page', async () => { |
| 97 | + // __summary: 'count' |
| 98 | + nock(props.fhirBaseURL) |
| 99 | + .get(`/${locationResourceType}/_search`) |
| 100 | + .query({ |
| 101 | + _total: 'accurate', |
| 102 | + _include: 'Location:partof', |
| 103 | + _getpagesoffset: 0, |
| 104 | + _count: 20, |
| 105 | + }) |
| 106 | + .reply(200, { total: 0 }); |
| 107 | + |
| 108 | + render(<AppWrapper {...props} />); |
| 109 | + await waitForElementToBeRemoved(document.querySelector('.ant-spin')); |
| 110 | + expect(screen.getByText(/All Locations Flat/)).toBeInTheDocument(); |
| 111 | + expect(screen.getByTitle(/Empty raw svg icon/)).toBeInTheDocument(); |
| 112 | + expect( |
| 113 | + screen.getByText(/No data available to display, you can start adding data now/) |
| 114 | + ).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('Show data as expected', async () => { |
| 118 | + // __summary: 'count' |
| 119 | + nock(props.fhirBaseURL) |
| 120 | + .get(`/${locationResourceType}/_search`) |
| 121 | + .query({ |
| 122 | + _total: 'accurate', |
| 123 | + _include: 'Location:partof', |
| 124 | + _getpagesoffset: 0, |
| 125 | + _count: 20, |
| 126 | + }) |
| 127 | + .reply(200, flatLocations); |
| 128 | + |
| 129 | + render(<AppWrapper {...props} />); |
| 130 | + await waitForElementToBeRemoved(document.querySelector('.ant-spin')); |
| 131 | + expect(screen.getByText(/All Locations Flat/)).toBeInTheDocument(); |
| 132 | + |
| 133 | + // check table contnets |
| 134 | + const table = document.querySelector('table'); |
| 135 | + // check table headers |
| 136 | + expect(table?.querySelectorAll('thead tr')).toHaveLength(1); |
| 137 | + const header = table?.querySelectorAll('thead tr'); |
| 138 | + header?.forEach((td) => { |
| 139 | + expect(td.textContent).toMatchSnapshot('table header'); |
| 140 | + }); |
| 141 | + // check table body |
| 142 | + expect(table?.querySelectorAll('tbody tr')).toHaveLength(1); |
| 143 | + const firstRowTd = table?.querySelectorAll('tbody tr:nth-child(1) td'); |
| 144 | + firstRowTd?.forEach((td) => { |
| 145 | + expect(td.textContent).toMatchSnapshot('table data'); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments