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

Commit

Permalink
fix(test): cleaned up id and fixed test with sequential insert
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Feb 19, 2020
1 parent 88b2204 commit 0f061d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 73 deletions.
114 changes: 55 additions & 59 deletions src/__tests__/clients/db/PatientRepository.test.ts
Expand Up @@ -2,178 +2,174 @@ 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'

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

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)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/patients/new/NewPatient.test.tsx
Expand Up @@ -52,7 +52,7 @@ describe('New Patient', () => {
}

beforeEach(() => {
jest.restoreAllMocks()
jest.resetAllMocks()
})

it('should render a general information form', async () => {
Expand Down
31 changes: 18 additions & 13 deletions src/__tests__/patients/patient-slice.test.ts
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 0f061d2

Please sign in to comment.