Skip to content

Commit

Permalink
test(compose): adds coverage for compose functions second arugment (c…
Browse files Browse the repository at this point in the history
…reate)
  • Loading branch information
rafamel committed Mar 28, 2019
1 parent 344fcd7 commit 8b94b12
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
16 changes: 12 additions & 4 deletions test/compose/cancellable.test.ts
Expand Up @@ -2,11 +2,19 @@ import mark from '~/helpers/mark';
import cancellable from '~/compose/cancellable';
import { ICancellable } from '~/types';

test(`should be marked as cancellable`, () => {
const p = Promise.resolve();
cancellable(p);
test(`returns new/mutated cancellable promise`, async () => {
expect.assertions(6);
const p = Promise.resolve('foo');

const m = cancellable(p);
const n = cancellable(p, true);

expect(mark.get(p, 'cancellable')).toBe(true);
expect(m).toBe(p);
expect(n).not.toBe(p);
await expect(m).resolves.toBe('foo');
await expect(n).resolves.toBe('foo');
expect(mark.get(m, 'cancellable')).toBe(true);
expect(mark.get(n, 'cancellable')).toBe(true);
});
test(`should have cancel/cancelled`, async () => {
expect.assertions(2);
Expand Down
15 changes: 11 additions & 4 deletions test/compose/deferrable.test.ts
Expand Up @@ -2,11 +2,18 @@ import mark from '~/helpers/mark';
import deferrable from '~/compose/deferrable';
import { IDeferrable } from '~/types';

test(`should be marked as deferrable`, () => {
const p = Promise.resolve();
deferrable(p);
test(`returns new/mutated deferrable promise`, async () => {
expect.assertions(6);
const p = Promise.resolve('foo');

expect(mark.get(p, 'deferrable')).toBe(true);
const m = deferrable(p);
const n = deferrable(p, true);
expect(m).toBe(p);
expect(n).not.toBe(p);
expect(mark.get(m, 'deferrable')).toBe(true);
expect(mark.get(n, 'deferrable')).toBe(true);
await expect(m).resolves.toBe('foo');
await expect(n).resolves.toBe('foo');
});
test(`Should have resolve/reject`, () => {
expect.assertions(2);
Expand Down
20 changes: 14 additions & 6 deletions test/compose/delay.test.ts
@@ -1,13 +1,21 @@
import delay from '~/compose/delay';

test(`delays when it's resolved`, async () => {
expect.assertions(3);

const init = Date.now();
test(`delays when it's resolved; returns new/mutated promise`, async () => {
expect.assertions(8);
let init = Date.now();
const p = Promise.resolve(10);
delay(200)(p);
const m = delay(200)(p);

await expect(p).resolves.toBe(10);
expect(m).toBe(p);
await expect(m).resolves.toBe(10);
expect(Date.now() - init).toBeGreaterThanOrEqual(200);
expect(Date.now() - init).toBeLessThan(400);

init = Date.now();
const n = delay(200)(p, true);

expect(n).not.toBe(p);
await expect(n).resolves.toBe(10);
expect(Date.now() - init).toBeGreaterThanOrEqual(200);
expect(Date.now() - init).toBeLessThan(400);
});
Expand Down
21 changes: 12 additions & 9 deletions test/compose/status.test.ts
Expand Up @@ -2,13 +2,19 @@ import status from '~/compose/status';
import mark from '~/helpers/mark';
import { IStatus } from '~/types';

test(`should be marked as status`, () => {
const p: IStatus & Promise<any> = Promise.resolve() as any;
status(p);

expect(mark.get(p, 'status')).toBe(true);
test(`returns new/mutated status promise`, async () => {
expect.assertions(6);
const p = Promise.resolve('foo');

const m = status(p);
const n = status(p, true);
expect(m).toBe(p);
expect(n).not.toBe(p);
expect(mark.get(m, 'status')).toBe(true);
expect(mark.get(n, 'status')).toBe(true);
await expect(m).resolves.toBe('foo');
await expect(n).resolves.toBe('foo');
});

test(`initializes correctly`, () => {
const p: IStatus & Promise<any> = Promise.resolve() as any;
status(p);
Expand All @@ -17,7 +23,6 @@ test(`initializes correctly`, () => {
expect(p.value).toBe(null);
expect(p.reason).toBe(null);
});

test(`sets status on resolve`, async () => {
expect.assertions(4);
const p: IStatus & Promise<any> = Promise.resolve(10) as any;
Expand All @@ -28,7 +33,6 @@ test(`sets status on resolve`, async () => {
expect(p.value).toBe(10);
expect(p.reason).toBe(null);
});

test(`sets status on reject`, async () => {
expect.assertions(4);
// eslint-disable-next-line prefer-promise-reject-errors
Expand All @@ -40,7 +44,6 @@ test(`sets status on reject`, async () => {
expect(p.value).toBe(null);
expect(p.reason).toBe(10);
});

test(`doesn't run again`, async () => {
expect.assertions(3);

Expand Down
17 changes: 12 additions & 5 deletions test/compose/timed.test.ts
Expand Up @@ -2,11 +2,18 @@ import timed from '~/compose/timed';
import mark from '~/helpers/mark';
import { ITimed } from '~/types';

test(`is marked as timed`, () => {
const p: ITimed & Promise<any> = Promise.resolve() as any;
timed(p);

expect(mark.get(p, 'timed')).toBe(true);
test(`returns new/mutated timed promise`, async () => {
expect.assertions(6);
const p = Promise.resolve('foo');

const m = timed(p);
const n = timed(p, true);
expect(m).toBe(p);
expect(n).not.toBe(p);
expect(mark.get(m, 'timed')).toBe(true);
expect(mark.get(n, 'timed')).toBe(true);
await expect(m).resolves.toBe('foo');
await expect(n).resolves.toBe('foo');
});
test(`has promise.time`, () => {
const p: ITimed & Promise<any> = Promise.resolve() as any;
Expand Down
16 changes: 16 additions & 0 deletions test/compose/timeout.test.ts
@@ -1,5 +1,21 @@
import mark from '~/helpers/mark';
import timeout from '~/compose/timeout';

test(`returns new/mutated promise`, async () => {
expect.assertions(8);
const p = Promise.resolve('foo');
const m = timeout(100)(p);
const n = timeout(100)(p, true);

expect(m).toBe(p);
expect(n).not.toBe(p);
await expect(m).resolves.toBe('foo');
await expect(n).resolves.toBe('foo');
expect(mark.get(m, 'cancellable')).toBe(true);
expect(mark.get(n, 'cancellable')).toBe(true);
expect(mark.get(m, 'deferrable')).toBe(true);
expect(mark.get(n, 'deferrable')).toBe(true);
});
test(`cancels on timeout wo/ reason`, async () => {
expect.assertions(1);
const p = new Promise((resolve) => setTimeout(() => resolve(10), 200));
Expand Down

0 comments on commit 8b94b12

Please sign in to comment.