Skip to content

Commit

Permalink
Tests: toBeAborted expect extension [refactor]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Mar 30, 2019
1 parent e2055ee commit b5a3c65
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 18 deletions.
31 changes: 15 additions & 16 deletions test/lazy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

// Modules
const React = require('react'),
{Suspense} = React,
{ABORT} = require('../symbols');
{Suspense} = React;

// Imports
const {itRenders, lazy, removeSpacing, preventUnhandledRejection} = require('./utils');
Expand Down Expand Up @@ -1300,7 +1299,7 @@ describe('lazy component', () => {
const p = render(e);
preventUnhandledRejection(p);

expect(Lazy.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy).toBeAborted();

await expect(p).rejects.toThrow(NO_SUSPENSE_ERROR);
});
Expand Down Expand Up @@ -1336,7 +1335,7 @@ describe('lazy component', () => {
const p = render(e);
preventUnhandledRejection(p);

expect(Lazy.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy).toBeAborted();

await expect(p).rejects.toThrow(NO_SUSPENSE_ERROR);
});
Expand Down Expand Up @@ -1580,14 +1579,14 @@ describe('multiple lazy components', () => {
const p = render(e);
preventUnhandledRejection(p);

expect(Lazy1.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy2.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy1).toBeAborted();
expect(Lazy2).toBeAborted();
if (fallbackFast) {
expect(Lazy3).not.toHaveBeenCalled();
} else {
expect(Lazy3.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy3).toBeAborted();
}
expect(Lazy4.promise[ABORT]).not.toHaveBeenCalled();
expect(Lazy4).not.toBeAborted();

const h = await p;
expect(h).toBe(`<div${openTag}><span>Fallback</span><div>Lazy inner 4</div></div>`);
Expand Down Expand Up @@ -1806,10 +1805,10 @@ describe('nested lazy components', () => {

await Lazy3.promise;

expect(Lazy1.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy2.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy3.promise[ABORT]).not.toHaveBeenCalled();
expect(Lazy3Inner.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy1).toBeAborted();
expect(Lazy2).toBeAborted();
expect(Lazy3).not.toBeAborted();
expect(Lazy3Inner).toBeAborted();

const h = await p;

Expand Down Expand Up @@ -1837,10 +1836,10 @@ describe('nested lazy components', () => {
await Lazy1.promise;
await Lazy2.promise;

expect(Lazy1.promise[ABORT]).not.toHaveBeenCalled();
expect(Lazy1Inner.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy2.promise[ABORT]).not.toHaveBeenCalled();
expect(Lazy2Inner.promise[ABORT]).toHaveBeenCalledTimes(1);
expect(Lazy1).not.toBeAborted();
expect(Lazy1Inner).toBeAborted();
expect(Lazy2).not.toBeAborted();
expect(Lazy2Inner).toBeAborted();

const h = await p;

Expand Down
22 changes: 21 additions & 1 deletion test/utils/expectExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

// Modules
const {ON_MOUNT} = require('../../symbols');
const {ABORT, ON_MOUNT} = require('../../symbols');

// Imports
const {TEST_LAZY} = require('./symbols');
Expand All @@ -18,6 +18,26 @@ const {TEST_LAZY} = require('./symbols');
*/
const extensions = {};

extensions.toBeAborted = extensions.toAbort = function(Component) {
const message = `Expected ${componentName(Component)} ${this.isNot ? 'not ' : ''}to be aborted`;

// Check `[ABORT]` called correctly if was called
const {fail, mock, notCalled} = calledCorrectly(Component, ABORT);

// Make `expect(C).not.toBeAborted()` not error if `C` never called
if (notCalled && this.isNot) return failed(message);

if (fail) return passedFailed(this, message, fail);

// Check `[ABORT]` called no more than once
const {calls} = mock;
if (calls.length > 1) return passedFailed(this, message, `it was aborted ${calls.length} times`);

// Check aborted
if (mock.calls.length === 0) return failed(message);
return passed(message);
};

extensions.toBeMounted = extensions.toMount = function(Component) {
const message = mountBaseMessage(this, Component);

Expand Down
92 changes: 91 additions & 1 deletion test/utils/expectExtensions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,104 @@

// Modules
const React = require('react'),
{ON_MOUNT} = require('../../symbols');
{ABORT, ON_MOUNT} = require('../../symbols');

// Imports
const {lazy} = require('./index');

// Tests

describe('jest expect extensions (used in tests only)', () => {
describe('toBeAborted', () => {
it('passes if passed a correctly aborted lazy component', () => {
const Lazy = lazy(() => <div></div>);
const promise = tryCatch(Lazy);
promise[ABORT]();
expect(Lazy).toBeAborted();
});

it('errors if passed a non-function', () => {
expect(() => {
expect(null).toBeAborted();
}).toThrow(/^Expected component to be aborted - it is not a component$/);
});

it('errors if passed a function which is not a test lazy component', () => {
expect(() => {
expect(function() {}).toBeAborted();
}).toThrow(/^Expected component to be aborted - it is not a test lazy component$/);
});

it('errors if passed a component which was not called', () => {
const Lazy = lazy(() => <div></div>);
expect(() => {
expect(Lazy).toBeAborted();
}).toThrow(/^Expected Lazy to be aborted - it was not called$/);
});

it('errors if passed a component which was not aborted', () => {
const Lazy = lazy(() => <div></div>);
tryCatch(Lazy);
expect(() => {
expect(Lazy).toBeAborted();
}).toThrow(/^Expected Lazy to be aborted$/);
});

it('errors if passed a component which was aborted twice', () => {
const Lazy = lazy(() => <div></div>);
const promise = tryCatch(Lazy);
promise[ABORT]();
promise[ABORT]();
expect(() => {
expect(Lazy).toBeAborted();
}).toThrow(/^Expected Lazy to be aborted - it was aborted 2 times$/);
});
});

describe('not.toBeAborted', () => {
it('passes if passed a lazy component which was called but not aborted', () => {
const Lazy = lazy(() => <div></div>);
tryCatch(Lazy);
expect(Lazy).not.toBeAborted();
});

it('passes if passed a lazy component which was not called', () => {
const Lazy = lazy(() => <div></div>);
expect(Lazy).not.toBeAborted();
});

it('errors if passed a non-function', () => {
expect(() => {
expect(null).not.toBeAborted();
}).toThrow(/^Expected component not to be aborted - it is not a component$/);
});

it('errors if passed a function which is not a test lazy component', () => {
expect(() => {
expect(function() {}).not.toBeAborted();
}).toThrow(/^Expected component not to be aborted - it is not a test lazy component$/);
});

it('errors if passed a component which was aborted', () => {
const Lazy = lazy(() => <div></div>);
const promise = tryCatch(Lazy);
promise[ABORT]();
expect(() => {
expect(Lazy).not.toBeAborted();
}).toThrow(/^Expected Lazy not to be aborted$/);
});

it('errors if passed a component which was aborted twice', () => {
const Lazy = lazy(() => <div></div>);
const promise = tryCatch(Lazy);
promise[ABORT]();
promise[ABORT]();
expect(() => {
expect(Lazy).not.toBeAborted();
}).toThrow(/^Expected Lazy not to be aborted - it was aborted 2 times$/);
});
});

describe('toBeMounted', () => {
it('passes if passed a correctly mounted lazy component', () => {
const Lazy = lazy(() => <div></div>);
Expand Down

0 comments on commit b5a3c65

Please sign in to comment.