Skip to content

Commit 2c0932f

Browse files
committed
fix(tests): refactor waitForSettings to poll API _rev instead of logs
1 parent d231228 commit 2c0932f

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

tests/integration/api/controllers/records.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ describe('Import Records', () => {
3434
}
3535
}
3636
}
37-
}, { ignoreReload: true })
38-
.then(() => utils.waitForSettings(settings => settings.forms && settings.forms.TEST)));
37+
}, { ignoreReload: true }));
3938

4039
describe('JSON', () => {
4140
it('parses and stores the passed JSON', () => {

tests/utils/index.js

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,16 @@ const shouldDocumentBeKept = (doc, filters) => {
477477
filters.patterns.some(pattern => doc._id.match(pattern));
478478
};
479479

480-
const waitForSettings = async (checkFn, retries = 15) => {
480+
const waitForSettings = async (previousRev, retries = 15) => {
481481
const settings = await request({ path: '/api/v1/settings' });
482-
if (checkFn(settings)) {
482+
if (settings._rev !== previousRev) {
483483
return;
484484
}
485485
if (retries <= 0) {
486486
throw new Error('Timed out waiting for settings update');
487487
}
488488
await delayPromise(500);
489-
return waitForSettings(checkFn, retries - 1);
489+
return waitForSettings(previousRev, retries - 1);
490490
};
491491

492492
const deleteSentinelDocs = async (docsToKeep) => {
@@ -563,11 +563,8 @@ const updateCustomSettings = async (updates) => {
563563
});
564564
};
565565

566-
const waitForSettingsUpdateLogs = (type) => {
567-
if (type === 'sentinel') {
568-
return waitForSentinelLogs(true, /Reminder messages allowed between/);
569-
}
570-
return waitForApiLogs(/Settings updated/);
566+
const waitForSentinelSettingsUpdateLogs = () => {
567+
return waitForSentinelLogs(true, /Reminder messages allowed between/);
571568
};
572569

573570
/**
@@ -611,11 +608,27 @@ const updateSettings = async (updates, options = {}) => {
611608
if (revert) {
612609
await revertSettings(true);
613610
}
614-
const watcher = ignoreReload && Object.keys(updates).length && await waitForSettingsUpdateLogs(ignoreReload);
611+
612+
let previousRev;
613+
if (ignoreReload && ignoreReload !== 'sentinel') {
614+
const settings = await request({ path: '/api/v1/settings' });
615+
previousRev = settings._rev;
616+
}
617+
618+
const watcher = ignoreReload === 'sentinel' &&
619+
Object.keys(updates).length &&
620+
await waitForSentinelSettingsUpdateLogs();
621+
615622
await updateCustomSettings(updates);
623+
616624
if (!ignoreReload && !sync) {
617625
return await commonElements.closeReloadModal(true);
618626
}
627+
628+
if (previousRev && Object.keys(updates).length) {
629+
await waitForSettings(previousRev);
630+
}
631+
619632
if (watcher) {
620633
await watcher.promise;
621634
}
@@ -650,7 +663,12 @@ const revertCustomSettings = async () => {
650663
* @return {Promise} completion promise
651664
*/
652665
const revertSettings = async ignoreRefresh => {
653-
const watcher = ignoreRefresh && await waitForSettingsUpdateLogs();
666+
let previousRev;
667+
if (ignoreRefresh && ignoreRefresh !== 'sentinel') {
668+
const settings = await request({ path: '/api/v1/settings' });
669+
previousRev = settings._rev;
670+
}
671+
const watcher = ignoreRefresh === 'sentinel' && await waitForSentinelSettingsUpdateLogs();
654672
const needsRefresh = await revertCustomSettings();
655673

656674
if (!ignoreRefresh) {
@@ -662,7 +680,14 @@ const revertSettings = async ignoreRefresh => {
662680
return;
663681
}
664682

665-
await watcher.promise;
683+
if (previousRev) {
684+
await waitForSettings(previousRev);
685+
}
686+
687+
if (watcher) {
688+
await watcher.promise;
689+
}
690+
666691
return needsRefresh;
667692
};
668693

@@ -760,14 +785,24 @@ const revertDb = async (except = [], ignoreRefresh = true) => { //NOSONAR
760785
await deleteAllDocs(except);
761786
await revertTranslations();
762787
await deleteLocalDocs();
763-
const watcher = ignoreRefresh && await waitForSettingsUpdateLogs();
788+
789+
let previousRev;
790+
if (ignoreRefresh && ignoreRefresh !== 'sentinel') {
791+
const settings = await request({ path: '/api/v1/settings' });
792+
previousRev = settings._rev;
793+
}
794+
const watcher = ignoreRefresh === 'sentinel' && await waitForSentinelSettingsUpdateLogs();
795+
764796
const needsRefresh = await revertCustomSettings();
765797

766798
// only refresh if the settings were changed or modal was already present and we're not explicitly ignoring
767799
if (!ignoreRefresh && (needsRefresh || await hasModal())) {
768800
watcher?.cancel();
769801
await commonElements.closeReloadModal(true);
770802
} else if (needsRefresh) {
803+
if (previousRev) {
804+
await waitForSettings(previousRev);
805+
}
771806
watcher && await watcher.promise;
772807
} else {
773808
watcher?.cancel();

0 commit comments

Comments
 (0)