From cf2a3869040ed521fe7f703b55f97e2be29371de Mon Sep 17 00:00:00 2001 From: Cheton Wu Date: Fri, 22 Dec 2017 18:40:26 +0800 Subject: [PATCH] Setup tests --- package.json | 20 +++++++++++--------- setupTests.js | 33 +++++++++++++++++++++++++++++++++ test/index.js | 22 +++++++++++++++++++++- 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 setupTests.js diff --git a/package.json b/package.json index 4a4a8b2..867ac52 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ "clean": "rm -f {lib,dist}/*", "cleancss": "cleancss -o dist/react-sidenav.min.css dist/react-sidenav.css", "demo": "http-server -p 8000 docs/", - "lint": "eslint ./src", - "lint:fix": "eslint --fix ./src", + "lint": "eslint ./src ./test", "test": "tap test/*.js --node-arg=--require --node-arg=babel-register --node-arg=--require --node-arg=babel-polyfill", "coveralls": "tap test/*.js --coverage --coverage-report=text-lcov --nyc-arg=--require --nyc-arg=babel-register --nyc-arg=--require --nyc-arg=babel-polyfill | coveralls", "dev": "cd examples; webpack-dev-server --hot --inline --host 0.0.0.0 --port 8000 --content-base ../docs" @@ -69,27 +68,30 @@ "coveralls": "~3.0.0", "css-loader": "~0.28.7", "ensure-array": "~1.0.0", + "enzyme": "~3.2.0", + "enzyme-adapter-react-16": "~1.1.1", "eslint": "~4.13.1", - "eslint-config-trendmicro": "~1.1.0", + "eslint-config-trendmicro": "~1.2.0", "eslint-loader": "~1.9.0", "eslint-plugin-import": "~2.8.0", - "eslint-plugin-jsx-a11y": "~6.0.2", + "eslint-plugin-jsx-a11y": "~6.0.3", "eslint-plugin-react": "~7.5.1", "extract-text-webpack-plugin": "~3.0.2", - "file-loader": "~1.1.5", + "file-loader": "~1.1.6", "find-imports": "~0.5.2", "html-webpack-plugin": "~2.30.1", "http-server": "~0.10.0", + "jsdom": "~11.5.1", "nib": "~1.1.2", "react": "~16.2.0", "react-dom": "~16.2.0", - "style-loader": "~0.19.0", - "styled-components": "~2.2.4", + "style-loader": "~0.19.1", + "styled-components": "~2.3.3", "stylint": "~1.5.9", "stylint-loader": "~1.0.0", "stylus-loader": "~3.0.1", - "tap": "~11.0.0", - "trendmicro-ui": "~0.4.4", + "tap": "~11.0.1", + "trendmicro-ui": "~0.5.0", "url-loader": "~0.6.2", "webpack": "~3.10.0", "webpack-dev-server": "~2.9.7" diff --git a/setupTests.js b/setupTests.js new file mode 100644 index 0000000..3f25c29 --- /dev/null +++ b/setupTests.js @@ -0,0 +1,33 @@ +import Enzyme from 'enzyme'; +import Adapter from 'enzyme-adapter-react-16'; +import { JSDOM } from 'jsdom'; + +// React 16 Enzyme adapter +Enzyme.configure({ adapter: new Adapter() }); + +// Ignore `.styl` files +require.extensions['.styl'] = () => { + return; +}; + +// JSDOM +const jsdom = new JSDOM(''); +const { window } = jsdom; + +const copyProps = (src, target) => { + const props = Object.getOwnPropertyNames(src) + .filter(prop => typeof target[prop] === 'undefined') + .reduce((result, prop) => ({ + ...result, + [prop]: Object.getOwnPropertyDescriptor(src, prop), + }), {}); + Object.defineProperties(target, props); +}; + +global.window = window; +global.document = window.document; +global.navigator = { + userAgent: 'node.js', +}; + +copyProps(window, global); diff --git a/test/index.js b/test/index.js index 361cc49..9189d7a 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,25 @@ +import React from 'react'; +import { mount } from 'enzyme'; import { test } from 'tap'; +import '../setupTests'; +import SideNav, { Toggle, Nav, NavItem, NavIcon, NavText } from '../src'; -test('noop', (t) => { +test('', (t) => { + const wrapper = mount(( + + + + + )); + t.equal(wrapper.find(SideNav).length, 1, 'should render component'); t.end(); });