Skip to content

Commit 2ea2f77

Browse files
committed
can now find attributes by value
1 parent 9fee13d commit 2ea2f77

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default class TestComponent extends Component {
2323
onClick={this.props.onClick}>
2424
Click Me
2525
</button>
26-
<input name="bob" />
26+
<input className="notbob" name />
27+
<input className="bob" name="bob" />
2728
<TinyComponent test="true"/>
2829
<OtherComponent test="true"/>
2930
</section>

tests/find.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ describe('Find middleware', () => {
4141
it('should find an input with a name attribute that equals \'bob\'', ()=>{
4242
Test(<TestComponent/>)
4343
.find('input[name="bob"]')
44+
.find('input[name]')
4445
.test(function(){
45-
expect(this.elements['[input[name="bob"]']).to.not.equal(undefined)
46+
expect(this.elements['input[name="bob"]'].className).equal('bob')
47+
expect(this.elements['input[name]'].className).equal('notbob')
4648
})
4749
})
4850

0 commit comments

Comments
 (0)