Skip to content

Commit

Permalink
tests: added deferred tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 13, 2021
1 parent 3f0e022 commit d09b3ad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/util/deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export type MaybePromise<T> = T | PromiseLike<T>;
/**
* Represents the completion of an asynchronous operation that can be completed later.
*/
export class Deferred<T> implements PromiseLike<T> {
readonly promise: Promise<T>;
export class Deferred<T = any> implements PromiseLike<T> {
private readonly promise: Promise<T>;
private _resolve: (value: MaybePromise<T>) => void = () => {};
private _reject: (reason?: any) => void = () => {};

Expand Down
52 changes: 52 additions & 0 deletions test/util/deferred.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Deferred } from '../../src/util/deferred';

describe('Tests cached status code', () => {
it('test resolve method', () => {
const deferred = new Deferred();

expect(deferred).resolves.toBe(1);
deferred.resolve(1);
});

it('test reject method', () => {
const deferred = new Deferred();

expect(deferred).rejects.toBe(1);
deferred.reject(1);
});

it('test then method', () => {
const deferred = new Deferred();

deferred.then((data) => {
expect(data).toBe(1);
});

deferred.resolve(1);
});

it('test catch method', () => {
const deferred = new Deferred();

deferred.catch((data) => {
expect(data).toBe(1);
});

deferred.resolve(1);
});

it('test finally method', () => {
const deferred = new Deferred<number>();

let data: number;
deferred.then((d) => {
data = d;
});

deferred.finally(() => {
expect(data).toBe(1);
});

deferred.resolve(1);
});
});

0 comments on commit d09b3ad

Please sign in to comment.