-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
СB-5198 adds unit tests for CancellablePromise and cancellableTimeout
- Loading branch information
1 parent
a23f95d
commit 94cce1a
Showing
5 changed files
with
97 additions
and
36 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
webapp/packages/core-utils/src/Promises/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,52 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { CancellablePromise } from './CancellablePromise'; | ||
import { PromiseCancelledError } from './PromiseCancelledError'; | ||
|
||
describe('CancellablePromise', () => { | ||
jest.mock('./PromiseCancelledError', () => ({ | ||
PromiseCancelledError: jest.fn(), | ||
})); | ||
|
||
it('cancels promise', async () => { | ||
const PromiseCancelledErrorMockInstance = new PromiseCancelledError(); | ||
const promise = new CancellablePromise<void>(resolve => { | ||
const token = setTimeout(() => resolve(), 0); | ||
return () => { | ||
clearTimeout(token); | ||
}; | ||
}); | ||
|
||
promise.cancel(); | ||
|
||
await expect(promise).rejects.toThrow(PromiseCancelledErrorMockInstance); | ||
}); | ||
|
||
it('should resolve promise', async () => { | ||
const promise = new CancellablePromise<number>(resolve => { | ||
const token = setTimeout(() => resolve(777), 0); | ||
return () => { | ||
clearTimeout(token); | ||
}; | ||
}); | ||
|
||
await expect(promise).resolves.toBe(777); | ||
}); | ||
|
||
it('should reject promise', async () => { | ||
const error = new Error('test'); | ||
const promise = new CancellablePromise<number>((resolve, reject) => { | ||
const token = setTimeout(() => reject(error), 0); | ||
return () => { | ||
clearTimeout(token); | ||
}; | ||
}); | ||
|
||
await expect(promise).rejects.toThrow(error); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
webapp/packages/core-utils/src/Promises/cancellableTimeout.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,27 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { cancellableTimeout } from './cancellableTimeout'; | ||
|
||
jest.mock('./CancellablePromise', () => ({ | ||
CancellablePromise: jest.fn().mockImplementation(() => ({ | ||
cancel: jest.fn(), | ||
})), | ||
})); | ||
|
||
describe('cancellableTimeout', () => { | ||
it('resolves after the specified timeout', async () => { | ||
const timeout = 0; | ||
const start = Date.now(); | ||
|
||
const promise = cancellableTimeout(timeout); | ||
|
||
await promise; | ||
|
||
expect(Date.now() - start).toBe(timeout); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
webapp/packages/core-utils/src/Promises/cancellableTimeout.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,17 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { CancellablePromise } from './CancellablePromise'; | ||
|
||
export function cancellableTimeout(timeout: number): CancellablePromise<void> { | ||
return new CancellablePromise<void>(resolve => { | ||
const token = setTimeout(() => resolve(), timeout); | ||
return () => { | ||
clearTimeout(token); | ||
}; | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
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