-
Notifications
You must be signed in to change notification settings - Fork 15
/
find-by-query.jsx
121 lines (89 loc) · 3.72 KB
/
find-by-query.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Note: you should use const createComponent = require('react-unit');
const createComponent = require('./react-unit');
const React = require('react');
const props = { value: 'found', onChange: function() {} }
const ByTag = () => <input {...props} />;
const ByClass = () => <input {...props} className="myClass" />;
const ByAttr = () => <input {...props} name="myName" />;
const ByRelativeClass = () =>
<div>
<div className="parent"><ByClass/></div>
<input {...props} className="myClass" value="not in parent!" />
</div>;
const ByTagAndAttr = () =>
<div>
<input {...props} name="myName" />
<span name="myName" value="a span" />
</div>;
const ByTagComposite = () => <ByTag/>;
const ByKey = () => <ul>{['a','b','c'].map(i => <li key={i}>{i}</li>)}</ul>;
const ByRef = () => <div><input {...props} ref="myRef" /></div>;
const ByContains = () =>
<div><span>one</span><span>two</span><span>three</span></div>;
describe('findByQuery', () => {
it('should find by tag name', () => {
const component = createComponent(<ByTag/>);
// Find every element with <input> tag
const input = component.findByQuery('input')[0];
expect(input.props.value).toEqual('found');
});
it('should find by class name', () => {
const component = createComponent(<ByClass/>);
// Find every element with className="myClass"
const input = component.findByQuery('.myClass')[0];
expect(input.props.value).toEqual('found');
});
it('should find by attribute', () => {
const component = createComponent(<ByAttr/>);
// Find every element with name="myName"
const input = component.findByQuery('[name="myName"]')[0];
expect(input.props.value).toEqual('found');
});
it('should find by relative class name', () => {
const component = createComponent(<ByRelativeClass/>);
// Find every element with className="myClass" inside a parent with
// className="parent"
const inputs = component.findByQuery('.parent .myClass');
expect(inputs.length).toEqual(1);
expect(inputs[0].props.value).toEqual('found');
});
it('should find by class in nested trees returning depth order', () => {
const component = createComponent(<ByRelativeClass/>);
// Find every element with className="myClass"
const inputs = component.findByQuery('.myClass');
expect(inputs.length).toEqual(2);
expect(inputs[0].props.value).toEqual('found');
expect(inputs[1].props.value).toEqual('not in parent!');
});
it('should find by tag and attribute', () => {
const component = createComponent(<ByTagAndAttr/>);
// Find every <input> with name="myName"
const inputs = component.findByQuery('input[name=myName]');
expect(inputs.length).toEqual(1);
expect(inputs[0].props.value).toEqual('found');
});
it('should find by tag name in a composite component', () => {
const component = createComponent(<ByTagComposite/>);
// Find every element with <input> tag
const input = component.findByQuery('input')[0];
expect(input.props.value).toEqual('found');
});
it('should find by key', () => {
const component = createComponent(<ByKey/>);
// Find every element with key="c"
const input = component.findByQuery('[key=c]')[0];
expect(input.text).toEqual('c');
});
it('should find by ref', () => {
const component = createComponent(<ByRef/>);
// Find every element with ref="myRef"
const input = component.findByQuery('[ref=myRef]')[0];
expect(input.props.value).toEqual('found');
});
it('should find by contains', () => {
const component = createComponent(<ByContains/>);
// Find elements that contains
const span = component.findByQuery('div span:contains("three")')[0];
expect(span.textContent).toEqual('three');
});
});