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

Commit

Permalink
feat: fix tests for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Apr 9, 2020
1 parent a1569b1 commit 3ce732a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/__tests__/scheduling/appointments/Appointments.test.tsx
Expand Up @@ -12,6 +12,8 @@ import PatientRepository from 'clients/db/PatientRepository'
import { mocked } from 'ts-jest/utils'
import Patient from 'model/Patient'
import * as ButtonBarProvider from 'page-header/ButtonBarProvider'
import AppointmentRepository from 'clients/db/AppointmentRepository'
import Appointment from 'model/Appointment'
import * as titleUtil from '../../../page-header/useTitle'

describe('Appointments', () => {
Expand All @@ -25,9 +27,10 @@ describe('Appointments', () => {
location: 'location',
reason: 'reason',
},
]
] as Appointment[]

const setup = async () => {
jest.spyOn(AppointmentRepository, 'findAll').mockResolvedValue(expectedAppointments)
jest.spyOn(PatientRepository, 'find')
const mockedPatientRepository = mocked(PatientRepository, true)
mockedPatientRepository.find.mockResolvedValue({
Expand Down
7 changes: 3 additions & 4 deletions src/clients/db/LabRepository.ts
Expand Up @@ -2,13 +2,12 @@ import Lab from 'model/Lab'
import Repository from './Repository'
import { labs } from '../../config/pouchdb'

labs.createIndex({
index: { fields: ['requestedOn'] },
})

export class LabRepository extends Repository<Lab> {
constructor() {
super(labs)
labs.createIndex({
index: { fields: ['requestedOn'] },
})
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/clients/db/Repository.ts
Expand Up @@ -25,23 +25,24 @@ export default class Repository<T extends AbstractDBModel> {
}

async findAll(sort = Unsorted): Promise<T[]> {
const selector = {
const selector: any = {
_id: { $gt: null },
}

return this.search({ selector }, sort)
}

async search(criteria: any, sort: SortRequest = Unsorted): Promise<T[]> {
// hack to get around the requirement that any sorted field must be in the selector list
sort.sorts.forEach((s) => {
criteria.selector[s.field] = { $gt: null }
selector[s.field] = { $gt: null }
})
const allCriteria = {
...criteria,
sort: sort.sorts.map((s) => ({ [s.field]: s.direction })),
}
const response = await this.db.find(allCriteria)

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

return result.docs.map(mapDocument)
}

async search(criteria: any): Promise<T[]> {
const response = await this.db.find(criteria)
return response.docs.map(mapDocument)
}

Expand Down

0 comments on commit 3ce732a

Please sign in to comment.