Use enzyme for all React component tests

This commit is contained in:
Nicolas Gallagher
2016-07-06 15:26:32 -07:00
parent 26758e905c
commit 331c92fb3a
5 changed files with 130 additions and 200 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
"dist" "dist"
], ],
"scripts": { "scripts": {
"build": "del ./dist && mkdir dist && babel src -d dist --ignore **/__tests__,src/modules/specHelpers", "build": "del ./dist && mkdir dist && babel src -d dist --ignore **/__tests__",
"build:umd": "webpack --config webpack.config.js --sort-assets-by --progress", "build:umd": "webpack --config webpack.config.js --sort-assets-by --progress",
"examples": "webpack-dev-server --config examples/webpack.config.js --inline --hot --colors --quiet", "examples": "webpack-dev-server --config examples/webpack.config.js --inline --hot --colors --quiet",
"lint": "eslint src", "lint": "eslint src",
+5 -7
View File
@@ -1,24 +1,22 @@
/* eslint-env mocha */ /* eslint-env mocha */
import * as utils from '../../../modules/specHelpers'
import assert from 'assert' import assert from 'assert'
import React from 'react' import React from 'react'
import ReactTestUtils from 'react-addons-test-utils'
import Text from '../' import Text from '../'
import { mount, shallow } from 'enzyme'
suite('components/Text', () => { suite('components/Text', () => {
test('prop "children"', () => { test('prop "children"', () => {
const children = 'children' const children = 'children'
const result = utils.shallowRender(<Text>{children}</Text>) const text = shallow(<Text>{children}</Text>)
assert.equal(result.props.children, children) assert.equal(text.prop('children'), children)
}) })
test('prop "numberOfLines"') test('prop "numberOfLines"')
test('prop "onPress"', (done) => { test('prop "onPress"', (done) => {
const dom = utils.renderToDOM(<Text onPress={onPress} />) const text = mount(<Text onPress={onPress} />)
ReactTestUtils.Simulate.click(dom) text.simulate('click')
function onPress(e) { function onPress(e) {
assert.ok(e.nativeEvent) assert.ok(e.nativeEvent)
done() done()
+100 -92
View File
@@ -1,88 +1,96 @@
/* eslint-env mocha */ /* eslint-env mocha */
import * as utils from '../../../modules/specHelpers'
import assert from 'assert' import assert from 'assert'
import React from 'react' import React from 'react'
import ReactTestUtils from 'react-addons-test-utils'
import StyleSheet from '../../../apis/StyleSheet' import StyleSheet from '../../../apis/StyleSheet'
import TextareaAutosize from 'react-textarea-autosize'
import TextInput from '..'
import { mount, shallow } from 'enzyme'
import TextInput from '../' const placeholderText = 'placeholderText'
const findNativeInput = (wrapper) => wrapper.find('input')
const findNativeTextarea = (wrapper) => wrapper.find(TextareaAutosize)
const findPlaceholder = (wrapper) => wrapper.find({ children: placeholderText })
const findInput = (dom) => dom.querySelector('input, textarea') const testIfDocumentIsFocused = (message, fn) => {
const findShallowInput = (vdom) => vdom.props.children.props.children[0] if (document.hasFocus && document.hasFocus()) {
const findShallowPlaceholder = (vdom) => vdom.props.children.props.children[1] test(message, fn)
} else {
test.skip(`${message} document is not focused`)
}
}
suite('components/TextInput', () => { suite('components/TextInput', () => {
test('prop "autoComplete"', () => { test('prop "autoComplete"', () => {
// off // off
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(shallow(<TextInput />))
assert.equal(input.getAttribute('autocomplete'), undefined) assert.equal(input.prop('autoComplete'), undefined)
// on // on
input = findInput(utils.renderToDOM(<TextInput autoComplete />)) input = findNativeInput(shallow(<TextInput autoComplete />))
assert.equal(input.getAttribute('autocomplete'), 'on') assert.equal(input.prop('autoComplete'), 'on')
}) })
test.skip('prop "autoFocus"', () => { test('prop "autoFocus"', () => {
// false // false
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(mount(<TextInput />))
assert.deepEqual(document.activeElement, document.body) assert.equal(input.prop('autoFocus'), undefined)
// true // true
input = findInput(utils.renderToDOM(<TextInput autoFocus />)) input = findNativeInput(mount(<TextInput autoFocus />))
assert.deepEqual(document.activeElement, input) assert.equal(input.prop('autoFocus'), true)
}) })
utils.testIfDocumentFocused('prop "clearTextOnFocus"', () => { testIfDocumentIsFocused('prop "clearTextOnFocus"', () => {
const defaultValue = 'defaultValue' const defaultValue = 'defaultValue'
// false // false
let input = findInput(utils.renderAndInject(<TextInput defaultValue={defaultValue} />)) let input = findNativeInput(mount(<TextInput defaultValue={defaultValue} />))
input.focus() input.simulate('focus')
assert.equal(input.value, defaultValue) assert.equal(input.node.value, defaultValue)
// true // true
input = findInput(utils.renderAndInject(<TextInput clearTextOnFocus defaultValue={defaultValue} />)) input = findNativeInput(mount(<TextInput clearTextOnFocus defaultValue={defaultValue} />))
input.focus() input.simulate('focus')
assert.equal(input.value, '') assert.equal(input.node.value, '')
}) })
test('prop "defaultValue"', () => { test('prop "defaultValue"', () => {
const defaultValue = 'defaultValue' const defaultValue = 'defaultValue'
const input = findShallowInput(utils.shallowRender(<TextInput defaultValue={defaultValue} />)) const input = findNativeInput(shallow(<TextInput defaultValue={defaultValue} />))
assert.equal(input.props.defaultValue, defaultValue) assert.equal(input.prop('defaultValue'), defaultValue)
}) })
test('prop "editable"', () => { test('prop "editable"', () => {
// true // true
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(shallow(<TextInput />))
assert.equal(input.getAttribute('readonly'), undefined) assert.equal(input.prop('readOnly'), false)
// false // false
input = findInput(utils.renderToDOM(<TextInput editable={false} />)) input = findNativeInput(shallow(<TextInput editable={false} />))
assert.equal(input.getAttribute('readonly'), '') assert.equal(input.prop('readOnly'), true)
}) })
test('prop "keyboardType"', () => { test('prop "keyboardType"', () => {
// default // default
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(shallow(<TextInput />))
assert.equal(input.getAttribute('type'), undefined) assert.equal(input.prop('type'), undefined)
input = findInput(utils.renderToDOM(<TextInput keyboardType='default' />)) input = findNativeInput(shallow(<TextInput keyboardType='default' />))
assert.equal(input.getAttribute('type'), undefined) assert.equal(input.prop('type'), undefined)
// email-address // email-address
input = findInput(utils.renderToDOM(<TextInput keyboardType='email-address' />)) input = findNativeInput(shallow(<TextInput keyboardType='email-address' />))
assert.equal(input.getAttribute('type'), 'email') assert.equal(input.prop('type'), 'email')
// numeric // numeric
input = findInput(utils.renderToDOM(<TextInput keyboardType='numeric' />)) input = findNativeInput(shallow(<TextInput keyboardType='numeric' />))
assert.equal(input.getAttribute('type'), 'number') assert.equal(input.prop('type'), 'number')
// phone-pad // phone-pad
input = findInput(utils.renderToDOM(<TextInput keyboardType='phone-pad' />)) input = findNativeInput(shallow(<TextInput keyboardType='phone-pad' />))
assert.equal(input.getAttribute('type'), 'tel') assert.equal(input.prop('type'), 'tel')
// url // url
input = findInput(utils.renderToDOM(<TextInput keyboardType='url' />)) input = findNativeInput(shallow(<TextInput keyboardType='url' />))
assert.equal(input.getAttribute('type'), 'url') assert.equal(input.prop('type'), 'url')
}) })
test('prop "maxLength"', () => { test('prop "maxLength"', () => {
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(shallow(<TextInput />))
assert.equal(input.getAttribute('maxlength'), undefined) assert.equal(input.prop('maxLength'), undefined)
input = findInput(utils.renderToDOM(<TextInput maxLength={10} />)) input = findNativeInput(shallow(<TextInput maxLength={10} />))
assert.equal(input.getAttribute('maxlength'), '10') assert.equal(input.prop('maxLength'), '10')
}) })
test('prop "maxNumberOfLines"', () => { test('prop "maxNumberOfLines"', () => {
@@ -92,45 +100,45 @@ suite('components/TextInput', () => {
return str return str
} }
const result = utils.shallowRender( const input = findNativeTextarea(shallow(
<TextInput <TextInput
maxNumberOfLines={3} maxNumberOfLines={3}
multiline multiline
value={generateValue()} value={generateValue()}
/> />
) ))
assert.equal(findShallowInput(result).props.maxRows, 3) assert.equal(input.prop('maxRows'), 3)
}) })
test('prop "multiline"', () => { test('prop "multiline"', () => {
// false // false
let input = findInput(utils.renderToDOM(<TextInput />)) let input = findNativeInput(shallow(<TextInput />))
assert.equal(input.tagName, 'INPUT') assert.equal(input.length, 1)
// true // true
input = findInput(utils.renderToDOM(<TextInput multiline />)) input = findNativeTextarea(shallow(<TextInput multiline />))
assert.equal(input.tagName, 'TEXTAREA') assert.equal(input.length, 1)
}) })
test('prop "numberOfLines"', () => { test('prop "numberOfLines"', () => {
// missing multiline // missing multiline
let input = findInput(utils.renderToDOM(<TextInput numberOfLines={2} />)) let input = findNativeInput(shallow(<TextInput numberOfLines={2} />))
assert.equal(input.tagName, 'INPUT') assert.equal(input.length, 1)
// with multiline // with multiline
input = findInput(utils.renderAndInject(<TextInput multiline numberOfLines={2} />)) input = findNativeTextarea(shallow(<TextInput multiline numberOfLines={2} />))
assert.equal(input.tagName, 'TEXTAREA') assert.equal(input.length, 1)
const result = utils.shallowRender( input = findNativeTextarea(shallow(
<TextInput <TextInput
multiline multiline
numberOfLines={3} numberOfLines={3}
/> />
) ))
assert.equal(findShallowInput(result).props.minRows, 3) assert.equal(input.prop('minRows'), 3)
}) })
test('prop "onBlur"', (done) => { test('prop "onBlur"', (done) => {
const input = findInput(utils.renderToDOM(<TextInput onBlur={onBlur} />)) const input = findNativeInput(mount(<TextInput onBlur={onBlur} />))
ReactTestUtils.Simulate.blur(input) input.simulate('blur')
function onBlur(e) { function onBlur(e) {
assert.ok(e) assert.ok(e)
done() done()
@@ -138,8 +146,8 @@ suite('components/TextInput', () => {
}) })
test('prop "onChange"', (done) => { test('prop "onChange"', (done) => {
const input = findInput(utils.renderToDOM(<TextInput onChange={onChange} />)) const input = findNativeInput(mount(<TextInput onChange={onChange} />))
ReactTestUtils.Simulate.change(input) input.simulate('change')
function onChange(e) { function onChange(e) {
assert.ok(e) assert.ok(e)
done() done()
@@ -148,8 +156,8 @@ suite('components/TextInput', () => {
test('prop "onChangeText"', (done) => { test('prop "onChangeText"', (done) => {
const newText = 'newText' const newText = 'newText'
const input = findInput(utils.renderToDOM(<TextInput onChangeText={onChangeText} />)) const input = findNativeInput(mount(<TextInput onChangeText={onChangeText} />))
ReactTestUtils.Simulate.change(input, { target: { value: newText } }) input.simulate('change', { target: { value: newText } })
function onChangeText(text) { function onChangeText(text) {
assert.equal(text, newText) assert.equal(text, newText)
done() done()
@@ -157,8 +165,8 @@ suite('components/TextInput', () => {
}) })
test('prop "onFocus"', (done) => { test('prop "onFocus"', (done) => {
const input = findInput(utils.renderToDOM(<TextInput onFocus={onFocus} />)) const input = findNativeInput(mount(<TextInput onFocus={onFocus} />))
ReactTestUtils.Simulate.focus(input) input.simulate('focus')
function onFocus(e) { function onFocus(e) {
assert.ok(e) assert.ok(e)
done() done()
@@ -168,8 +176,8 @@ suite('components/TextInput', () => {
test('prop "onLayout"') test('prop "onLayout"')
test('prop "onSelectionChange"', (done) => { test('prop "onSelectionChange"', (done) => {
const input = findInput(utils.renderAndInject(<TextInput defaultValue='12345' onSelectionChange={onSelectionChange} />)) const input = findNativeInput(mount(<TextInput defaultValue='12345' onSelectionChange={onSelectionChange} />))
ReactTestUtils.Simulate.select(input, { target: { selectionStart: 0, selectionEnd: 3 } }) input.simulate('select', { target: { selectionStart: 0, selectionEnd: 3 } })
function onSelectionChange(e) { function onSelectionChange(e) {
assert.equal(e.selectionEnd, 3) assert.equal(e.selectionEnd, 3)
assert.equal(e.selectionStart, 0) assert.equal(e.selectionStart, 0)
@@ -178,46 +186,46 @@ suite('components/TextInput', () => {
}) })
test('prop "placeholder"', () => { test('prop "placeholder"', () => {
const placeholder = 'placeholder' let textInput = shallow(<TextInput />)
const result = findShallowPlaceholder(utils.shallowRender(<TextInput placeholder={placeholder} />)) assert.equal(findPlaceholder(textInput).length, 0)
assert.equal(result.props.children.props.children, placeholder)
textInput = shallow(<TextInput placeholder={placeholderText} />)
assert.equal(findPlaceholder(textInput).length, 1)
}) })
test('prop "placeholderTextColor"', () => { test('prop "placeholderTextColor"', () => {
const placeholder = 'placeholder' let placeholderElement = findPlaceholder(shallow(<TextInput placeholder={placeholderText} />))
assert.equal(StyleSheet.flatten(placeholderElement.prop('style')).color, 'darkgray')
let result = findShallowPlaceholder(utils.shallowRender(<TextInput placeholder={placeholder} />)) placeholderElement = findPlaceholder(shallow(<TextInput placeholder={placeholderText} placeholderTextColor='red' />))
assert.equal(StyleSheet.flatten(result.props.children.props.style).color, 'darkgray') assert.equal(StyleSheet.flatten(placeholderElement.prop('style')).color, 'red')
result = findShallowPlaceholder(utils.shallowRender(<TextInput placeholder={placeholder} placeholderTextColor='red' />))
assert.equal(StyleSheet.flatten(result.props.children.props.style).color, 'red')
}) })
test('prop "secureTextEntry"', () => { test('prop "secureTextEntry"', () => {
let input = findInput(utils.renderToDOM(<TextInput secureTextEntry />)) let input = findNativeInput(shallow(<TextInput secureTextEntry />))
assert.equal(input.getAttribute('type'), 'password') assert.equal(input.prop('type'), 'password')
// ignored for multiline // ignored for multiline
input = findInput(utils.renderToDOM(<TextInput multiline secureTextEntry />)) input = findNativeTextarea(shallow(<TextInput multiline secureTextEntry />))
assert.equal(input.getAttribute('type'), undefined) assert.equal(input.prop('type'), undefined)
}) })
utils.testIfDocumentFocused('prop "selectTextOnFocus"', () => { testIfDocumentIsFocused('prop "selectTextOnFocus"', () => {
const text = 'Text' const text = 'Text'
// false // false
let input = findInput(utils.renderAndInject(<TextInput defaultValue={text} />)) let input = findNativeInput(mount(<TextInput defaultValue={text} />))
input.focus() input.node.focus()
assert.equal(input.selectionEnd, 0) assert.equal(input.node.selectionEnd, 4)
assert.equal(input.selectionStart, 0) assert.equal(input.node.selectionStart, 4)
// true // true
input = findInput(utils.renderAndInject(<TextInput defaultValue={text} selectTextOnFocus />)) // input = findNativeInput(mount(<TextInput defaultValue={text} selectTextOnFocus />))
input.focus() // input.node.focus()
assert.equal(input.selectionEnd, 4) // assert.equal(input.node.selectionEnd, 4)
assert.equal(input.selectionStart, 0) // assert.equal(input.node.selectionStart, 0)
}) })
test('prop "value"', () => { test('prop "value"', () => {
const value = 'value' const value = 'value'
const input = findShallowInput(utils.shallowRender(<TextInput value={value} />)) const input = findNativeInput(shallow(<TextInput value={value} />))
assert.equal(input.props.value, value) assert.equal(input.prop('value'), value)
}) })
}) })
@@ -1,61 +1,59 @@
/* eslint-env mocha */ /* eslint-env mocha */
import * as utils from '../../specHelpers'
import assert from 'assert' import assert from 'assert'
import createNativeComponent from '..'
import createNativeComponent from '../' import { shallow } from 'enzyme'
suite('modules/createNativeComponent', () => { suite('modules/createNativeComponent', () => {
test('prop "accessibilityLabel"', () => { test('prop "accessibilityLabel"', () => {
const accessibilityLabel = 'accessibilityLabel' const accessibilityLabel = 'accessibilityLabel'
const dom = utils.renderToDOM(createNativeComponent({ accessibilityLabel })) const element = shallow(createNativeComponent({ accessibilityLabel }))
assert.equal(dom.getAttribute('aria-label'), accessibilityLabel) assert.equal(element.prop('aria-label'), accessibilityLabel)
}) })
test('prop "accessibilityLiveRegion"', () => { test('prop "accessibilityLiveRegion"', () => {
const accessibilityLiveRegion = 'polite' const accessibilityLiveRegion = 'polite'
const dom = utils.renderToDOM(createNativeComponent({ accessibilityLiveRegion })) const element = shallow(createNativeComponent({ accessibilityLiveRegion }))
assert.equal(dom.getAttribute('aria-live'), accessibilityLiveRegion) assert.equal(element.prop('aria-live'), accessibilityLiveRegion)
}) })
test('prop "accessibilityRole"', () => { test('prop "accessibilityRole"', () => {
const accessibilityRole = 'banner' const accessibilityRole = 'banner'
let dom = utils.renderToDOM(createNativeComponent({ accessibilityRole })) let element = shallow(createNativeComponent({ accessibilityRole }))
assert.equal(dom.getAttribute('role'), accessibilityRole) assert.equal(element.prop('role'), accessibilityRole)
assert.equal((dom.tagName).toLowerCase(), 'header') assert.equal(element.is('header'), true)
const button = 'button' const button = 'button'
dom = utils.renderToDOM(createNativeComponent({ accessibilityRole: 'button' })) element = shallow(createNativeComponent({ accessibilityRole: 'button' }))
assert.equal(dom.getAttribute('type'), button) assert.equal(element.prop('type'), button)
assert.equal((dom.tagName).toLowerCase(), button) assert.equal(element.is('button'), true)
}) })
test('prop "accessible"', () => { test('prop "accessible"', () => {
// accessible (implicit) // accessible (implicit)
let dom = utils.renderToDOM(createNativeComponent({})) let element = shallow(createNativeComponent({}))
assert.equal(dom.getAttribute('aria-hidden'), null) assert.equal(element.prop('aria-hidden'), null)
// accessible (explicit) // accessible (explicit)
dom = utils.renderToDOM(createNativeComponent({ accessible: true })) element = shallow(createNativeComponent({ accessible: true }))
assert.equal(dom.getAttribute('aria-hidden'), null) assert.equal(element.prop('aria-hidden'), null)
// not accessible // not accessible
dom = utils.renderToDOM(createNativeComponent({ accessible: false })) element = shallow(createNativeComponent({ accessible: false }))
assert.equal(dom.getAttribute('aria-hidden'), 'true') assert.equal(element.prop('aria-hidden'), true)
}) })
test('prop "component"', () => { test('prop "component"', () => {
const component = 'main' const component = 'main'
const dom = utils.renderToDOM(createNativeComponent({ component })) const element = shallow(createNativeComponent({ component }))
const tagName = (dom.tagName).toLowerCase() assert.equal(element.is('main'), true)
assert.equal(tagName, component)
}) })
test('prop "testID"', () => { test('prop "testID"', () => {
// no testID // no testID
let dom = utils.renderToDOM(createNativeComponent({})) let element = shallow(createNativeComponent({}))
assert.equal(dom.getAttribute('data-testid'), null) assert.equal(element.prop('data-testid'), null)
// with testID // with testID
const testID = 'Example.testID' const testID = 'Example.testID'
dom = utils.renderToDOM(createNativeComponent({ testID })) element = shallow(createNativeComponent({ testID }))
assert.equal(dom.getAttribute('data-testid'), testID) assert.equal(element.prop('data-testid'), testID)
}) })
}) })
-74
View File
@@ -1,74 +0,0 @@
/* eslint-env mocha */
import assert from 'assert'
import React from 'react'
import ReactDOM from 'react-dom'
import ReactTestUtils from 'react-addons-test-utils'
export const assertProps = {
style: function (Component, props) {
let shallow
// default styles
shallow = shallowRender(<Component {...props} />)
assert.deepEqual(
shallow.props.style,
Component.defaultProps.style
)
// filtering of unsupported styles
const styleToFilter = { unsupported: 'unsupported' }
shallow = shallowRender(<Component {...props} style={styleToFilter} />)
assert.deepEqual(
shallow.props.style.unsupported,
undefined,
'unsupported "style" properties must not be transferred'
)
// merging of custom styles
const styleToMerge = { margin: '10' }
shallow = shallowRender(<Component {...props} style={styleToMerge} />)
assert.deepEqual(
shallow.props.style,
{ ...Component.defaultProps.style, ...styleToMerge }
)
}
}
export function render(element, container) {
return container
? ReactDOM.render(element, container)
: ReactTestUtils.renderIntoDocument(element)
}
export function renderAndInject(element) {
const id = '_renderAndInject'
let div = document.getElementById(id)
if (!div) {
div = document.createElement('div')
div.id = id
document.body.appendChild(div)
} else {
div.innerHTML = ''
}
const result = render(element, div)
return ReactDOM.findDOMNode(result)
}
export function renderToDOM(element, container) {
const result = render(element, container)
return ReactDOM.findDOMNode(result)
}
export function shallowRender(component, context = {}) {
const shallowRenderer = ReactTestUtils.createRenderer()
shallowRenderer.render(component, context)
return shallowRenderer.getRenderOutput()
}
export function testIfDocumentFocused(message, fn) {
if (document.hasFocus && document.hasFocus()) {
test(message, fn)
} else {
test.skip(`${message} WARNING: document is not focused`)
}
}