Skip to content

Commit

Permalink
СB-5198 adds unit tests for CancellablePromise and cancellableTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyteleshev committed May 30, 2024
1 parent a23f95d commit 94cce1a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 36 deletions.
52 changes: 52 additions & 0 deletions webapp/packages/core-utils/src/Promises/CancellablePromise.test.ts
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 webapp/packages/core-utils/src/Promises/cancellableTimeout.test.ts
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 webapp/packages/core-utils/src/Promises/cancellableTimeout.ts
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);
};
});
}
35 changes: 0 additions & 35 deletions webapp/packages/core-utils/src/Promises/deferPromise.ts

This file was deleted.

2 changes: 1 addition & 1 deletion webapp/packages/core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* you may not use this file except in compliance with the License.
*/
export * from './Promises/CancellablePromise';
export * from './Promises/deferPromise';
export * from './Promises/cancellableTimeout';
export * from './Promises/Deferred';
export * from './Promises/PromiseCancelledError';
export * from './Promises/PromiseExecutor';
Expand Down

0 comments on commit 94cce1a

Please sign in to comment.