From 3ce732a99a9952e082febd811a1205260682f1e2 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Wed, 8 Apr 2020 22:58:13 -0500 Subject: [PATCH] feat: fix tests for sorting --- .../appointments/Appointments.test.tsx | 5 +++- src/clients/db/LabRepository.ts | 7 +++--- src/clients/db/Repository.ts | 25 ++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/__tests__/scheduling/appointments/Appointments.test.tsx b/src/__tests__/scheduling/appointments/Appointments.test.tsx index 4c3b73282e..d7f2feea05 100644 --- a/src/__tests__/scheduling/appointments/Appointments.test.tsx +++ b/src/__tests__/scheduling/appointments/Appointments.test.tsx @@ -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', () => { @@ -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({ diff --git a/src/clients/db/LabRepository.ts b/src/clients/db/LabRepository.ts index 742c15cb84..6ffbfdd6d3 100644 --- a/src/clients/db/LabRepository.ts +++ b/src/clients/db/LabRepository.ts @@ -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 { constructor() { super(labs) + labs.createIndex({ + index: { fields: ['requestedOn'] }, + }) } } diff --git a/src/clients/db/Repository.ts b/src/clients/db/Repository.ts index 18dfa6ab90..38186304e3 100644 --- a/src/clients/db/Repository.ts +++ b/src/clients/db/Repository.ts @@ -25,23 +25,24 @@ export default class Repository { } async findAll(sort = Unsorted): Promise { - const selector = { + const selector: any = { _id: { $gt: null }, } - return this.search({ selector }, sort) - } - - async search(criteria: any, sort: SortRequest = Unsorted): Promise { - // 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 { + const response = await this.db.find(criteria) return response.docs.map(mapDocument) }