-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathto-have-accessible-name.test.tsx
134 lines (112 loc) · 4.21 KB
/
to-have-accessible-name.test.tsx
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
import * as React from 'react';
import { View, Text, TextInput } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';
test('toHaveAccessibleName() handles view with "accessibilityLabel" prop', () => {
render(<View testID="view" accessibilityLabel="Test label" />);
const element = screen.getByTestId('view');
expect(element).toHaveAccessibleName('Test label');
expect(element).not.toHaveAccessibleName('Other label');
});
test('toHaveAccessibleName() handles view with "aria-label" prop', () => {
render(<View testID="view" aria-label="Test label" />);
const element = screen.getByTestId('view');
expect(element).toHaveAccessibleName('Test label');
expect(element).not.toHaveAccessibleName('Other label');
});
test('toHaveAccessibleName() handles view with "accessibilityLabelledBy" prop', () => {
render(
<View>
<Text nativeID="label">External label</Text>
<TextInput testID="input" accessibilityLabelledBy="label" />
</View>
);
const element = screen.getByTestId('input');
expect(element).toHaveAccessibleName('External label');
expect(element).not.toHaveAccessibleName('Other label');
});
test('toHaveAccessibleName() handles nested "accessibilityLabelledBy"', () => {
render(
<>
<View nativeID="label">
<Text>External label</Text>
</View>
<TextInput testID="input" accessibilityLabelledBy="label" />
</>
);
const element = screen.getByTestId('input');
expect(element).toHaveAccessibleName('External label');
expect(element).not.toHaveAccessibleName('Other label');
});
test('toHaveAccessibleName() handles view with nested "accessibilityLabelledBy" with no text', () => {
render(
<>
<View nativeID="label">
<View />
</View>
<TextInput testID="text-input" accessibilityLabelledBy="label" />
</>
);
const element = screen.getByTestId('text-input');
expect(element).not.toHaveAccessibleName();
});
test('toHaveAccessibleName() handles view with "aria-labelledby" prop', () => {
render(
<View>
<Text nativeID="label">External label</Text>
<TextInput testID="input" aria-labelledby="label" />
</View>
);
const element = screen.getByTestId('input');
expect(element).toHaveAccessibleName('External label');
expect(element).not.toHaveAccessibleName('Other label');
});
test('toHaveAccessibleName() handles view with implicit accessible name', () => {
render(<Text testID="view">Text</Text>);
const element = screen.getByTestId('view');
expect(element).toHaveAccessibleName('Text');
expect(element).not.toHaveAccessibleName('Other text');
});
test('toHaveAccessibleName() supports calling without expected name', () => {
render(<View testID="view" accessibilityLabel="Test label" />);
const element = screen.getByTestId('view');
expect(element).toHaveAccessibleName();
expect(() => expect(element).not.toHaveAccessibleName())
.toThrowErrorMatchingInlineSnapshot(`
"expect(element).not.toHaveAccessibleName()
Expected element not to have accessible name:
undefined
Received:
Test label"
`);
});
test('toHaveAccessibleName() handles a view without name when called without expected name', () => {
render(<View testID="view" />);
const element = screen.getByTestId('view');
expect(element).not.toHaveAccessibleName();
expect(() => expect(element).toHaveAccessibleName())
.toThrowErrorMatchingInlineSnapshot(`
"expect(element).toHaveAccessibleName()
Expected element to have accessible name:
undefined
Received:
"
`);
});
it('toHaveAccessibleName() rejects non-host element', () => {
const nonElement = 'This is not a ReactTestInstance';
expect(() => expect(nonElement).toHaveAccessibleName())
.toThrowErrorMatchingInlineSnapshot(`
"expect(received).toHaveAccessibleName()
received value must be a host element.
Received has type: string
Received has value: "This is not a ReactTestInstance""
`);
expect(() => expect(nonElement).not.toHaveAccessibleName())
.toThrowErrorMatchingInlineSnapshot(`
"expect(received).not.toHaveAccessibleName()
received value must be a host element.
Received has type: string
Received has value: "This is not a ReactTestInstance""
`);
});