-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from ishqelliot/feature/mathesar_ui/issues-290
add test for filterUtils and CancellablePromise
- Loading branch information
Showing
3 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
mathesar_ui/src/component-library/common/utils/__tests__/CancellablePromise.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
}); |
2 changes: 1 addition & 1 deletion
2
...library/common/utils/ImmutableSet.test.ts → ...mmon/utils/__tests__/ImmutableSet.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
mathesar_ui/src/component-library/common/utils/__tests__/filterUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); |