fusion-test-utils
Provides test utility functions for FusionJS
yarn add fusion-test-utils
Example
import App from 'fusion-core';
import {render, request} from 'fusion-test-utils';
// test renders of your application
const app = new App();
const ctx = await render(app, '/test-url', {
headers: {
'x-header': 'value',
}
});
// do assertions on ctx
// test requests to your application
const app = new App();
const ctx = await request(app, '/test-url', {
headers: {
'x-header': 'value',
}
});
// do assertions on ctx
API
request(app: FusionApp, url: String, options: ?Object)
=> Promise
Simulates a request through your application.
app
- instance of a FusionApp
url
- path for request
options
- optional object containing custom settings for the request
options.method
- the request method, e.g., GET, POST,
options.headers
- headers to be added to the request
options.body
- body for the request
render(app: FusionApp, url: String, options: ?Object)
=> Promise
This is the same as request
, but defaults the accept
header to text/html
which will trigger a render of your application.
test(testName: String, executor: (assert) => {})
A block which executes a test case when using fusion-cli as a test runner. The first argument is the name of the test, and the second argument is a function that executes your test code. The test case will receive a cross-environment assertion helper with all methods defined in the assert module, as well as a .matchSnapshot()
method.
Example usage:
import React from 'react';
import {test} from 'fusion-test-utils';
import {shallow} from 'enzyme';
import MyComponent from '../my-component';
test('MyComponent snapshot', assert => {
const wrapper = shallow(<MyComponent />);
assert.matchSnapshot(wrapper);
});
test('async functions', async assert => {
const value = await doSomething();
assert.equal(true, value, 'something is equal to true');
});
mockFunction()
Returns a mock function which allows you to inspect the mock state via the .mock property. Example usage:
import {mockFunction, test} from '../index';
test('function mocks', assert => {
const myMock = mockFunction();
myMock();
assert.equal(myMock.mock.calls.length, 1);
});