Skip to content

Commit

Permalink
Added tests for promise rejection
Browse files Browse the repository at this point in the history
  • Loading branch information
the-bay-kay committed Oct 31, 2023
1 parent 772f508 commit aa34ac3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions www/__tests__/unifiedDataLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ describe('combineWithDedup can', () => {
const promiseGenerator = (values: Array<ServerData<any>>) => {
return Promise.resolve(values);
};
const badPromiseGenerator = (input: string) => {
return Promise.reject(input);
}

it('throws an error on an empty input', async () => {
expect(() => {
combinedPromises([], combineWithDedup);
}).toThrow();
});

it('catches when all promises fails', async () => {
expect(combinedPromises([badPromiseGenerator('')], combineWithDedup)).rejects.toEqual(['']);
expect(combinedPromises([badPromiseGenerator('bad'), badPromiseGenerator('promise')], combineWithDedup)).rejects.toEqual(['bad','promise']);
expect(combinedPromises([badPromiseGenerator('very'), badPromiseGenerator('bad'), badPromiseGenerator('promise')], combineWithDedup)).rejects.toEqual(['very','bad','promise']);

expect(combinedPromises([badPromiseGenerator('bad'), promiseGenerator([testOne])], combineWithDedup)).resolves.toEqual([testOne]);
expect(combinedPromises([promiseGenerator([testOne]), badPromiseGenerator('bad')], combineWithDedup)).resolves.toEqual([testOne]);
});

it('work with arrays of len 1', async () => {
const promiseArrayOne = [promiseGenerator([testOne])];
const promiseArrayTwo = [promiseGenerator([testOne, testTwo])];
Expand Down
2 changes: 1 addition & 1 deletion www/js/services/unifiedDataLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const combinedPromises = function(promiseList: Array<Promise<any>>,
const checkAndResolve = function() {
if (firstPromiseDone && nextPromiseDone) {
if (firstError && nextError) {
reject([firstError, nextError]);
reject([firstError].concat(nextError));
} else {
logDebug(`About to dedup firstResult = ${firstResult.length}` +
` nextResult = ${nextResult.length}`);
Expand Down

0 comments on commit aa34ac3

Please sign in to comment.