Skip to content

Commit

Permalink
feat(labs): added tests to feature
Browse files Browse the repository at this point in the history
Modified the Repository, added a mapping function to add an index for each sort field required.
Tests were failing due to the lack of index when sorting

fix HospitalRun#1971
  • Loading branch information
sotous committed May 1, 2020
1 parent 3060210 commit f890aa2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ package-lock.json
.DS_Store
yarn.lock
.vscode/launch.json
.vscode/tasks.json
49 changes: 27 additions & 22 deletions src/__tests__/labs/labs.slice.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { AnyAction } from 'redux'
import { mocked } from 'ts-jest/utils'
import SortRequest from 'clients/db/SortRequest'
import labs, { fetchLabsStart, fetchLabsSuccess, searchLabs } from '../../labs/labs-slice'
import labs, {
fetchLabsStart,
fetchLabsSuccess,
fetchLabs,
searchLabs,
} from '../../labs/labs-slice'
import Lab from '../../model/Lab'
import LabRepository from '../../clients/db/LabRepository'

Expand Down Expand Up @@ -63,15 +68,15 @@ describe('labs slice', () => {
expect(LabRepository.search).toHaveBeenCalledWith(expectedSearchObject)
})

// it('should call the LabRepository findAll method if there is no string text and status is set to all', async () => {
// const dispatch = jest.fn()
// const getState = jest.fn()
// jest.spyOn(LabRepository, 'findAll')
it('should call the LabRepository findAll method if there is no string text and status is set to all', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(LabRepository, 'findAll')

// await searchLabs('')(dispatch, getState, null)
await searchLabs('')(dispatch, getState, null)

// expect(LabRepository.findAll).toHaveBeenCalledTimes(1)
// })
expect(LabRepository.findAll).toHaveBeenCalledTimes(1)
})

it('should dispatch the FETCH_LABS_SUCCESS action', async () => {
const dispatch = jest.fn()
Expand Down Expand Up @@ -105,25 +110,25 @@ describe('labs slice', () => {
],
}

// it('should have called findAll with sort request in searchLabs method', async () => {
// const dispatch = jest.fn()
// const getState = jest.fn()
// jest.spyOn(LabRepository, 'findAll')
it('should have called findAll with sort request in searchLabs method', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(LabRepository, 'findAll')

// await searchLabs('')(dispatch, getState, null)
await searchLabs('')(dispatch, getState, null)

// expect(LabRepository.findAll).toHaveBeenCalledWith(sortRequest)
// })
expect(LabRepository.findAll).toHaveBeenCalledWith(sortRequest)
})

// it('should have called findAll with sort request in fetchLabs method', async () => {
// const dispatch = jest.fn()
// const getState = jest.fn()
// jest.spyOn(LabRepository, 'findAll')
it('should have called findAll with sort request in fetchLabs method', async () => {
const dispatch = jest.fn()
const getState = jest.fn()
jest.spyOn(LabRepository, 'findAll')

// await fetchLabs()(dispatch, getState, null)
await fetchLabs()(dispatch, getState, null)

// expect(LabRepository.findAll).toHaveBeenCalledWith(sortRequest)
// })
expect(LabRepository.findAll).toHaveBeenCalledWith(sortRequest)
})

it('should include sorts in the search criteria', async () => {
const dispatch = jest.fn()
Expand Down
3 changes: 0 additions & 3 deletions src/clients/db/LabRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ interface SearchContainer {
export class LabRepository extends Repository<Lab> {
constructor() {
super(labs)
labs.createIndex({
index: { fields: ['requestedOn', 'type', 'status'] },
})
}

async search(container: SearchContainer): Promise<Lab[]> {
Expand Down
19 changes: 18 additions & 1 deletion src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint "@typescript-eslint/camelcase": "off" */
import { v4 as uuidv4 } from 'uuid'
import AbstractDBModel from '../../model/AbstractDBModel'
import { Unsorted } from './SortRequest'
import SortRequest, { Unsorted } from './SortRequest'

function mapDocument(document: any): any {
const { _id, _rev, ...values } = document
Expand Down Expand Up @@ -33,6 +33,23 @@ export default class Repository<T extends AbstractDBModel> {
selector[s.field] = { $gt: null }
})

// Adds an index to each of the fields coming from the sorting object
// allowing the algorithm to sort by any given SortRequest, by avoiding the default index error (lack of index)

await Promise.all(
sort.sorts.map(
async (s): Promise<SortRequest> => {
await this.db.createIndex({
index: {
fields: [s.field],
},
})

return sort
},
),
)

const result = await this.db.find({
selector,
sort: sort.sorts.length > 0 ? sort.sorts.map((s) => ({ [s.field]: s.direction })) : undefined,
Expand Down

0 comments on commit f890aa2

Please sign in to comment.