Skip to content

Commit

Permalink
RN-400: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-bes committed Jun 23, 2022
1 parent f99c27f commit adf82df
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 54 deletions.
33 changes: 29 additions & 4 deletions packages/central-server/src/tests/apiV2/getChanges.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

import { expect } from 'chai';

import { TYPES } from '@tupaia/database';
import { oneSecondSleep, randomIntBetween } from '@tupaia/utils';
import { SyncQueue } from '../../database';
import { TestableApp, upsertQuestion } from '../testUtilities';
import { MeditrakSyncQueue } from '../../database';
import { TestableApp, upsertEntity, upsertQuestion, upsertSurveyGroup } from '../testUtilities';

describe('GET /changes/count', async () => {
const app = new TestableApp();
const { models } = app;
const meditrakSyncQueue = new MeditrakSyncQueue(models);

before(async () => {
await app.grantFullAccess();

// Set up real sync queue for testing the /changes endpoint
const syncQueue = new SyncQueue(models, models.meditrakSyncQueue, [TYPES.QUESTION]); // eslint-disable-line no-unused-vars
await meditrakSyncQueue.createPermissionsBasedView();
meditrakSyncQueue.setDebounceTime(100); // Faster debounce time for tests
meditrakSyncQueue.listenForChanges();
});

after(() => {
Expand Down Expand Up @@ -47,6 +49,7 @@ describe('GET /changes/count', async () => {
}
// Wait one second for the triggers to have properly added the changes to the queue
await oneSecondSleep();
await meditrakSyncQueue.scheduleChangeQueueHandler();
const response = await app.get(`changes/count?since=${since}`);
expect(response.body.changeCount).to.equal(numberOfQuestionsToAdd);
});
Expand Down Expand Up @@ -112,6 +115,7 @@ describe('GET /changes/count', async () => {
const totalDeletes =
numberOfQuestionsToDeleteFromFirstUpdate + numberOfQuestionsToDeleteFromSecondUpdate;
const netNewRecords = grossNewRecords - totalDeletes;
await meditrakSyncQueue.scheduleChangeQueueHandler();
let response = await app.get(`changes/count?since=${timestampBeforeFirstUpdate}`);
expect(response.body.changeCount).to.equal(netNewRecords);

Expand All @@ -135,4 +139,25 @@ describe('GET /changes/count', async () => {
response = await app.get(`changes/count?since=${timestampBeforeSecondDelete}`);
expect(response.body.changeCount).to.equal(numberOfQuestionsToDeleteFromSecondUpdate);
});

it('should return the correct number of changes based on appVersion', async function () {
const since = Date.now();
// Wait one second for the triggers to have properly added the changes to the queue
await oneSecondSleep();

await upsertQuestion(); // version 0.0.1
await upsertSurveyGroup(); // version 1.6.69
await upsertEntity(); // version 1.7.102

await oneSecondSleep();
await meditrakSyncQueue.scheduleChangeQueueHandler();
let response = await app.get(`changes/count?since=${since}&appVersion=0.0.1`);
expect(response.body.changeCount).to.equal(1);

response = await app.get(`changes/count?since=${since}&appVersion=1.6.69`);
expect(response.body.changeCount).to.equal(2);

response = await app.get(`changes/count?since=${since}&appVersion=1.7.102`);
expect(response.body.changeCount).to.equal(3);
});
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export const upsertSurvey = async data => {
return upsertDummyRecord(models.survey, data);
};

export const upsertSurveyGroup = async data => {
return upsertDummyRecord(models.surveyGroup, data);
};

export const upsertSurveyResponse = async data => {
const publicPermissionGroup = await models.permissionGroup.findOne({ name: 'Public' });
const user = await models.user.findOne();
Expand Down

0 comments on commit adf82df

Please sign in to comment.