-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.spec.js
203 lines (172 loc) · 4.77 KB
/
index.spec.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import React, { Component } from 'react'
import renderer from './index'
import { Text, View, TextInput } from 'react-native'
const Comp = () => (
<View>
<Text>
Welcome to React Native!
</Text>
<Text>
To get started, edit index.ios.js
</Text>
<Text>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
<TextInput value='asdf' />
</View>
)
describe('tagNames', () => {
it('basic', () => {
let view = renderer(<Comp />)
let input = view.query('TextInput')
})
})
describe('attributes', () => {
it('attribute existance', () => {
let view = renderer(<Comp />)
let input = view.query('[value]')
expect(input).toBeDefined()
})
it('matches =', () => {
let view = renderer(<Comp />)
let input = view.query("[value='asdf']")
expect(input).toBeDefined()
})
it('matches |= where value is exact', () => {
let view = renderer(<Comp />)
let input = view.query("[value|='asdf']")
expect(input).toBeDefined()
})
it('matches |= where value is followed by -', () => {
let view = renderer(<Text testID='asdf-fdsa'>Yo</Text>)
let text = view.query("[testID|='asdf']")
expect(text).toBeDefined()
})
it('matches ^=', () => {
let view = renderer(<Comp />)
let input = view.query("[value^='as']")
expect(input).toBeDefined()
})
it('matches $=', () => {
let view = renderer(<Comp />)
let input = view.query("[value$='df']")
expect(input).toBeDefined()
})
it('matches *=', () => {
let view = renderer(<Comp />)
let input = view.query("[value*='sd']")
expect(input).toBeDefined()
})
it('can match multiple attributes', () => {
let view = renderer(<Text a='asdf' b='fdsa'>Yo</Text>)
let text = view.query("[a$='df'][b*='ds']")
expect(text).toBeDefined()
})
it('can match with a tagname', () => {
let view = renderer(<Comp />)
let input = view.query("TextInput[value='asdf']")
expect(input).toBeDefined()
})
})
describe('id', () => {
it('will match a testID', () => {
let view = renderer(<Text testID='test'>Yo</Text>)
let text = view.query('#test')
expect(text).toBeDefined()
expect(text.text()).toEqual('Yo')
})
it('ignores periods as classnames', () => {
let view = renderer(<Text testID='a.b'>Yo</Text>)
expect(view.query('#a.b')).not.toBeNull()
})
})
describe('selectors', () => {
it('matches multiple views with multiple selectors', () => {
let view = renderer(
<View>
<Text testID='test'>Yo</Text>
<TextInput />
</View>
)
let views = view.queryAll('#test, TextInput')
expect(views.length).toEqual(2)
})
it('provides a query API to queried results', () => {
const view = renderer(
<View>
<View testID='block1'>
<Text>1</Text>
<Text>2</Text>
<Text>3</Text>
</View>
<View testID='block2'>
<Text>4</Text>
<Text>5</Text>
<Text>6</Text>
</View>
</View>
)
const block1 = view.query('#block1')
const textBlocks = block1.queryAll('Text')
expect(textBlocks.length).toEqual(3)
expect(textBlocks.map((item) => item.text())).toEqual(['1', '2', '3'])
let block2 = view.query('#block2')
let textComponent = block2.query('Text')
expect(textComponent.text()).toEqual('4')
})
})
describe('simulate', () => {
it('errors when you try to simulate on something that cant handle that event', () => {
let onChange = jest.fn()
let view = renderer(<TextInput onChange={onChange} />)
expect(() => {
view.simulate('changeText', 'asdf')
}).toThrow()
expect(onChange).not.toHaveBeenCalled()
})
it('doesnt error and calls the handler', () => {
let onChangeText = jest.fn()
let view = renderer(<TextInput onChangeText={onChangeText} />)
expect(() => {
view.simulate('changeText', 'asdf')
}).not.toThrow()
expect(onChangeText).toHaveBeenCalledWith('asdf')
})
})
describe('text', () => {
it('finds all the text rendered by a component and its subviews', () => {
let view = renderer(
<View>
<Text>H</Text>
<Text>e</Text>
<Text>l</Text>
<Text>l</Text>
<View>
<Text>o</Text>
</View>
</View>
)
expect(view.text()).toEqual('Hello')
})
})
describe('instance', () => {
it('will return the underlying react instance', () => {
let view = renderer(
<View></View>
)
expect(view.instance()).not.toBeNull()
})
})
describe('state', () => {
it('returns the components state', () => {
class Test extends Component {
state = { test: 'test' }
render () {
return (<View></View>)
}
}
let view = renderer(<Test />)
expect(view.state()).toEqual({ test: 'test' })
})
})