Skip to content
This repository has been archived by the owner. It is now read-only.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.buildkite Implement CI for multiple node versions (#177) Oct 25, 2018
.github Add issue template (#87) Feb 16, 2018
docs/migrations Update API to use simulate Dec 4, 2017
flow-fixtures Add createRequestContext and createRenderContext functions Nov 16, 2018
flow-typed Export tape for typing usage May 22, 2018
src Fix mock context for headers in Node 10 Feb 25, 2019
.babelrc Upgrade dependencies Mar 2, 2019
.codecov.yml
.eslintrc.js Add initial implementation (#3) Nov 7, 2017
.flowconfig Implement wrapper in `fusion-test-utils` to encapsulate plugin resolu… Jan 16, 2018
.gitignore Add initial implementation (#3) Nov 7, 2017
.npmrc
Dockerfile Implement CI for multiple node versions (#177) Oct 25, 2018
LICENSE Add initial implementation (#3) Nov 7, 2017
README.md
docker-compose.yml Implement CI for multiple node versions (#177) Oct 25, 2018
package.json Release v1.4.1 (#215) Mar 6, 2019
renovate.json
yarn.lock Upgrade dependencies Mar 2, 2019

README.md

fusion-test-utils

Build status

Provides test utility functions for Fusion.js


yarn add fusion-test-utils

Example

import App from 'fusion-core';
import {getSimulator} from 'fusion-test-utils';

// create simulator
const app = new App();
const simulator = getSimulator(app /*, (optional) test plugin with assertions on dependencies */);

// test renders of your application
const ctx = await simulator.render('/test-url', {
  headers: {
    'x-header': 'value',
  }
});
// do assertions on ctx

// test requests to your application
const ctx = await simulator.request('/test-url', {
  headers: {
    'x-header': 'value',
  }
});
// do assertions on ctx

API

getSimulator(app: FusionApp, testPlugin?: FusionPlugin) => { request, render, getService }

Creates a simulator which exposes functionality to simulate requests and renders through your application. app - instance of a FusionApp testPlugin - optional plugin to make assertions on dependencies

getSimulator(...).request(url: String, options: ?Object) => Promise

Simulates a request through your application. 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

getSimulator(...).render(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.

getSimulator(...).getService(token: Token<T>) => T

This is a useful utility for testing the API provided by a plugin. For example:

const app = new App(el, el => el);
app.register(LoggerToken, LoggerPlugin);
const sim = getSimulator(app);
const logger = sim.getService(LoggerToken);

createRequestContext(url: String, options: ?Object) => Context

Creates a mock context object. 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

createRenderContext(url: String, options: ?Object) => Context

The same as createRequestContext, but defaults the accept header to text/html which will trigger a render of the 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);
  // And optionally, you can pass your own snapshot name as the second argument
  assert.matchSnapshot(wrapper, 'my snapshot description');
});

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);
});
You can’t perform that action at this time.