Skip to content

Commit

Permalink
Merge pull request #1013 from ishqelliot/feature/mathesar_ui/issues-290
Browse files Browse the repository at this point in the history
add test for filterUtils and CancellablePromise
  • Loading branch information
seancolsen committed Jan 25, 2022
2 parents 2741385 + ea07f72 commit 53b0c3d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import CancellablePromise from '../CancellablePromise';

const dummyOnCancel = jest.fn().mockReturnValueOnce('cancelled');
test('CancellablePromise cancelled', () => {
const fakePromise = new CancellablePromise((resolve, reject) => {
try {
resolve(202);
} catch (e) {
reject('Rejected: error');
}
}, dummyOnCancel);
expect(fakePromise.isCancelled).toBeFalsy();
fakePromise.cancel();
expect(fakePromise.isCancelled).toBeTruthy();
expect(dummyOnCancel).toHaveBeenCalledTimes(1);
expect(fakePromise.then((res) => res)).toEqual(null);
expect(fakePromise.catch(() => {})).toEqual(null);
expect(fakePromise.finally(() => {})).toEqual(null);
});

test('CancellablePromise not cancelled gets resolved', async () => {
const fakePromise = new CancellablePromise((resolve, reject) => {
try {
resolve(202);
} catch (e) {
reject('Rejected: error');
}
}, dummyOnCancel);
expect(fakePromise.isCancelled).toBeFalsy();
await expect(fakePromise.then((res) => res)).resolves.toBe(202);
});

test('CancellablePromise not cancelled gets rejected', async () => {
const fakePromise = new CancellablePromise((resolve, reject) => {
reject('Rejected: error');
}, dummyOnCancel);
expect(fakePromise.isCancelled).toBeFalsy();
await expect(fakePromise.then((res) => res)).rejects.toEqual(
'Rejected: error',
);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImmutableSet } from './ImmutableSet';
import { ImmutableSet } from '../ImmutableSet';

test('ImmutableSet', () => {
const s = new ImmutableSet<number>([7, 8]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { filterTree } from '../filterUtils';

const dummyData = [
{
alevel1Key1: 'level1Value1',
targetChildKey: [
{
level2Key1: 'level2Value1',
targetChildKey: [
{
level3Key1: 'target',
},
{
nonSearchNonChildEntry: 'someValue',
},
],
},
],
},
{
blevel1Key1: 'level1Value1',
targetChildKey: [
{
level2Key1: 'level2Value1',
targetChildKey: [
{
level3Key1: 'randomValue',
},
],
},
],
},
];

const testRes = [
{
alevel1Key1: 'level1Value1',
targetChildKey: [
{
level2Key1: 'level2Value1',
targetChildKey: [
{
level3Key1: 'target',
},
],
},
],
},
];
test('filterUtils', () => {
expect(filterTree(dummyData, 'level3Key1', 'targetChildKey', 'get')).toEqual(
testRes,
);
expect(filterTree(dummyData, 'level3Key1', 'targetChildKey', '')).toEqual(
dummyData,
);
});

0 comments on commit 53b0c3d

Please sign in to comment.