From 8a4443ae7ddab442b9051f63f8330ece76a0e0b0 Mon Sep 17 00:00:00 2001 From: kennsippell Date: Fri, 13 Dec 2024 18:06:56 -0800 Subject: [PATCH] Test fixes after merge --- src/lib/manage-hierarchy.ts | 7 ++--- test/lib/manage-hierarchy.spec.ts | 44 ++++++++++++++++--------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/lib/manage-hierarchy.ts b/src/lib/manage-hierarchy.ts index 63d32d2..4df5d65 100644 --- a/src/lib/manage-hierarchy.ts +++ b/src/lib/manage-hierarchy.ts @@ -1,13 +1,14 @@ import _ from 'lodash'; import Auth from './authentication'; -import { ChtApi, RemotePlace } from './cht-api'; +import { ChtApi } from './cht-api'; import { ChtConfJobData } from '../worker/cht-conf-worker'; import { ContactType } from '../config'; import { JobParams, IQueue, getChtConfQueue } from './queues'; import Place from '../services/place'; import RemotePlaceResolver from './remote-place-resolver'; import SessionCache from '../services/session-cache'; +import { RemotePlace } from './remote-place-cache'; export const HIERARCHY_ACTIONS = ['move', 'merge', 'delete']; export type HierarchyAction = typeof HIERARCHY_ACTIONS[number]; @@ -85,7 +86,7 @@ function getSourceAndDestinationIds( if (hierarchyAction === 'move') { if (destinationId === sourceLineage[1]?.id) { - throw Error(`Place "${sourceLineage[0]?.name}" already has "${destinationLineage[1]?.name}" as parent`); + throw Error(`Place "${sourceLineage[0]?.name.original}" already has "${destinationLineage[1]?.name.original}" as parent`); } } @@ -129,7 +130,7 @@ async function resolve(prefix: string, formData: any, contactType: ContactType, await RemotePlaceResolver.resolveOne(place, sessionCache, chtApi, { fuzz: true }); place.validate(); - const validationError = place.validationErrors && Object.keys(place.validationErrors).find(err => err.startsWith('hierarchy_')); + const validationError = place.validationErrors && Object.keys(place.validationErrors).find(err => err.startsWith(prefix)); if (validationError) { throw Error(place.validationErrors?.[validationError]); } diff --git a/test/lib/manage-hierarchy.spec.ts b/test/lib/manage-hierarchy.spec.ts index bb8e840..a5b76a9 100644 --- a/test/lib/manage-hierarchy.spec.ts +++ b/test/lib/manage-hierarchy.spec.ts @@ -9,6 +9,7 @@ import { mockChtApi } from '../mocks'; import chaiAsPromised from 'chai-as-promised'; import Auth from '../../src/lib/authentication'; import { BullQueue } from '../../src/lib/queues'; +import RemotePlaceCache from '../../src/lib/remote-place-cache'; Chai.use(chaiAsPromised); const { expect } = Chai; @@ -16,26 +17,27 @@ const { expect } = Chai; describe('lib/manage-hierarchy.ts', () => { let chtConfQueue: any; + const subcountyDocs = [ + { _id: 'from-sub', name: 'From Sub' }, + { _id: 'to-sub', name: 'To Sub' } + ]; + const chuDocs = [ + { _id: 'from-chu-id', name: 'c-h-u', parent: { _id: 'from-sub' } }, + { _id: 'to-chu-id', name: 'destination', parent: { _id: 'to-sub' } }, + ]; + + const chtApiWithDocs = () => mockChtApi(subcountyDocs, chuDocs); + beforeEach(() => { chtConfQueue = sinon.createStubInstance(BullQueue); sinon.stub(Auth, 'encodeTokenForWorker').returns('encoded-token'); + RemotePlaceCache.clear({}); }); afterEach(() => { sinon.restore(); }); - const chtApi = () => mockChtApi( - [ - { id: 'from-sub', name: 'From Sub', lineage: [], type: 'remote' }, - { id: 'to-sub', name: 'To Sub', lineage: [], type: 'remote' } - ], - [ - { id: 'from-chu-id', name: 'c-h-u', lineage: ['from-sub'], type: 'remote' }, - { id: 'to-chu-id', name: 'destination', lineage: ['to-sub'], type: 'remote' } - ], - ); - describe('move', () => { it('move CHU: success', async () => { const formData = { @@ -47,7 +49,7 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); + const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue); expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']); expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq([undefined, 'to-sub']); @@ -55,7 +57,7 @@ describe('lib/manage-hierarchy.ts', () => { expect(chtConfQueue.add.calledOnce).to.be.true; const jobParams = chtConfQueue.add.getCall(0).args[0]; - expect(jobParams).to.have.property('jobName').that.equals('move_[From Sub.c-h-u]_to_[To Sub]'); + expect(jobParams).to.have.property('jobName').that.equals('move_[From Sub.C-h-u]_to_[To Sub]'); expect(jobParams).to.have.property('jobData').that.deep.include({ action: 'move', sourceId: 'from-chu-id', @@ -74,7 +76,7 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); + const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, mockChtApi(chuDocs), chtConfQueue); await expect(actual).to.eventually.be.rejectedWith('search string is empty'); }); @@ -88,7 +90,7 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); + const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue); await expect(actual).to.eventually.be.rejectedWith('Place "c-h-u" already has "From Sub" as parent'); }); @@ -102,8 +104,8 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); - await expect(actual).to.eventually.be.rejectedWith('Cannot find \'b_sub_county\' matching \'Invalid Sub\''); + const actual = ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue); + await expect(actual).to.eventually.be.rejectedWith('Cannot find \'b_sub_county\' matching \'invalid sub\''); }); }); @@ -119,7 +121,7 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); + const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue); expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']); expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq(['to-chu-id', 'to-sub']); @@ -127,7 +129,7 @@ describe('lib/manage-hierarchy.ts', () => { expect(chtConfQueue.add.calledOnce).to.be.true; const jobParams = chtConfQueue.add.getCall(0).args[0]; - expect(jobParams).to.have.property('jobName').that.equals('merge_[From Sub.c-h-u]_to_[To Sub.destination]'); + expect(jobParams).to.have.property('jobName').that.equals('merge_[From Sub.C-h-u]_to_[To Sub.Destination]'); expect(jobParams).to.have.property('jobData').that.deep.include({ action: 'merge', sourceId: 'from-chu-id', @@ -148,7 +150,7 @@ describe('lib/manage-hierarchy.ts', () => { const contactType = Config.getContactType('c_community_health_unit'); const sessionCache = new SessionCache(); - const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApi(), chtConfQueue); + const actual = await ManageHierarchyLib.scheduleJob(formData, contactType, sessionCache, chtApiWithDocs(), chtConfQueue); expect(actual.sourceLineage.map((l:any) => l.id)).to.deep.eq(['from-chu-id', 'from-sub']); expect(actual.destinationLineage.map((l:any) => l.id)).to.deep.eq([]); @@ -156,7 +158,7 @@ describe('lib/manage-hierarchy.ts', () => { expect(chtConfQueue.add.calledOnce).to.be.true; const jobParams = chtConfQueue.add.getCall(0).args[0]; - expect(jobParams).to.have.property('jobName').that.equals('delete_[From Sub.c-h-u]'); + expect(jobParams).to.have.property('jobName').that.equals('delete_[From Sub.C-h-u]'); expect(jobParams).to.have.property('jobData').that.deep.include({ action: 'delete', sourceId: 'from-chu-id',