Skip to content

Commit

Permalink
chore(ffe-checkbox-react): Migrate tests to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristofer Selbekk committed Feb 22, 2018
1 parent ad31b46 commit f5935ff
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 176 deletions.
10 changes: 10 additions & 0 deletions packages/ffe-checkbox-react/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"overrides": [
{
"files": [ "src/**/*.spec.js"],
"env": {
"jest": true
}
}
]
}
6 changes: 0 additions & 6 deletions packages/ffe-checkbox-react/example.html

This file was deleted.

23 changes: 9 additions & 14 deletions packages/ffe-checkbox-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"author": "SpareBank 1",
"license": "MIT",
"scripts": {
"build": "babel -d lib/. --ignore=*.test.js src/.",
"build": "babel -d lib/. --ignore=*.spec.js src/.",
"lint": "eslint src/.",
"test:nsp": "nsp check",
"test:spec": "mocha --require babel-register --require ./test-setup.js src/**/*.test.js",
"test": "npm run test:spec && npm run test:nsp",
"tdd": "mocha --require babel-register src/**/*.test.js -w"
"test:spec": "jest",
"test:watch": "jest --watch",
"test": "npm run test:spec && npm run test:nsp"
},
"peerDependencies": {
"ffe-form": "^8.1.5"
Expand All @@ -29,19 +29,14 @@
"uuid": "^3.1.0"
},
"devDependencies": {
"chai": "^4.1.2",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.13.1",
"ffe-core": "^11.0.2",
"ffe-form": "^8.1.11",
"mocha": "^4.1.0",
"mocha-tap13": "^1.0.2",
"jest": "^22.4.0",
"nsp": "^3.1.0",
"onchange": "3.2.1",
"react": "^16.1.1",
"react-addons-test-utils": "^15.0.1",
"react-dom": "^16.1.1",
"sinon": "^4.1.3"
"react-dom": "^16.1.1"
},
"jest": {
"setupTestFrameworkScriptFile": "./test-setup.js"
}
}
109 changes: 109 additions & 0 deletions packages/ffe-checkbox-react/src/Checkbox.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react';
import { shallow } from 'enzyme';

import Checkbox from './Checkbox';

const getWrapper = props => shallow(<Checkbox {...props} />);

describe('<Checkbox />', () => {
it('should render a input', () => {
const wrapper = getWrapper();
expect(wrapper.find('input').exists()).toBe(true);
});

it('should call onChange when clicked', () => {
const spy = jest.fn();
const wrapper = getWrapper({ onChange: spy });

wrapper.find('input').simulate('change');

expect(spy).toHaveBeenCalledTimes(1);
});

it('should call onChange with the correct parameter', () => {
const mockEvent = { target: { value: true } };
const spy = jest.fn();

const wrapper = getWrapper({ onChange: spy });

wrapper.find('input').simulate('change', mockEvent);

expect(spy.mock.calls[0][0]).toBe(mockEvent);
});

it('should render a default value if passed', () => {
let wrapper = getWrapper();

expect(wrapper.find('input').prop('checked')).toBe(undefined);

wrapper = getWrapper({ checked: true });

expect(wrapper.find('input').prop('checked')).toBe(true);
});

it('should apply the same id to <label> and <input>', () => {
const wrapper = getWrapper({ name: 'Some text goes here' });

expect(wrapper.find('label').prop('htmlFor')).toBe(
wrapper.find('input').prop('id'),
);
});

it('should support noMargins', () => {
const wrapper = getWrapper({ noMargins: false });

expect(wrapper.find('.ffe-checkbox--no-margin').exists()).toBe(false);

wrapper.setProps({ noMargins: true });

expect(wrapper.find('.ffe-checkbox--no-margin').exists()).toBe(true);
});

it('should support inline', () => {
const wrapper = getWrapper();

expect(wrapper.find('.ffe-checkbox--inline').exists()).toBe(true);

wrapper.setProps({ inline: false });

expect(wrapper.find('.ffe-checkbox--inline').exists()).toBe(false);

wrapper.setProps({ inline: true });

expect(wrapper.find('.ffe-checkbox--inline').exists()).toBe(true);
});

it('should support invalid', () => {
const wrapper = getWrapper({ invalid: false });

expect(wrapper.find('input').prop('aria-invalid')).toBe('false');

wrapper.setProps({ invalid: true });

expect(wrapper.find('input').prop('aria-invalid')).toBe('true');
});

it('setting "aria-invalid" should override "invalid"', () => {
const wrapper = getWrapper({ invalid: true, 'aria-invalid': 'false' });

expect(wrapper.find('input').prop('aria-invalid')).toBe('false');

wrapper.setProps({ invalid: false, 'aria-invalid': 'true' });

expect(wrapper.find('input').prop('aria-invalid')).toBe('true');
});

it('should set arbitrary props (rest) on input', () => {
const wrapper = getWrapper({
name: 'checkbox',
iDontReallyDoAnything: 'false',
tabIndex: -1,
});

expect(wrapper.find('input').prop('name')).toBe('checkbox');
expect(wrapper.find('input').prop('iDontReallyDoAnything')).toBe(
'false',
);
expect(wrapper.find('input').prop('tabIndex')).toBe(-1);
});
});
156 changes: 0 additions & 156 deletions packages/ffe-checkbox-react/src/Checkbox.test.js

This file was deleted.

0 comments on commit f5935ff

Please sign in to comment.