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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 @@ -6,7 +6,7 @@ import React from 'react'
import { act } from 'react-dom/test-utils'
import { Provider } from 'react-redux'
import { MemoryRouter } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import { addBreadcrumbs } from '../breadcrumbs/breadcrumbs-slice'
Expand All @@ -17,8 +17,9 @@ import Incidents from '../incidents/Incidents'
import ViewLabs from '../labs/ViewLabs'
import Permissions from '../model/Permissions'
import Appointments from '../scheduling/appointments/Appointments'
import { RootState } from '../store'

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

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

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

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

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

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

const wrapper = mount(
<Provider store={store}>
Expand All @@ -180,7 +181,7 @@ describe('HospitalRun', () => {
user: { permissions: [Permissions.WritePatients] },
breadcrumbs: { breadcrumbs: [] },
components: { sidebarCollapsed: false },
})}
} as any)}
>
<MemoryRouter initialEntries={['/']}>
<HospitalRun />
Expand Down
9 changes: 5 additions & 4 deletions src/__tests__/breadcrumbs/Breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '../../__mocks__/matchMediaMock'

import {
Breadcrumb as HRBreadcrumb,
BreadcrumbItem as HRBreadcrumbItem,
Expand All @@ -9,19 +8,21 @@ import { createMemoryHistory } from 'history'
import React from 'react'
import { Provider } from 'react-redux'
import { Router } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import Breadcrumbs from '../../breadcrumbs/Breadcrumbs'
import Breadcrumb from '../../model/Breadcrumb'
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,18 +1,20 @@
import { renderHook } from '@testing-library/react-hooks'
import React from 'react'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import * as breadcrumbsSlice from '../../breadcrumbs/breadcrumbs-slice'
import useAddBreadcrumbs from '../../breadcrumbs/useAddBreadcrumbs'
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 @@ -28,7 +30,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 @@ -47,7 +49,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 @@ -7,18 +7,19 @@ import { createMemoryHistory } from 'history'
import React from 'react'
import { Provider } from 'react-redux'
import { Router } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import Sidebar from '../../components/Sidebar'
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 @@ -5,7 +5,7 @@ import { mount } from 'enzyme'
import React from 'react'
import { Provider } from 'react-redux'
import { MemoryRouter } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import IncidentRepository from '../../clients/db/IncidentRepository'
Expand All @@ -14,8 +14,9 @@ import ReportIncident from '../../incidents/report/ReportIncident'
import ViewIncident from '../../incidents/view/ViewIncident'
import Incident from '../../model/Incident'
import Permissions from '../../model/Permissions'
import { RootState } from '../../store'

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

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

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

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

let wrapper: any

Expand All @@ -105,7 +106,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 @@ -16,8 +16,9 @@ import Incident from '../../../model/Incident'
import Permissions from '../../../model/Permissions'
import * as ButtonBarProvider from '../../../page-header/ButtonBarProvider'
import * as titleUtil from '../../../page-header/useTitle'
import { RootState } from '../../../store'

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

describe('View Incidents', () => {
let history: any
Expand Down Expand Up @@ -52,7 +53,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 @@ -16,8 +16,9 @@ import ReportIncident from '../../../incidents/report/ReportIncident'
import Permissions from '../../../model/Permissions'
import * as ButtonBarProvider from '../../../page-header/ButtonBarProvider'
import * as titleUtil from '../../../page-header/useTitle'
import { RootState } from '../../../store'

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

describe('Report Incident', () => {
let history: any
Expand All @@ -43,7 +44,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 @@ -16,8 +16,9 @@ import Incident from '../../../model/Incident'
import Permissions from '../../../model/Permissions'
import * as ButtonBarProvider from '../../../page-header/ButtonBarProvider'
import * as titleUtil from '../../../page-header/useTitle'
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 @@ -52,7 +53,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 @@ -5,7 +5,7 @@ import { mount } from 'enzyme'
import React from 'react'
import { Provider } from 'react-redux'
import { MemoryRouter } from 'react-router-dom'
import configureMockStore from 'redux-mock-store'
import createMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

import LabRepository from '../../clients/db/LabRepository'
Expand All @@ -16,8 +16,9 @@ import ViewLab from '../../labs/ViewLab'
import Lab from '../../model/Lab'
import Patient from '../../model/Patient'
import Permissions from '../../model/Permissions'
import { RootState } from '../../store'

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

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

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

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

let wrapper: any

Expand All @@ -113,7 +114,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 @@ -20,8 +20,9 @@ import Patient from '../../model/Patient'
import Permissions from '../../model/Permissions'
import * as ButtonBarProvider from '../../page-header/ButtonBarProvider'
import * as titleUtil from '../../page-header/useTitle'
import { RootState } from '../../store'

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

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

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