Add Enzyme to www and add a test for the NextStepButton. This takes our enzyme helpers from gui and copies them here. It also requires a babel plugin to play nicely with jest.

This commit is contained in:
picklesrus 2019-07-31 17:35:57 -04:00
parent 1da17e4035
commit 0e09e29c80
8 changed files with 3942 additions and 3547 deletions

View File

@ -1,6 +1,7 @@
{
"plugins": [
"transform-object-rest-spread"
"transform-object-rest-spread",
"transform-require-context"
],
"presets": ["es2015", "react"],
}

7392
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -61,6 +61,7 @@
"babel-eslint": "10.0.2",
"babel-loader": "7.1.0",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-plugin-transform-require-context": "^0.1.1",
"babel-preset-es2015": "6.22.0",
"babel-preset-react": "6.22.0",
"bowser": "1.9.4",
@ -71,6 +72,8 @@
"copy-webpack-plugin": "0.2.0",
"create-react-class": "15.6.2",
"css-loader": "0.23.1",
"enzyme": "3.10.0",
"enzyme-adapter-react-16": "1.14.0",
"eslint": "5.16.0",
"eslint-config-scratch": "5.0.0",
"eslint-plugin-cypress": "^2.0.1",
@ -133,6 +136,16 @@
"webpack-dev-middleware": "2.0.4",
"xhr": "2.2.0"
},
"jest": {
"setupFiles": [
"<rootDir>/test/helpers/enzyme-setup.js"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/__mocks__/fileMock.js",
"\\.(css|less|scss)$": "<rootDir>/test/__mocks__/styleMock.js"
}
},
"nyc": {
"include": [
"bin/**/*.js",

View File

@ -0,0 +1,3 @@
// __mocks__/fileMock.js
module.exports = 'test-file-stub';

View File

@ -0,0 +1,3 @@
// __mocks__/styleMock.js
module.exports = {};

View File

@ -0,0 +1,4 @@
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});

View File

@ -0,0 +1,39 @@
/*
* Helpers for using enzyme and react-test-renderer with react-intl
* Directly from https://github.com/yahoo/react-intl/wiki/Testing-with-React-Intl
*/
import React from 'react';
import renderer from 'react-test-renderer';
import {IntlProvider, intlShape} from 'react-intl';
import {mount, shallow} from 'enzyme';
const intlProvider = new IntlProvider({locale: 'en'}, {});
const {intl} = intlProvider.getChildContext();
const nodeWithIntlProp = node => React.cloneElement(node, {intl});
const shallowWithIntl = (node, {context} = {}) => shallow(
nodeWithIntlProp(node),
{
context: Object.assign({}, context, {intl})
}
);
const mountWithIntl = (node, {context, childContextTypes} = {}) => mount(
nodeWithIntlProp(node),
{
context: Object.assign({}, context, {intl}),
childContextTypes: Object.assign({}, {intl: intlShape}, childContextTypes)
}
);
// react-test-renderer component for use with snapshot testing
const componentWithIntl = (children, props = {locale: 'en'}) => renderer.create(
<IntlProvider {...props}>{children}</IntlProvider>
);
export {
componentWithIntl,
shallowWithIntl,
mountWithIntl
};

View File

@ -0,0 +1,32 @@
import React from 'react';
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
import NextStepButton from '../../../src/components/join-flow/next-step-button';
import Spinner from '../../../src/components/spinner/spinner.jsx';
describe('NextStepButton', () => {
const defaultProps = () => ({
text: 'I am a button',
waiting: false
});
test('testing spinner does not show and button enabled', () => {
const component = mountWithIntl(
<NextStepButton
{...defaultProps()}
/>
);
expect(component.find(Spinner).exists()).toEqual(false);
expect(component.find('button[type="submit"]').prop('disabled')).toBe(false);
});
test('testing spinner does show and button disabled', () => {
const component = mountWithIntl(
<NextStepButton
{...defaultProps()}
/>
);
component.setProps({waiting: true});
expect(component.find(Spinner).exists()).toEqual(true);
expect(component.find('button[type="submit"]').prop('disabled')).toBe(true);
});
});