|
| 1 | +import '../../../__mocks__/matchMediaMock' |
| 2 | +import React from 'react' |
| 3 | +import PatientRepository from 'clients/db/PatientRepository' |
| 4 | +import Note from 'model/Note' |
| 5 | +import { createMemoryHistory } from 'history' |
| 6 | +import configureMockStore from 'redux-mock-store' |
| 7 | +import Patient from 'model/Patient' |
| 8 | +import thunk from 'redux-thunk' |
| 9 | +import { mount } from 'enzyme' |
| 10 | +import { Router } from 'react-router' |
| 11 | +import { Provider } from 'react-redux' |
| 12 | +import NoteTab from 'patients/notes/NoteTab' |
| 13 | +import * as components from '@hospitalrun/components' |
| 14 | +import { act } from 'react-dom/test-utils' |
| 15 | +import { mocked } from 'ts-jest/utils' |
| 16 | +import NewNoteModal from 'patients/notes/NewNoteModal' |
| 17 | +import Permissions from '../../../model/Permissions' |
| 18 | +import * as patientSlice from '../../../patients/patient-slice' |
| 19 | + |
| 20 | +const expectedPatient = { |
| 21 | + id: '123', |
| 22 | + notes: [{ date: new Date().toISOString(), text: 'notes1' } as Note], |
| 23 | +} as Patient |
| 24 | + |
| 25 | +const mockStore = configureMockStore([thunk]) |
| 26 | +const history = createMemoryHistory() |
| 27 | + |
| 28 | +let user: any |
| 29 | +let store: any |
| 30 | + |
| 31 | +const setup = (patient = expectedPatient, permissions = [Permissions.WritePatients]) => { |
| 32 | + user = { permissions } |
| 33 | + store = mockStore({ patient, user }) |
| 34 | + const wrapper = mount( |
| 35 | + <Router history={history}> |
| 36 | + <Provider store={store}> |
| 37 | + <NoteTab patient={patient} /> |
| 38 | + </Provider> |
| 39 | + </Router>, |
| 40 | + ) |
| 41 | + |
| 42 | + return wrapper |
| 43 | +} |
| 44 | + |
| 45 | +describe('Notes Tab', () => { |
| 46 | + describe('Add New Note', () => { |
| 47 | + beforeEach(() => { |
| 48 | + jest.resetAllMocks() |
| 49 | + jest.spyOn(PatientRepository, 'saveOrUpdate') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should render a add notes button', () => { |
| 53 | + const wrapper = setup() |
| 54 | + |
| 55 | + const addNoteButton = wrapper.find(components.Button) |
| 56 | + expect(addNoteButton).toHaveLength(1) |
| 57 | + expect(addNoteButton.text().trim()).toEqual('patient.notes.new') |
| 58 | + }) |
| 59 | + |
| 60 | + it('should not render a add notes button if the user does not have permissions', () => { |
| 61 | + const wrapper = setup(expectedPatient, []) |
| 62 | + |
| 63 | + const addNotesButton = wrapper.find(components.Button) |
| 64 | + expect(addNotesButton).toHaveLength(0) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should open the Add Notes Modal', () => { |
| 68 | + const wrapper = setup() |
| 69 | + |
| 70 | + act(() => { |
| 71 | + const onClick = wrapper.find(components.Button).prop('onClick') as any |
| 72 | + onClick() |
| 73 | + }) |
| 74 | + wrapper.update() |
| 75 | + |
| 76 | + expect(wrapper.find(components.Modal).prop('show')).toBeTruthy() |
| 77 | + }) |
| 78 | + |
| 79 | + it('should update the patient with the new diagnosis when the save button is clicked', async () => { |
| 80 | + const expectedNote = { |
| 81 | + text: 'note text', |
| 82 | + date: new Date().toISOString(), |
| 83 | + } as Note |
| 84 | + const expectedUpdatedPatient = { |
| 85 | + ...expectedPatient, |
| 86 | + notes: [...(expectedPatient.notes as any), expectedNote], |
| 87 | + } as Patient |
| 88 | + |
| 89 | + const mockedPatientRepository = mocked(PatientRepository, true) |
| 90 | + mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedUpdatedPatient) |
| 91 | + |
| 92 | + const wrapper = setup() |
| 93 | + |
| 94 | + await act(async () => { |
| 95 | + const modal = wrapper.find(NewNoteModal) |
| 96 | + await modal.prop('onSave')(expectedNote) |
| 97 | + }) |
| 98 | + |
| 99 | + expect(mockedPatientRepository.saveOrUpdate).toHaveBeenCalledWith(expectedUpdatedPatient) |
| 100 | + expect(store.getActions()).toContainEqual(patientSlice.updatePatientStart()) |
| 101 | + expect(store.getActions()).toContainEqual( |
| 102 | + patientSlice.updatePatientSuccess(expectedUpdatedPatient), |
| 103 | + ) |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + describe('notes list', () => { |
| 108 | + it('should list the patients diagnoses', () => { |
| 109 | + const notes = expectedPatient.notes as Note[] |
| 110 | + const wrapper = setup() |
| 111 | + |
| 112 | + const list = wrapper.find(components.List) |
| 113 | + const listItems = wrapper.find(components.ListItem) |
| 114 | + |
| 115 | + expect(list).toHaveLength(1) |
| 116 | + expect(listItems).toHaveLength(notes.length) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should render a warning message if the patient does not have any diagnoses', () => { |
| 120 | + const wrapper = setup({ ...expectedPatient, notes: [] }) |
| 121 | + |
| 122 | + const alert = wrapper.find(components.Alert) |
| 123 | + |
| 124 | + expect(alert).toHaveLength(1) |
| 125 | + expect(alert.prop('title')).toEqual('patient.notes.warning.noNotes') |
| 126 | + expect(alert.prop('message')).toEqual('patient.notes.addNoteAbove') |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments