Skip to content

Commit 23f20de

Browse files
committed
Merge pull request #37 from NickStefan/master
find now supports more css selectors
2 parents 3e6b9e6 + a2191f1 commit 23f20de

File tree

6 files changed

+77
-25
lines changed

6 files changed

+77
-25
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ Test(<TestComponent onClick={spy}/>) //or shallow render Test(<Component/>, {sha
3131
//finding an element
3232
Test(<TestComponent/>)
3333
.find('.box')
34-
.test(({box}) => {
35-
/*new in 0.3.4
36-
if you only have elements in your helpers object, they're available in the root object
37-
ex: helpers.elements.box.props -> box.props, you can still use the long way :)
38-
*/
34+
.elements('.box', (box) => {
3935
expect(box.props.children).to.be.equal('found me!')
4036
})
4137
~~~
@@ -91,7 +87,7 @@ Test(<TestComponent/>)
9187

9288
.find('.box')
9389
.find('div')
94-
.element('box', box => {
90+
.element('.box', box => {
9591
expect(box.props.children).to.be.equal('found me!')
9692
})
9793

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "legit-tests",
3-
"version": "0.6.4",
3+
"version": "0.7.0",
44
"description": "a chainable testing library for React",
55
"main": "legit-tests.js",
66
"scripts": {
@@ -47,6 +47,7 @@
4747
"babel": "^5.8.29",
4848
"harmony-reflect": "^1.4.2",
4949
"jsdom": "^6.5.1",
50+
"lodash": "^3.10.1",
5051
"react": "^0.14.0",
5152
"react-addons-test-utils": "^0.14.0",
5253
"react-dom": "^0.14.0"

src/middleware/find.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,66 @@
11
import TestUtils from 'react-addons-test-utils'
2+
import _ from 'lodash'
23

34
export default function find(selector){
45

5-
let elements, name
6-
if (!(typeof selector === "string")) {
7-
name = (selector.name || selector.displayName).toLowerCase()
6+
var self = this
7+
var foundElements = []
8+
var elements
9+
var selector
10+
11+
if (_.isFunction(selector)){
812
elements = TestUtils.scryRenderedComponentsWithType(this.instance, selector)
9-
} else if (selector.match(/\./)) {
10-
selector = selector.replace(/\./, '')
11-
elements = TestUtils.scryRenderedDOMComponentsWithClass(this.instance, selector)
13+
selector = (selector.name || selector.displayName).toLowerCase()
14+
} else {
15+
16+
var tokens = selector.split(/(?=\.)|(?=#)|(?=\[)/)
17+
tokens
18+
.forEach(function(subselector){
19+
var els
20+
switch (subselector[0]){
21+
// class
22+
case '.':
23+
els = TestUtils.scryRenderedDOMComponentsWithClass(self.instance, subselector.slice(1))
24+
foundElements.push( Array.isArray(els) ? els : [els] )
25+
break
26+
27+
// id
28+
case '#':
29+
els = TestUtils.findAllInRenderedTree(self.instance, function(component){
30+
if (component.id === subselector.slice(1)){
31+
return true
32+
}
33+
})
34+
foundElements.push( Array.isArray(els) ? els : [els] )
35+
break
36+
37+
// data attribute
38+
case '[':
39+
els = TestUtils.findAllInRenderedTree(self.instance, function(component){
40+
if (component.getAttribute) {
41+
return component.getAttribute(subselector.slice(1,-1))
42+
}
43+
})
44+
foundElements.push( Array.isArray(els) ? els : [els] )
45+
break
46+
47+
// tag
48+
default:
49+
els = TestUtils.scryRenderedDOMComponentsWithTag(self.instance, subselector)
50+
foundElements.push( Array.isArray(els) ? els : [els] )
51+
break
52+
}
53+
})
54+
55+
elements = _.intersection.apply(_, foundElements)
1256
}
13-
else elements = TestUtils.scryRenderedDOMComponentsWithTag(this.instance, selector)
1457

15-
if (Array.isArray(elements) && elements.length === 1) {
16-
this.elements[name || selector] = elements[0]
17-
} else {
18-
this.elements[name || selector] = elements
58+
if (elements){
59+
if (Array.isArray(elements) && elements.length === 1) {
60+
this.elements[selector] = elements[0]
61+
} else {
62+
this.elements[selector] = elements
63+
}
1964
}
65+
2066
}

tests/components/component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class TestComponent extends Component {
1212
return (
1313
<section>
1414
<div onClick={this.props.onClick}>{this.state.test}</div>
15-
<p className="box">found me!</p>
15+
<p className="box" data-p-tag>found me!</p>
1616
<button
1717
type="button"
1818
onClick={this.props.onClick}>

tests/element.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('.element', () => {
1717
Test(<TestComponent/>)
1818
.find('.box')
1919
.find('div')
20-
.element('box', box => {
20+
.element('.box', box => {
2121
expect(box.props.children).to.be.equal('found me!')
2222
})
2323
.element('div', div => {
@@ -29,7 +29,7 @@ describe('.element', () => {
2929
Test(<TestComponent />)
3030
.find('.box')
3131
.find('div')
32-
.element(['box', 'div'], (box, div) => {
32+
.element(['.box', 'div'], (box, div) => {
3333
expect(div.length).to.be.equal(2)
3434
expect(box.props.children).to.be.equal('found me!')
3535
})

tests/find.jsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@ describe('Find middleware', () => {
1616

1717
it('should find p tag with class', () => {
1818
Test(<TestComponent/>)
19-
.use(Find, '.box')
19+
.use(Find, 'p.box')
2020
.test(function() {
21-
expect(this.elements.box.props.children).to.be.equal('found me!')
21+
expect(this.elements['p.box'].props.children).to.be.equal('found me!')
2222
})
2323

2424
Test(<TestComponent/>)
25-
.use(Find, '.box')
26-
.test(({box}) => {
25+
.use(Find, 'p.box')
26+
.element('p.box',(box) => {
2727
expect(box.innerHTML).to.be.equal('found me!')
2828
})
2929

3030
})
3131

32+
it('should find p tag with data attribute', () => {
33+
Test(<TestComponent/>)
34+
.use(Find, '[data-p-tag]')
35+
.test(function() {
36+
expect(this.elements['[data-p-tag]'].props.children).to.be.equal('found me!')
37+
})
38+
39+
})
40+
3241
it('should find a rendered component', () => {
3342
Test(<TestComponent/>)
3443
.find(TinyComponent)

0 commit comments

Comments
 (0)