Skip to content

Commit bcd6345

Browse files
committed
ToggleGroup tests
1 parent 4eba599 commit bcd6345

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

src/components/ui/ToggleGroup/tests/ToggleGroup.test.js

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,85 @@ const items = [
99
];
1010

1111
describe('ToggleGroup component', () => {
12-
it('renders correctly', () => {
13-
const { container } = render(<ToggleGroup />);
12+
test('renders correctly', () => {
13+
const { container } = render(<ToggleGroup items={items}/>);
1414
expect(container.firstChild).toBeInTheDocument();
15+
const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
16+
17+
for (let i = 0; i < items.length; i++) {
18+
expect(toggleGroupRoot.children[i]).toBeInTheDocument();
19+
}
1520
});
1621

17-
it('renders the correct number of ToggleItem components', () => {
22+
test('renders the correct number of ToggleItem components', () => {
1823
const { getAllByText } = render(<ToggleGroup items={items} />);
1924
expect(getAllByText(/Item/).length).toBe(items.length);
2025
});
2126

22-
// it('passes the correct props to ToggleGroupRoot', () => {
23-
// render(<ToggleGroup type="multiple" items={items}/>);
24-
// const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
25-
// console.log(toggleGroupRoot.children)
27+
test('ToggleGroup handles multiple selection', () => {
28+
render(<ToggleGroup type="multiple" items={items}/>);
29+
const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
30+
fireEvent.click(toggleGroupRoot.children[0]);
31+
fireEvent.click(toggleGroupRoot.children[1]);
32+
33+
expect(toggleGroupRoot.children[0]).toHaveAttribute('aria-pressed', 'true');
34+
expect(toggleGroupRoot.children[0]).toHaveAttribute('data-active', 'true');
35+
36+
expect(toggleGroupRoot.children[1]).toHaveAttribute('aria-pressed', 'true');
37+
expect(toggleGroupRoot.children[1]).toHaveAttribute('data-active', 'true');
38+
39+
expect(toggleGroupRoot.children[2]).toHaveAttribute('aria-pressed', 'false');
40+
41+
42+
});
43+
44+
test('ToggleGroup handles multiple selection with variation in toggles', () => {
45+
render(<ToggleGroup type="multiple" items={items}/>);
46+
const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
47+
fireEvent.click(toggleGroupRoot.children[0]);
48+
fireEvent.click(toggleGroupRoot.children[1]);
49+
fireEvent.click(toggleGroupRoot.children[2]);
50+
fireEvent.click(toggleGroupRoot.children[1]);
51+
52+
expect(toggleGroupRoot.children[0]).toHaveAttribute('aria-pressed', 'true');
53+
expect(toggleGroupRoot.children[0]).toHaveAttribute('data-active', 'true');
54+
55+
expect(toggleGroupRoot.children[1]).toHaveAttribute('aria-pressed', 'false');
56+
57+
expect(toggleGroupRoot.children[2]).toHaveAttribute('aria-pressed', 'true');
58+
expect(toggleGroupRoot.children[2]).toHaveAttribute('data-active', 'true');
59+
60+
});
61+
62+
test('ToggleGroup handles single selection', () => {
63+
render(<ToggleGroup type="single" items={items}/>);
64+
const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
65+
fireEvent.click(toggleGroupRoot.children[0]);
66+
67+
expect(toggleGroupRoot.children[0]).toHaveAttribute('aria-pressed', 'true');
68+
expect(toggleGroupRoot.children[0]).toHaveAttribute('data-active', 'true');
69+
70+
expect(toggleGroupRoot.children[1]).toHaveAttribute('aria-pressed', 'false');
71+
expect(toggleGroupRoot.children[2]).toHaveAttribute('aria-pressed', 'false');
72+
73+
});
74+
75+
test('ToggleGroup handles single selection with variation in toggles', () => {
76+
render(<ToggleGroup type="single" items={items}/>);
77+
const toggleGroupRoot = document.querySelector('.rad-ui-toggle-group');
78+
79+
fireEvent.click(toggleGroupRoot.children[0]);
80+
fireEvent.click(toggleGroupRoot.children[1]);
81+
fireEvent.click(toggleGroupRoot.children[2]);
82+
fireEvent.click(toggleGroupRoot.children[1]);
83+
84+
expect(toggleGroupRoot.children[0]).toHaveAttribute('aria-pressed', 'false');
85+
86+
expect(toggleGroupRoot.children[1]).toHaveAttribute('aria-pressed', 'true');
87+
expect(toggleGroupRoot.children[1]).toHaveAttribute('data-active', 'true');
2688

27-
// });
89+
expect(toggleGroupRoot.children[2]).toHaveAttribute('aria-pressed', 'false');
90+
91+
});
92+
2893
});

0 commit comments

Comments
 (0)