-
-
Notifications
You must be signed in to change notification settings - Fork 458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce jest and enzyme #136
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "react", "stage-0"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import expect from 'expect.js'; | ||
/* eslint-disable no-undef */ | ||
import React, { Component } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import TestUtils, { Simulate } from 'react-addons-test-utils'; | ||
import Select, { Option } from 'rc-select'; | ||
import $ from 'jquery'; | ||
import { mount, render } from 'enzyme'; | ||
import { renderToJson } from 'enzyme-to-json'; | ||
|
||
describe('Select', () => { | ||
let instance; | ||
|
@@ -32,19 +34,21 @@ describe('Select', () => { | |
open: true, | ||
}, () => { | ||
expect(instance.getPopupDOMNode().parentNode | ||
.parentNode.parentNode.nodeName.toLowerCase()).to.be('body'); | ||
expect(instance.getPopupDOMNode().className).not.to.contain('hidden'); | ||
.parentNode.parentNode.nodeName.toLowerCase()).toBe('body'); | ||
expect(instance.getPopupDOMNode().className).not.toContain('hidden'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should add css class of root dom node', () => { | ||
instance = ReactDOM.render( | ||
it('renders correctly', () => { | ||
const wrapper = render( | ||
<Select className="forTest" openClassName="my-open" value="2"> | ||
<Option value="1">1</Option> | ||
<Option value="2" disabled>2</Option> | ||
</Select>, div); | ||
expect(ReactDOM.findDOMNode(instance).className.indexOf('forTest') !== -1).to.be(true); | ||
</Select> | ||
); | ||
|
||
expect(renderToJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should default select the right option', (done) => { | ||
|
@@ -56,8 +60,8 @@ describe('Select', () => { | |
instance.setState({ | ||
open: true, | ||
}, () => { | ||
expect(instance.getPopupMenuComponent().instanceArray[0].isSelected()).to.be(false); | ||
expect(instance.getPopupMenuComponent().instanceArray[1].isSelected()).to.be(true); | ||
expect(instance.getPopupMenuComponent().instanceArray[0].isSelected()).toBe(false); | ||
expect(instance.getPopupMenuComponent().instanceArray[1].isSelected()).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
|
@@ -72,9 +76,9 @@ describe('Select', () => { | |
instance.setState({ | ||
open: true, | ||
}, () => { | ||
expect(instance.getPopupMenuComponent().instanceArray[0].isSelected()).to.be(true); | ||
expect(instance.getPopupMenuComponent().instanceArray[1].isSelected()).to.be(true); | ||
expect(instance.getPopupMenuComponent().instanceArray[2].isSelected()).to.be(false); | ||
expect(instance.getPopupMenuComponent().instanceArray[0].isSelected()).toBe(true); | ||
expect(instance.getPopupMenuComponent().instanceArray[1].isSelected()).toBe(true); | ||
expect(instance.getPopupMenuComponent().instanceArray[2].isSelected()).toBe(false); | ||
done(); | ||
}); | ||
}); | ||
|
@@ -87,7 +91,7 @@ describe('Select', () => { | |
</Select>, | ||
div); | ||
expect(TestUtils.scryRenderedDOMComponentsWithClass(instance, | ||
'rc-select-selection__clear')[0].style.display).to.be('block'); | ||
'rc-select-selection__clear')[0].style.display).toBe('block'); | ||
}); | ||
|
||
it('should hide clear button', () => { | ||
|
@@ -98,7 +102,7 @@ describe('Select', () => { | |
</Select>, | ||
div); | ||
expect(TestUtils.scryRenderedDOMComponentsWithClass(instance, | ||
'rc-select-selection__clear')[0].style.display).to.be('none'); | ||
'rc-select-selection__clear')[0].style.display).toBe('none'); | ||
}); | ||
|
||
it('should not response click event when select is disabled', (done) => { | ||
|
@@ -108,7 +112,7 @@ describe('Select', () => { | |
<Option value="2">2</Option> | ||
</Select>, div); | ||
Simulate.click(ReactDOM.findDOMNode(instance.refs.selection)); | ||
expect(instance.state.open).not.to.be.ok(); | ||
expect(instance.state.open).not.toBeTruthy(); | ||
done(); | ||
}); | ||
|
||
|
@@ -119,7 +123,7 @@ describe('Select', () => { | |
<Option value="2">2</Option> | ||
</Select>, div); | ||
expect($(ReactDOM.findDOMNode(instance)) | ||
.find('.rc-select-selection-selected-value').length).to.be(1); | ||
.find('.rc-select-selection-selected-value').length).toBe(1); | ||
done(); | ||
}); | ||
|
||
|
@@ -130,12 +134,12 @@ describe('Select', () => { | |
<Option value="2">2</Option> | ||
</Select>, div); | ||
expect($(ReactDOM.findDOMNode(instance)) | ||
.find('.rc-select-selection__placeholder').length).to.be(1); | ||
.find('.rc-select-selection__placeholder').length).toBe(1); | ||
done(); | ||
}); | ||
|
||
describe('when open', function test() { | ||
this.timeout(400000); | ||
describe('when open', () => { | ||
// this.timeout(400000); | ||
|
||
beforeEach((done) => { | ||
div = document.createElement('div'); | ||
|
@@ -164,54 +168,58 @@ describe('Select', () => { | |
Simulate.change(instance.getInputDOMNode()); | ||
setTimeout(() => { | ||
expect($(instance.getPopupDOMNode()) | ||
.find('.rc-select-dropdown-menu-item').length).to.be(1); | ||
.find('.rc-select-dropdown-menu-item').length).toBe(1); | ||
expect($(instance.getPopupDOMNode()) | ||
.find('.rc-select-dropdown-menu-item')[0].innerHTML).to.be('Not Found'); | ||
.find('.rc-select-dropdown-menu-item')[0].innerHTML).toBe('Not Found'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('should show search input in single selection trigger', (done) => { | ||
expect($(instance.getInputDOMNode()).parents('.rc-select-open').length).to.be(1); | ||
expect($(instance.getInputDOMNode()).parents('.rc-select-open').length).toBe(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('automatic tokenization ', () => { | ||
it('tokenize tag select', () => { | ||
instance = ReactDOM.render( | ||
const wrapper = mount( | ||
<Select tags tokenSeparators={[',']}> | ||
<Option value="1">1</Option> | ||
<Option value="2">2</Option> | ||
</Select>, | ||
div); | ||
const input = TestUtils.findRenderedDOMComponentWithTag(instance, 'input'); | ||
|
||
input.value = '2,3,4'; | ||
Simulate.change(input); | ||
wrapper.find('input').simulate('change', { target: { | ||
value: '2,3,4', | ||
} }); | ||
|
||
expect(instance.state.value).to.eql([ | ||
expect(wrapper.state().value).toEqual([ | ||
{ key: '2', label: '2' }, | ||
{ key: '3', label: '3' }, | ||
{ key: '4', label: '4' }, | ||
]); | ||
}); | ||
|
||
it('tokenize multiple select', () => { | ||
instance = ReactDOM.render( | ||
const wrapper = mount( | ||
<Select multiple optionLabelProp="children" tokenSeparators={[',']}> | ||
<Option value="1">One</Option> | ||
<Option value="2">Two</Option> | ||
</Select>, | ||
div); | ||
const input = TestUtils.findRenderedDOMComponentWithTag(instance, 'input'); | ||
); | ||
|
||
const input = wrapper.find('input'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enzyme 之于 React 就像 jQuery 之于 DOM,操作 Component 会简单很多。 |
||
|
||
input.value = 'One,'; | ||
Simulate.change(input); | ||
input.value = 'One,Two,Three'; | ||
Simulate.change(input); | ||
input.simulate('change', { target: { | ||
value: 'One', | ||
} }); | ||
|
||
expect(instance.state.value).to.eql([ | ||
input.simulate('change', { target: { | ||
value: 'One,Two,Three', | ||
} }); | ||
|
||
expect(wrapper.state().value).toEqual([ | ||
{ key: '1', label: 'One' }, | ||
{ key: '2', label: 'Two' }, | ||
]); | ||
|
@@ -242,10 +250,10 @@ describe('Select', () => { | |
} | ||
} | ||
|
||
instance = ReactDOM.render(<App />, div); | ||
instance.setState({ value: { label: 'One', key: '1' } }, () => { | ||
const input = TestUtils.findRenderedDOMComponentWithTag(instance, 'input'); | ||
expect(input.value).to.be('One'); | ||
const wrapper = mount(<App />); | ||
|
||
wrapper.setState({ value: { label: 'One', key: '1' } }, () => { | ||
expect(wrapper.find('input').props().value).toBe('One'); | ||
done(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
exports[`Select renders correctly 1`] = ` | ||
<div | ||
class="forTest rc-select rc-select-enabled"> | ||
<div | ||
aria-autocomplete="list" | ||
aria-expanded="false" | ||
aria-haspopup="true" | ||
class="rc-select-selection | ||
rc-select-selection--single" | ||
role="combobox" | ||
tabindex="0"> | ||
<div | ||
class="rc-select-selection__rendered"> | ||
<div | ||
class="rc-select-selection-selected-value" | ||
style="display:block;opacity:1;" | ||
title="2"> | ||
2 | ||
</div> | ||
<div | ||
class="rc-select-search rc-select-search--inline" | ||
style="display:none;"> | ||
<div | ||
class="rc-select-search__field__wrap"> | ||
<input | ||
class="rc-select-search__field" | ||
value="" /> | ||
<span | ||
class="rc-select-search__field__mirror" /> | ||
</div> | ||
</div> | ||
</div> | ||
<span | ||
class="rc-select-arrow" | ||
style="user-select:none;-webkit-user-select:none;" | ||
unselectable="unselectable"> | ||
<b /> | ||
</span> | ||
</div> | ||
</div> | ||
`; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
像这种 DOM 结构是不是渲染正确的,直接用 snapshot 测试,可以省事很多,而且覆盖率也更高。