From 0f061d2bec64883453c7ca283c541db846459f51 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Tue, 18 Feb 2020 18:55:15 -0600 Subject: [PATCH] fix(test): cleaned up id and fixed test with sequential insert --- .../clients/db/PatientRepository.test.ts | 114 +++++++++--------- .../patients/new/NewPatient.test.tsx | 2 +- src/__tests__/patients/patient-slice.test.ts | 31 +++-- 3 files changed, 74 insertions(+), 73 deletions(-) diff --git a/src/__tests__/clients/db/PatientRepository.test.ts b/src/__tests__/clients/db/PatientRepository.test.ts index 6d60393d33..6c16fe003f 100644 --- a/src/__tests__/clients/db/PatientRepository.test.ts +++ b/src/__tests__/clients/db/PatientRepository.test.ts @@ -2,162 +2,160 @@ import { patients } from 'config/pouchdb' import PatientRepository from 'clients/db/PatientRepository' import Patient from 'model/Patient' import { fromUnixTime } from 'date-fns' -import { updatePatientStart } from 'patients/patient-slice' + +async function removeAllDocs() { + // eslint-disable-next-line + const allDocs = await patients.allDocs({ include_docs: true }) + await Promise.all( + allDocs.rows.map(async (row) => { + if (row.doc) { + await patients.remove(row.doc) + } + }), + ) +} describe('patient repository', () => { describe('find', () => { + afterEach(async () => { + await removeAllDocs() + }) it('should return a patient with the correct data', async () => { - await patients.put({ _id: 'id5678' }) // store another patient just to make sure we pull back the right one - const expectedPatient = await patients.put({ _id: 'id1234' }) + await patients.put({ _id: 'id1111' }) // store another patient just to make sure we pull back the right one + const expectedPatient = await patients.put({ _id: 'id2222' }) - const actualPatient = await PatientRepository.find('id1234') + const actualPatient = await PatientRepository.find('id2222') expect(actualPatient).toBeDefined() expect(actualPatient.id).toEqual(expectedPatient.id) - - await patients.remove(await patients.get('id1234')) - await patients.remove(await patients.get('id5678')) }) }) describe('search', () => { + afterEach(async () => { + await removeAllDocs() + }) + it('should return all records that friendly ids match search text', async () => { // same full name to prove that it is finding by friendly id const expectedFriendlyId = 'P00001' - await patients.put({ _id: 'id5678', friendlyId: expectedFriendlyId, fullName: 'test test' }) - await patients.put({ _id: 'id1234', friendlyId: 'P00002', fullName: 'test test' }) + await patients.put({ _id: 'someId1', friendlyId: expectedFriendlyId, fullName: 'test test' }) + await patients.put({ _id: 'someId2', friendlyId: 'P00002', fullName: 'test test' }) const result = await PatientRepository.search(expectedFriendlyId) expect(result).toHaveLength(1) expect(result[0].friendlyId).toEqual(expectedFriendlyId) - - await patients.remove(await patients.get('id1234')) - await patients.remove(await patients.get('id5678')) }) it('should return all records that fullName contains search text', async () => { - await patients.put({ _id: 'id1234', friendlyId: 'P00002', fullName: 'blh test test blah' }) - await patients.put({ _id: 'id5678', friendlyId: 'P00001', fullName: 'test test' }) - await patients.put({ _id: 'id2345', friendlyId: 'P00003', fullName: 'not found' }) + await patients.put({ _id: 'id3333', friendlyId: 'P00002', fullName: 'blh test test blah' }) + await patients.put({ _id: 'id4444', friendlyId: 'P00001', fullName: 'test test' }) + await patients.put({ _id: 'id5555', friendlyId: 'P00003', fullName: 'not found' }) const result = await PatientRepository.search('test test') expect(result).toHaveLength(2) - expect(result[0].id).toEqual('id1234') - expect(result[1].id).toEqual('id5678') - - await patients.remove(await patients.get('id1234')) - await patients.remove(await patients.get('id5678')) - await patients.remove(await patients.get('id2345')) + expect(result[0].id).toEqual('id3333') + expect(result[1].id).toEqual('id4444') }) it('should match search criteria with case insensitive match', async () => { - await patients.put({ _id: 'id5678', friendlyId: 'P00001', fullName: 'test test' }) - await patients.put({ _id: 'id1234', friendlyId: 'P00002', fullName: 'not found' }) + await patients.put({ _id: 'id6666', friendlyId: 'P00001', fullName: 'test test' }) + await patients.put({ _id: 'id7777', friendlyId: 'P00002', fullName: 'not found' }) const result = await PatientRepository.search('TEST TEST') expect(result).toHaveLength(1) - expect(result[0].id).toEqual('id5678') - - await patients.remove(await patients.get('id1234')) - await patients.remove(await patients.get('id5678')) + expect(result[0].id).toEqual('id6666') }) }) describe('findAll', () => { + afterEach(async () => { + await removeAllDocs() + }) it('should find all patients in the database sorted by their ids', async () => { - const expectedPatient2 = await patients.put({ _id: 'id5678' }) - const expectedPatient1 = await patients.put({ _id: 'id1234' }) + const expectedPatient1 = await patients.put({ _id: 'id9999' }) + const expectedPatient2 = await patients.put({ _id: 'id8888' }) const result = await PatientRepository.findAll() expect(result).toHaveLength(2) - expect(result[0].id).toEqual(expectedPatient1.id) - expect(result[1].id).toEqual(expectedPatient2.id) - - await patients.remove(await patients.get('id1234')) - await patients.remove(await patients.get('id5678')) + expect(result[0].id).toEqual(expectedPatient2.id) + expect(result[1].id).toEqual(expectedPatient1.id) }) }) describe('save', () => { + afterEach(async () => { + await removeAllDocs() + }) + it('should generate an id that is a timestamp for the patient', async () => { const newPatient = await PatientRepository.save({ fullName: 'test test', } as Patient) expect(fromUnixTime(parseInt(newPatient.id, 10)).getTime() > 0).toBeTruthy() - - await patients.remove(await patients.get(newPatient.id)) }) it('should generate a friendly id', async () => { const newPatient = await PatientRepository.save({ - fullName: 'test test', + fullName: 'test1 test1', } as Patient) expect(newPatient.friendlyId).toEqual('P00001') - - await patients.remove(await patients.get(newPatient.id)) }) it('should sequentially generate a friendly id', async () => { - const existingPatient = await PatientRepository.save({ - fullName: 'test test', - } as Patient) + await patients.put({ _id: 'id9999', friendlyId: 'P00001' }) const newPatient = await PatientRepository.save({ - fullName: 'test1 test1', + fullName: 'test3 test3', } as Patient) expect(newPatient.friendlyId).toEqual('P00002') - - await patients.remove(await patients.get(existingPatient.id)) - await patients.remove(await patients.get(newPatient.id)) }) }) describe('saveOrUpdate', () => { + afterEach(async () => { + await removeAllDocs() + }) + it('should save the patient if an id was not on the entity', async () => { const newPatient = await PatientRepository.saveOrUpdate({ - fullName: 'test1 test1', + fullName: 'test4 test4', } as Patient) expect(newPatient.id).toBeDefined() - - await patients.remove(await patients.get(newPatient.id)) }) it('should update the patient if one was already existing', async () => { const existingPatient = await PatientRepository.save({ - fullName: 'test test', + fullName: 'test5 test5', } as Patient) const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient) expect(updatedPatient.id).toEqual(existingPatient.id) - - await patients.remove(await patients.get(existingPatient.id)) }) it('should update the existing fields', async () => { const existingPatient = await PatientRepository.save({ - fullName: 'test test', + fullName: 'test6 test6', } as Patient) existingPatient.fullName = 'changed' const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient) expect(updatedPatient.fullName).toEqual('changed') - - await patients.remove(await patients.get(existingPatient.id)) }) it('should add new fields without changing existing fields', async () => { const existingPatient = await PatientRepository.save({ - fullName: 'test test', + fullName: 'test7 test7', } as Patient) existingPatient.givenName = 'givenName' @@ -165,15 +163,13 @@ describe('patient repository', () => { expect(updatedPatient.fullName).toEqual(existingPatient.fullName) expect(updatedPatient.givenName).toEqual('givenName') - - await patients.remove(await patients.get(existingPatient.id)) }) }) describe('delete', () => { it('should delete the patient', async () => { const patientToDelete = await PatientRepository.save({ - fullName: 'test test', + fullName: 'test8 test8', } as Patient) await PatientRepository.delete(patientToDelete) diff --git a/src/__tests__/patients/new/NewPatient.test.tsx b/src/__tests__/patients/new/NewPatient.test.tsx index 8a7ccb659e..e1af5011f4 100644 --- a/src/__tests__/patients/new/NewPatient.test.tsx +++ b/src/__tests__/patients/new/NewPatient.test.tsx @@ -52,7 +52,7 @@ describe('New Patient', () => { } beforeEach(() => { - jest.restoreAllMocks() + jest.resetAllMocks() }) it('should render a general information form', async () => { diff --git a/src/__tests__/patients/patient-slice.test.ts b/src/__tests__/patients/patient-slice.test.ts index 65f83e5185..461b8001fd 100644 --- a/src/__tests__/patients/patient-slice.test.ts +++ b/src/__tests__/patients/patient-slice.test.ts @@ -105,10 +105,12 @@ describe('patients slice', () => { describe('createPatient()', () => { it('should dispatch the CREATE_PATIENT_START action', async () => { + jest.spyOn(PatientRepository, 'save') + mocked(PatientRepository).save.mockResolvedValue({ id: 'sliceId1' } as Patient) const dispatch = jest.fn() const getState = jest.fn() const expectedPatient = { - id: 'id', + id: 'sliceId1', } as Patient await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null) @@ -120,8 +122,9 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'save') + mocked(PatientRepository).save.mockResolvedValue({ id: 'sliceId2' } as Patient) const expectedPatient = { - id: 'id', + id: 'sliceId2', } as Patient await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null) @@ -133,9 +136,9 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() const mockedPatientRepository = mocked(PatientRepository, true) - mockedPatientRepository.save.mockResolvedValue({ id: '12345' } as Patient) + mockedPatientRepository.save.mockResolvedValue({ id: 'slideId3' } as Patient) const expectedPatient = { - id: 'id', + id: 'slideId3', } as Patient await createPatient(expectedPatient, createMemoryHistory())(dispatch, getState, null) @@ -144,7 +147,8 @@ describe('patients slice', () => { }) it('should navigate to the /patients/:id where id is the new patient id', async () => { - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId4' + jest.spyOn(PatientRepository, 'save') const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.save.mockResolvedValue({ id: expectedPatientId } as Patient) const history = createMemoryHistory() @@ -160,12 +164,13 @@ describe('patients slice', () => { it('should call the Toaster function with the correct data', async () => { jest.spyOn(components, 'Toast') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId5' const expectedFullName = 'John Doe II' const expectedPatient = { id: expectedPatientId, fullName: expectedFullName, } as Patient + jest.spyOn(PatientRepository, 'save') const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.save.mockResolvedValue(expectedPatient) const mockedComponents = mocked(components, true) @@ -188,7 +193,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'find') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId6' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.find.mockResolvedValue(expectedPatient) @@ -202,7 +207,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'find') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId7' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.find.mockResolvedValue(expectedPatient) @@ -217,7 +222,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'find') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId8' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.find.mockResolvedValue(expectedPatient) @@ -238,7 +243,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'saveOrUpdate') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId9' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient) @@ -252,7 +257,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'saveOrUpdate') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId10' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient) @@ -266,7 +271,7 @@ describe('patients slice', () => { const dispatch = jest.fn() const getState = jest.fn() jest.spyOn(PatientRepository, 'saveOrUpdate') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId11' const expectedPatient = { id: expectedPatientId } as Patient const mockedPatientRepository = mocked(PatientRepository, true) mockedPatientRepository.saveOrUpdate.mockResolvedValue(expectedPatient) @@ -281,7 +286,7 @@ describe('patients slice', () => { it('should call the Toaster function with the correct data', async () => { jest.spyOn(components, 'Toast') - const expectedPatientId = '12345' + const expectedPatientId = 'sliceId11' const fullName = 'John Doe II' const expectedPatient = { id: expectedPatientId,