diff --git a/www/__tests__/unifiedDataLoader.test.ts b/www/__tests__/unifiedDataLoader.test.ts index c814997a9..db0994519 100644 --- a/www/__tests__/unifiedDataLoader.test.ts +++ b/www/__tests__/unifiedDataLoader.test.ts @@ -4,36 +4,95 @@ import { ServerDataPoint } from '../js/types/diaryTypes'; mockLogger(); -describe('combineWithDedup can', () => { - const testOne : ServerDataPoint = { - data: '', - metadata: { - key: '', - platform: '', - write_ts: 1, // the only value checked by combineWithDedup - time_zone: '', - write_fmt_time: '', - write_local_dt: null, - }, - }; - const testTwo = JSON.parse(JSON.stringify(testOne)); - testTwo.metadata.write_ts = 2; - const testThree = JSON.parse(JSON.stringify(testOne)); - testThree.metadata.write_ts = 3; +const testOne : ServerDataPoint = { + data: '', + metadata: { + key: '', + platform: '', + write_ts: 1, // the only value checked by combineWithDedup + time_zone: '', + write_fmt_time: '', + write_local_dt: null, + }, +}; + +const testTwo = JSON.parse(JSON.stringify(testOne)); +testTwo.metadata.write_ts = 2; +const testThree = JSON.parse(JSON.stringify(testOne)); +testThree.metadata.write_ts = 3; +const testFour= JSON.parse(JSON.stringify(testOne)); +testFour.metadata.write_ts = 4; +describe('combineWithDedup can', () => { it('work with empty arrays', () => { expect(combineWithDedup([], [])).toEqual([]); expect(combineWithDedup([], [testOne])).toEqual([testOne]); expect(combineWithDedup([testOne, testTwo], [])).toEqual([testOne, testTwo]); }); - it('Can work with arrays of len 1', () => { + it('work with arrays of len 1', () => { expect(combineWithDedup([testOne], [testOne])).toEqual([testOne]); expect(combineWithDedup([testOne], [testTwo])).toEqual([testOne, testTwo]); }); - it('Can work with arrays of len > 1', () => { + it('work with arrays of len > 1', () => { expect(combineWithDedup([testOne], [testOne, testTwo])).toEqual([testOne, testTwo]); expect(combineWithDedup([testOne], [testTwo, testTwo])).toEqual([testOne, testTwo]); expect(combineWithDedup([testOne, testTwo], [testTwo, testTwo])).toEqual([testOne, testTwo]); expect(combineWithDedup([testOne, testTwo, testThree], [testOne, testTwo])).toEqual([testOne, testTwo, testThree]); }); }); + +// combinedPromises tests +const promiseGenerator = (values: Array) => { + return Promise.resolve(values); +}; + +it('throws an error on an empty input', async () => { + expect(() => {combinedPromises([], combineWithDedup)}).toThrow(); +}); + +it('work with arrays of len 1', async () => { + const promiseArrayOne = [promiseGenerator([testOne])]; + const promiseArrayTwo = [promiseGenerator([testOne, testTwo])]; + const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup); + const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup); + + expect(testResultOne).toEqual([testOne]); + expect(testResultTwo).toEqual([testOne, testTwo]); +}); + +it('works with arrays of len 2', async () => { + const promiseArrayOne = [promiseGenerator([testOne]), promiseGenerator([testTwo])]; + const promiseArrayTwo = [promiseGenerator([testOne, testTwo]), promiseGenerator([testThree])]; + const promiseArrayThree = [promiseGenerator([testOne]), promiseGenerator([testTwo, testThree])]; + const promiseArrayFour = [promiseGenerator([testOne, testTwo]), promiseGenerator([testThree, testFour])]; + const promiseArrayFive = [promiseGenerator([testOne, testTwo]), promiseGenerator([testTwo, testThree])]; + + const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup); + const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup); + const testResultThree = await combinedPromises(promiseArrayThree, combineWithDedup); + const testResultFour = await combinedPromises(promiseArrayFour, combineWithDedup); + const testResultFive = await combinedPromises(promiseArrayFive, combineWithDedup); + + expect(testResultOne).toEqual([testOne, testTwo]); + expect(testResultTwo).toEqual([testOne, testTwo, testThree]); + expect(testResultThree).toEqual([testOne, testTwo, testThree]); + expect(testResultFour).toEqual([testOne, testTwo, testThree, testFour]); + expect(testResultFive).toEqual([testOne, testTwo, testThree]); +}); + +it('works with arrays of len >= 2', async () => { + const promiseArrayOne = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testThree])]; + const promiseArrayTwo = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testTwo])]; + const promiseArrayThree = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testThree, testFour])]; + const promiseArrayFour = [promiseGenerator([testOne]), promiseGenerator([testTwo, testThree]), promiseGenerator([testFour])]; + + const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup); + const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup); + const testResultThree = await combinedPromises(promiseArrayThree, combineWithDedup); + const testResultFour = await combinedPromises(promiseArrayFour, combineWithDedup); + + expect(testResultOne).toEqual([testOne, testTwo, testThree]); + expect(testResultTwo).toEqual([testOne, testTwo]); + expect(testResultThree).toEqual([testOne, testTwo, testThree, testFour]); + expect(testResultFour).toEqual([testOne, testTwo, testThree, testFour]); +}); diff --git a/www/js/unifiedDataLoader.ts b/www/js/unifiedDataLoader.ts index f2e8c672c..f7036663a 100644 --- a/www/js/unifiedDataLoader.ts +++ b/www/js/unifiedDataLoader.ts @@ -1,4 +1,4 @@ -import { logInfo } from './plugin/logger' +import { logDebug } from './plugin/logger' import { getRawEntries } from './commHelper'; import { ServerDataPoint, ServerData, TimeQuery } from './types/diaryTypes' @@ -8,7 +8,7 @@ import { ServerDataPoint, ServerData, TimeQuery } from './types/diaryTypes' * @param list2 same as list1 * @returns a dedup array generated from the input lists */ -const combineWithDedup = function(list1: Array, list2: Array) { +export const combineWithDedup = function(list1: Array, list2: Array) { const combinedList = list1.concat(list2); return combinedList.filter(function(value, i, array) { const firstIndexOfValue = array.findIndex(function(element) { @@ -24,8 +24,11 @@ const combineWithDedup = function(list1: Array, list2: Array>, +export const combinedPromises = function(promiseList: Array>, combiner: (list1: Array, list2: Array) => Array ) { + if (promiseList.length === 0) { + throw new RangeError('combinedPromises needs input array.length >= 1'); + } return new Promise(function(resolve, reject) { var firstResult = []; var firstError = null; @@ -41,11 +44,11 @@ const combinedPromises = function(promiseList: Array>, if (firstError && nextError) { reject([firstError, nextError]); } else { - logInfo("About to dedup localResult = "+firstResult.length + logDebug("About to dedup localResult = "+firstResult.length +"remoteResult = "+nextResult.length); const dedupedList = combiner(firstResult, nextResult); - logInfo("Deduped list = "+dedupedList.length); + logDebug("Deduped list = "+dedupedList.length); resolve(dedupedList); } }