Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

fix: standardize mock store setup in tests #2075

Merged
merged 5 commits into from May 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/__tests__/HospitalRun.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router-dom'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import { Toaster } from '@hospitalrun/components'
import { act } from 'react-dom/test-utils'
import Dashboard from 'dashboard/Dashboard'
Expand All @@ -15,8 +15,9 @@ import Appointments from 'scheduling/appointments/Appointments'
import HospitalRun from '../HospitalRun'
import Permissions from '../model/Permissions'
import Incidents from '../incidents/Incidents'
import { RootState } from '../store'

const mockStore = configureMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('HospitalRun', () => {
describe('routing', () => {
Expand All @@ -29,7 +30,7 @@ describe('HospitalRun', () => {
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
incidents: { incidents: [] },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('HospitalRun', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
} as any)}
>
<MemoryRouter initialEntries={['/appointments']}>
<HospitalRun />
Expand All @@ -82,7 +83,7 @@ describe('HospitalRun', () => {
labs: { labs: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

let wrapper: any
await act(async () => {
Expand All @@ -106,7 +107,7 @@ describe('HospitalRun', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand All @@ -129,7 +130,7 @@ describe('HospitalRun', () => {
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
incidents: { incidents: [] },
})
} as any)

let wrapper: any
await act(async () => {
Expand All @@ -153,7 +154,7 @@ describe('HospitalRun', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand All @@ -178,7 +179,7 @@ describe('HospitalRun', () => {
user: { permissions: [Permissions.WritePatients] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
} as any)}
>
<MemoryRouter initialEntries={['/']}>
<HospitalRun />
Expand Down
8 changes: 5 additions & 3 deletions src/__tests__/breadcrumbs/Breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import { Provider } from 'react-redux'
import { mount } from 'enzyme'
import { createMemoryHistory } from 'history'
import { Router } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import {
Breadcrumb as HRBreadcrumb,
BreadcrumbItem as HRBreadcrumbItem,
} from '@hospitalrun/components'

import Breadcrumbs from 'breadcrumbs/Breadcrumbs'
import Breadcrumb from 'model/Breadcrumb'
import thunk from 'redux-thunk'
import { RootState } from '../../store'

const mockStore = configureMockStore()
const mockStore = createMockStore<RootState, any>([thunk])

describe('Breadcrumbs', () => {
const setup = (breadcrumbs: Breadcrumb[]) => {
const history = createMemoryHistory()
const store = mockStore({
breadcrumbs: { breadcrumbs },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand Down
12 changes: 7 additions & 5 deletions src/__tests__/breadcrumbs/useAddBreadcrumbs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React from 'react'
import { renderHook } from '@testing-library/react-hooks'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import useAddBreadcrumbs from '../../breadcrumbs/useAddBreadcrumbs'
import * as breadcrumbsSlice from '../../breadcrumbs/breadcrumbs-slice'
import { RootState } from '../../store'

const store = configureMockStore()
const mockStore = createMockStore<RootState, any>([thunk])

describe('useAddBreadcrumbs', () => {
beforeEach(() => jest.clearAllMocks())

it('should call addBreadcrumbs with the correct data', () => {
const wrapper = ({ children }: any) => <Provider store={store({})}>{children}</Provider>
const wrapper = ({ children }: any) => <Provider store={mockStore({})}>{children}</Provider>

jest.spyOn(breadcrumbsSlice, 'addBreadcrumbs')
const breadcrumbs = [
Expand All @@ -27,7 +29,7 @@ describe('useAddBreadcrumbs', () => {
})

it('should call addBreadcrumbs with an additional dashboard breadcrumb', () => {
const wrapper = ({ children }: any) => <Provider store={store({})}>{children}</Provider>
const wrapper = ({ children }: any) => <Provider store={mockStore({})}>{children}</Provider>

jest.spyOn(breadcrumbsSlice, 'addBreadcrumbs')
const breadcrumbs = [
Expand All @@ -46,7 +48,7 @@ describe('useAddBreadcrumbs', () => {
})

it('should call removeBreadcrumbs with the correct data after unmount', () => {
const wrapper = ({ children }: any) => <Provider store={store({})}>{children}</Provider>
const wrapper = ({ children }: any) => <Provider store={mockStore({})}>{children}</Provider>

jest.spyOn(breadcrumbsSlice, 'addBreadcrumbs')
jest.spyOn(breadcrumbsSlice, 'removeBreadcrumbs')
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import Sidebar from 'components/Sidebar'
import { Router } from 'react-router-dom'
import { ListItem } from '@hospitalrun/components'
import { act } from '@testing-library/react'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { Provider } from 'react-redux'
import { RootState } from '../../store'

const mockStore = configureMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('Sidebar', () => {
let history = createMemoryHistory()
const store = mockStore({
components: { sidebarCollapsed: false },
})
} as any)
const setup = (location: string) => {
history = createMemoryHistory()
history.push(location)
Expand Down
13 changes: 7 additions & 6 deletions src/__tests__/incidents/Incidents.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import { act } from '@testing-library/react'
import Permissions from 'model/Permissions'
import ViewIncident from '../../incidents/view/ViewIncident'
import Incidents from '../../incidents/Incidents'
import ReportIncident from '../../incidents/report/ReportIncident'
import Incident from '../../model/Incident'
import IncidentRepository from '../../clients/db/IncidentRepository'
import { RootState } from '../../store'

const mockStore = configureMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('Incidents', () => {
describe('routing', () => {
Expand All @@ -32,7 +33,7 @@ describe('Incidents', () => {
incident: {
incident: expectedIncident,
},
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand All @@ -51,7 +52,7 @@ describe('Incidents', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('Incidents', () => {
reportedOn: new Date().toISOString(),
},
},
})
} as any)

let wrapper: any

Expand All @@ -103,7 +104,7 @@ describe('Incidents', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = await mount(
<Provider store={store}>
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/incidents/list/ViewIncidents.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import * as breadcrumbUtil from '../../../breadcrumbs/useAddBreadcrumbs'
import ViewIncidents from '../../../incidents/list/ViewIncidents'
import Incident from '../../../model/Incident'
import IncidentRepository from '../../../clients/db/IncidentRepository'
import { RootState } from '../../../store'

const mockStore = createMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('View Incidents', () => {
let history: any
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('View Incidents', () => {
incidents: {
incidents: expectedIncidents,
},
})
} as any)

let wrapper: any
await act(async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/incidents/report/ReportIncident.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import * as ButtonBarProvider from '../../../page-header/ButtonBarProvider'
import * as breadcrumbUtil from '../../../breadcrumbs/useAddBreadcrumbs'
import ReportIncident from '../../../incidents/report/ReportIncident'
import IncidentRepository from '../../../clients/db/IncidentRepository'
import { RootState } from '../../../store'

const mockStore = createMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('Report Incident', () => {
let history: any
Expand All @@ -41,7 +42,7 @@ describe('Report Incident', () => {
incident: {
error,
},
})
} as any)

let wrapper: any
await act(async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/incidents/view/ViewIncident.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import * as breadcrumbUtil from '../../../breadcrumbs/useAddBreadcrumbs'
import ViewIncident from '../../../incidents/view/ViewIncident'
import Incident from '../../../model/Incident'
import IncidentRepository from '../../../clients/db/IncidentRepository'
import { RootState } from '../../../store'

const mockStore = createMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('View Incident', () => {
const expectedDate = new Date(2020, 5, 1, 19, 48)
Expand Down Expand Up @@ -50,7 +51,7 @@ describe('View Incident', () => {
incident: {
incident: expectedIncident,
},
})
} as any)

let wrapper: any
await act(async () => {
Expand Down
13 changes: 7 additions & 6 deletions src/__tests__/labs/Labs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mount } from 'enzyme'
import { MemoryRouter } from 'react-router-dom'
import { Provider } from 'react-redux'
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import { act } from '@testing-library/react'
import Labs from 'labs/Labs'
import NewLabRequest from 'labs/requests/NewLabRequest'
Expand All @@ -14,8 +14,9 @@ import LabRepository from 'clients/db/LabRepository'
import Lab from 'model/Lab'
import Patient from 'model/Patient'
import PatientRepository from 'clients/db/PatientRepository'
import { RootState } from '../../store'

const mockStore = configureMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('Labs', () => {
jest.spyOn(LabRepository, 'findAll').mockResolvedValue([])
Expand All @@ -39,7 +40,7 @@ describe('Labs', () => {
patient: { id: 'patientId', fullName: 'some name' },
error: {},
},
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand All @@ -58,7 +59,7 @@ describe('Labs', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = mount(
<Provider store={store}>
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('Labs', () => {
patient: { id: 'patientId', fullName: 'some name' },
error: {},
},
})
} as any)

let wrapper: any

Expand All @@ -111,7 +112,7 @@ describe('Labs', () => {
user: { permissions: [] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})
} as any)

const wrapper = await mount(
<Provider store={store}>
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/labs/ViewLab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import TextFieldWithLabelFormGroup from 'components/input/TextFieldWithLabelForm
import format from 'date-fns/format'
import * as titleUtil from '../../page-header/useTitle'
import ViewLab from '../../labs/ViewLab'
import { RootState } from '../../store'

const mockStore = createMockStore([thunk])
const mockStore = createMockStore<RootState, any>([thunk])

describe('View Labs', () => {
let history: any
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('View Labs', () => {
error,
status: Object.keys(error).length > 0 ? 'error' : 'completed',
},
})
} as any)

let wrapper: any
await act(async () => {
Expand Down
Loading