Skip to content

Commit bc623e8

Browse files
committed
fix: remove generic containers inside other containers
1 parent ab17f99 commit bc623e8

File tree

2 files changed

+79
-64
lines changed

2 files changed

+79
-64
lines changed

src/a11y-snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const flattenContainers = (nodes: AXNode[]): AXNode[] =>
1414
return [...acc, ...flattenContainers(children)]
1515
}
1616

17-
return [...acc, node]
17+
return [...acc, { ...node, children: flattenContainers(children) }]
1818
}, [])
1919

2020
const removeEmpty = ({ children = [], ...node }: AXNode): AXNode => {

test/a11y-snapshot.test.tsx

Lines changed: 78 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ test('can create a11y snapshot from HTML string', async () => {
66
const element = '<section><h1>Hello</h1><a href="/world">World</a></section>'
77

88
expect(await a11ySnapshot(element)).toMatchInlineSnapshot(`
9-
"role: WebArea
10-
children:
11-
-
12-
role: region
9+
"role: WebArea
1310
children:
1411
-
15-
role: heading
16-
name: Hello
17-
level: 1
12+
role: region
1813
children:
1914
-
20-
role: text
15+
role: heading
2116
name: Hello
22-
-
23-
role: link
24-
name: World
25-
children:
17+
level: 1
18+
children:
19+
-
20+
role: text
21+
name: Hello
2622
-
27-
role: text
23+
role: link
2824
name: World
29-
"
30-
`)
25+
children:
26+
-
27+
role: text
28+
name: World
29+
"
30+
`)
3131
})
3232

3333
test('can create a11y snapshot from DOM element', async () => {
@@ -37,21 +37,21 @@ test('can create a11y snapshot from DOM element', async () => {
3737
element.appendChild(heading)
3838

3939
expect(await a11ySnapshot(element)).toMatchInlineSnapshot(`
40-
"role: WebArea
41-
children:
42-
-
43-
role: banner
40+
"role: WebArea
4441
children:
4542
-
46-
role: heading
47-
name: 'hello header'
48-
level: 1
43+
role: banner
4944
children:
5045
-
51-
role: text
46+
role: heading
5247
name: 'hello header'
53-
"
54-
`)
48+
level: 1
49+
children:
50+
-
51+
role: text
52+
name: 'hello header'
53+
"
54+
`)
5555
})
5656

5757
test('can create a11y snapshot from React element', async () => {
@@ -63,28 +63,28 @@ test('can create a11y snapshot from React element', async () => {
6363
)
6464

6565
expect(await a11ySnapshot(element)).toMatchInlineSnapshot(`
66-
"role: WebArea
67-
children:
68-
-
69-
role: region
66+
"role: WebArea
7067
children:
7168
-
72-
role: heading
73-
name: Hello
74-
level: 1
69+
role: region
7570
children:
7671
-
77-
role: text
72+
role: heading
7873
name: Hello
79-
-
80-
role: link
81-
name: World
82-
children:
74+
level: 1
75+
children:
76+
-
77+
role: text
78+
name: Hello
8379
-
84-
role: text
80+
role: link
8581
name: World
86-
"
87-
`)
82+
children:
83+
-
84+
role: text
85+
name: World
86+
"
87+
`)
8888
})
8989

9090
test('should remove generic containers from a11y snapshot', async () => {
@@ -95,31 +95,46 @@ test('should remove generic containers from a11y snapshot', async () => {
9595
<h1>Hello</h1>
9696
</div>
9797
<div>
98-
<a href="/world">World</a>
98+
<div>
99+
<div>
100+
<a href="/world">World</a>
101+
</div>
102+
</div>
99103
</div>
104+
<label>
105+
<div>
106+
<div>foo</div>
107+
</div>
108+
</label>
100109
</div>
101110
)
102111

103112
expect(await a11ySnapshot(element)).toMatchInlineSnapshot(`
104-
"role: WebArea
105-
children:
106-
-
107-
role: heading
108-
name: Hello
109-
level: 1
113+
"role: WebArea
110114
children:
111115
-
112-
role: text
116+
role: heading
113117
name: Hello
114-
-
115-
role: link
116-
name: World
117-
children:
118+
level: 1
119+
children:
120+
-
121+
role: text
122+
name: Hello
118123
-
119-
role: text
124+
role: link
120125
name: World
121-
"
122-
`)
126+
children:
127+
-
128+
role: text
129+
name: World
130+
-
131+
role: Label
132+
children:
133+
-
134+
role: text
135+
name: foo
136+
"
137+
`)
123138
})
124139

125140
test('a11y snapshot is affected by document styles', async () => {
@@ -140,15 +155,15 @@ test('a11y snapshot is affected by document styles', async () => {
140155
)
141156

142157
expect(await a11ySnapshot(element)).toMatchInlineSnapshot(`
143-
"role: WebArea
144-
children:
145-
-
146-
role: link
147-
name: World
158+
"role: WebArea
148159
children:
149160
-
150-
role: text
161+
role: link
151162
name: World
152-
"
153-
`)
163+
children:
164+
-
165+
role: text
166+
name: World
167+
"
168+
`)
154169
})

0 commit comments

Comments
 (0)