Skip to content

Commit 71c0bc0

Browse files
committed
Merge pull request #39 from NickStefan/find-attribute-values
Find attribute values
2 parents 3a26bdf + ec3c778 commit 71c0bc0

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let spy = sinon.spy()
2121

2222

2323
//Calling a prop
24-
Test(<TestComponent onClick={spy}/>) //or shallow render Test(<Component/>, {shallow: true})
24+
Test(<TestComponent onClick={spy}/>)
2525
.find('button')
2626
.simulate({method: 'click', element: 'button'})
2727
.test(() => {
@@ -112,6 +112,37 @@ Test(<TestComponent />)
112112

113113
~~~
114114

115+
##DOM rendering
116+
__Shallow__ -- uses React shallow rendering (no DOM)
117+
~~~js
118+
Test(<TestComponent onClick={spy}/>, {shallow: true})
119+
.find('button')
120+
.simulate({method: 'click', element: 'button'})
121+
.test(() => {
122+
expect(spy.called).to.be.true
123+
})
124+
~~~
125+
126+
__Normal__ -- React render into document fragment
127+
~~~js
128+
Test(<TestComponent onClick={spy}/>)
129+
.find('button')
130+
.simulate({method: 'click', element: 'button'})
131+
.test(() => {
132+
expect(spy.called).to.be.true
133+
})
134+
~~~
135+
136+
__fullDOM__ -- ReactDOM render into document.body.div of jsdom
137+
~~~js
138+
Test(<section />, {fullDOM: true})
139+
.test(function() {
140+
expect(global.window.document.querySelector('section'))
141+
.to.be.okay
142+
})
143+
.clean() // restores the document.body to empty
144+
~~~
145+
115146
You can see more examples in the tests directory.
116147

117148
##Testing Alt Stores

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "legit-tests",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "a chainable testing library for React",
55
"main": "legit-tests.js",
66
"scripts": {

src/middleware/find.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ export default function find(selector){
3636

3737
// data attribute
3838
case '[':
39+
var attributeName = _.first( subselector.slice(1,-1).split('=') )
40+
var attributeValue = subselector.slice(1,-1).split('=').slice(1).join('=').replace(/^"(.*)"$/, '$1')
41+
3942
els = TestUtils.findAllInRenderedTree(self.instance, function(component){
4043
if (component.getAttribute) {
41-
return component.getAttribute(subselector.slice(1,-1))
44+
var val = component.getAttribute(attributeName)
45+
if (val === attributeValue || (val === 'true' && attributeValue === '')){
46+
return true
47+
}
4248
}
4349
})
50+
4451
foundElements.push( Array.isArray(els) ? els : [els] )
4552
break
4653

tests/components/component.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class TestComponent extends Component {
2323
onClick={this.props.onClick}>
2424
Click Me
2525
</button>
26+
<input className="notbob" name />
27+
<input className="bob" name="bob" />
2628
<TinyComponent test="true"/>
2729
<OtherComponent test="true"/>
2830
</section>

tests/find.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ describe('Find middleware', () => {
3838

3939
})
4040

41+
it('should find an input with a name attribute that equals \'bob\'', ()=>{
42+
Test(<TestComponent/>)
43+
.find('input[name="bob"]')
44+
.find('input[name]')
45+
.test(function(){
46+
expect(this.elements['input[name="bob"]'].className).equal('bob')
47+
expect(this.elements['input[name]'].className).equal('notbob')
48+
})
49+
})
50+
4151
it('should find a rendered component', () => {
4252
Test(<TestComponent/>)
4353
.find(TinyComponent)

0 commit comments

Comments
 (0)