Skip to content

Commit 510ed7d

Browse files
committed
Added tests
1 parent d312d5e commit 510ed7d

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/lib/hooks/useRole/App.test.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
interface Props extends UseRoleOptions {
77
open?: boolean;
88
}
9-
let { open = true, ...rest }: Props = $props();
9+
let { open = false, ...rest }: Props = $props();
1010
const elements: { reference: HTMLElement | null; floating: HTMLElement | null } = $state({
1111
reference: null,
1212
floating: null

src/lib/hooks/useRole/index.test.svelte.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,67 @@ const ARIA_ROLES = ['grid', 'listbox', 'menu', 'tree', 'tooltip', 'alertdialog',
66

77
describe('useRole', () => {
88
it('by default applies the "dialog" role to the floating element', () => {
9-
render(App, { role: undefined });
9+
render(App, { role: undefined, open: true });
1010
expect(screen.queryByRole('dialog')).toBeInTheDocument();
1111
cleanup();
1212
});
1313

1414
for (const role of ARIA_ROLES) {
1515
it(`applies the "${role}" role to the floating element`, () => {
16-
render(App, { role });
16+
render(App, { role, open: true });
1717
expect(screen.queryByRole(role)).toBeInTheDocument();
1818
cleanup();
1919
});
2020
}
2121

22-
it.skip('sets correct aria attributes based on the open state', async () => {
23-
const { rerender } = render(App, { role: 'tooltip', open: true });
22+
describe('tooltip', () => {
23+
it.skip('sets correct aria attributes based on the open state', async () => {
24+
const { rerender } = render(App, { role: 'tooltip', open: true });
2425

25-
expect(screen.getByRole('button')).toHaveAttribute('aria-describedby');
26+
expect(screen.getByRole('button')).toHaveAttribute(
27+
'aria-describedby',
28+
screen.getByRole('tooltip').getAttribute('id')
29+
);
2630

27-
await rerender({ role: 'tooltip', open: false });
31+
await rerender({ role: 'tooltip', open: false });
2832

29-
expect(screen.getByRole('buton')).not.toHaveAttribute('aria-describedby');
33+
expect(screen.getByRole('buton')).not.toHaveAttribute('aria-describedby');
3034

31-
cleanup();
35+
cleanup();
36+
});
37+
});
38+
39+
describe('label', () => {
40+
it.skip('sets correct aria attributes based on the open state', async () => {
41+
const { rerender } = render(App, { role: 'label', open: true });
42+
43+
expect(screen.getByRole('button')).toHaveAttribute(
44+
'aria-labelledby',
45+
screen.getByRole('tooltip').getAttribute('id')
46+
);
47+
48+
await rerender({ role: 'tooltip', open: false });
49+
50+
expect(screen.getByRole('buton')).not.toHaveAttribute('aria-labelledby');
51+
52+
cleanup();
53+
});
54+
});
55+
56+
describe('dialog', () => {
57+
it.skip('sets correct aria attributes based on the open state', async () => {
58+
const { rerender } = render(App, { role: 'dialog', open: false });
59+
60+
expect(screen.getByRole('button')).toHaveAttribute('aria-haspopup', 'dialog');
61+
expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'false');
62+
63+
await rerender({ role: 'dialog', open: true });
64+
65+
expect(screen.getByRole('dialog')).toBeInTheDocument();
66+
expect(screen.getByRole('button')).toHaveAttribute(
67+
'aria-controls',
68+
screen.getByRole('dialog').getAttribute('id')
69+
);
70+
});
3271
});
3372
});

0 commit comments

Comments
 (0)