MCPcopy Index your code
hub / github.com/Legitcode/tests / find

Function find

src/middleware/find.js:4–73  ·  view source on GitHub ↗
(selector)

Source from the content-addressed store, hash-verified

2import _ from 'lodash'
3
4export default function find(selector){
5
6 var self = this
7 var foundElements = []
8 var elements
9 var selector
10
11 if (_.isFunction(selector)){
12 elements = TestUtils.scryRenderedComponentsWithType(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 var attributeName = _.first( subselector.slice(1,-1).split('=') )
40 var attributeValue = subselector.slice(1,-1).split('=').slice(1).join('=').replace(/^"(.*)"$/, '$1')
41
42 els = TestUtils.findAllInRenderedTree(self.instance, function(component){
43 if (component.getAttribute) {
44 var val = component.getAttribute(attributeName)
45 if (val === attributeValue || (val === 'true' && attributeValue === '')){
46 return true
47 }
48 }
49 })
50
51 foundElements.push( Array.isArray(els) ? els : [els] )
52 break
53
54 // tag
55 default:
56 els = TestUtils.scryRenderedDOMComponentsWithTag(self.instance, subselector)
57 foundElements.push( Array.isArray(els) ? els : [els] )
58 break
59 }
60 })
61

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected