Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 7517ca5

Browse files
committed
fix(focus-lock): update types for focus lock component
1 parent fe2e1ac commit 7517ca5

File tree

4 files changed

+109
-21
lines changed

4 files changed

+109
-21
lines changed

packages/c-focus-lock/src/c-focus-lock.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ export const CFocusLock = defineComponent({
112112
const [firstChild] = slots.default?.({}) as VNode[]
113113

114114
if (!firstChild) {
115-
warn([
116-
{
117-
condition: __DEV__,
118-
message: `[chakra-ui:focus-lock]: Focus lock component expects at least and only one child element.`,
119-
},
120-
])
115+
warn({
116+
condition: __DEV__,
117+
message: `[chakra-ui:focus-lock]: Focus lock component expects at least and only one child element.`,
118+
})
121119
return
122120
}
123121

packages/c-focus-lock/tests/c-focus-lock.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ afterEach(() => {
1010
jest.clearAllMocks()
1111
})
1212

13-
// TODO:
14-
//
15-
// The majority of the tests in this file are skipped in jest
16-
// because focus tracking seems to behave differently here
17-
// than with in the browser.
18-
//
19-
// Focus appears to be sent to the body instead of the components.
20-
// However in the browser/playground focus behaves as expected.
21-
//
22-
// A possible solution for this problem is to test this in Cypress.
23-
2413
const renderComponent = (props?: any) => {
2514
const base = {
2615
components: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should match aria-hidden snapshot 1`] = `
4+
<div>
5+
<main>
6+
<div data-testid="aria-container"><input data-testid="input"><button>Another button</button></div>
7+
<section data-testid="outside-aria-container" chakra-aria-hidden="true" aria-hidden="true">Outside aria hidden</section>
8+
</main>
9+
</div>
10+
`;
11+
12+
exports[`should render properly 1`] = `
13+
<DocumentFragment>
14+
<main>
15+
<div
16+
data-testid="aria-container"
17+
>
18+
<input
19+
data-testid="input"
20+
/>
21+
<button>
22+
Another button
23+
</button>
24+
</div>
25+
<section
26+
data-testid="outside-aria-container"
27+
>
28+
Outside aria hidden
29+
</section>
30+
</main>
31+
</DocumentFragment>
32+
`;
Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
import { render } from '../../test-utils/src'
2-
import { VueA11y } from '../src'
1+
import { render, waitMs, screen } from '../../test-utils/src'
2+
import { hideOthers } from '../src'
3+
import { useRef } from '@chakra-ui/vue-utils'
4+
import { nextTick, ref, onMounted } from 'vue'
5+
6+
const renderComponent = (props?: any) => {
7+
const base = {
8+
template: `
9+
<main>
10+
<div v-if="open" :ref="targetRef" data-testid="aria-container">
11+
<input data-testid="input" />
12+
<button>Another button</button>
13+
</div>
14+
<section data-testid="outside-aria-container">Outside aria hidden</section>
15+
</main>
16+
`,
17+
setup() {
18+
const [targetRef, targetEl] = useRef()
19+
const open = ref(true)
20+
21+
onMounted(async () => {
22+
await nextTick()
23+
if (targetEl.value) {
24+
hideOthers(targetEl.value)
25+
}
26+
})
27+
28+
return {
29+
targetRef,
30+
open,
31+
}
32+
},
33+
...props,
34+
}
35+
return render(base)
36+
}
37+
38+
function assert(active: boolean = true) {
39+
const ariaContainer = screen.getByTestId('aria-container')
40+
const outsideAriaContainer = screen.getByTestId('outside-aria-container')
41+
42+
if (active) {
43+
expect(ariaContainer).not.toHaveAttribute('aria-hidden', 'true')
44+
expect(outsideAriaContainer).toHaveAttribute('aria-hidden', 'true')
45+
expect(outsideAriaContainer).toHaveAttribute('chakra-aria-hidden', 'true')
46+
} else {
47+
expect(ariaContainer).not.toHaveAttribute('aria-hidden', 'true')
48+
expect(outsideAriaContainer).not.toHaveAttribute('aria-hidden', 'true')
49+
expect(outsideAriaContainer).not.toHaveAttribute(
50+
'chakra-aria-hidden',
51+
'true'
52+
)
53+
}
54+
}
355

456
it('should render properly', () => {
5-
const { asFragment } = render(VueA11y)
57+
const { asFragment } = renderComponent()
658
expect(asFragment()).toMatchSnapshot()
7-
})
59+
})
60+
61+
it('should match aria-hidden snapshot', async () => {
62+
renderComponent()
63+
await waitMs(300)
64+
expect(document.body.innerHTML).toMatchSnapshot()
65+
})
66+
67+
it('should apply aria-hidden attribute to other elements except target', async () => {
68+
renderComponent()
69+
await waitMs(300)
70+
71+
assert(true)
72+
})
73+
74+
it.todo(
75+
'should remove aria-hidden attributes form other elements except target when job is undone'
76+
)

0 commit comments

Comments
 (0)