3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import { screen } from '@testing-library/react';
21 import userEvent from '@testing-library/user-event';
22 import { render } from '../../../helpers/testUtils';
23 import { FCProps } from '../../../types/misc';
24 import { SubnavigationAccordion } from '../SubnavigationAccordion';
26 it('should have correct style and html structure', () => {
27 setupWithProps({ expanded: false });
29 expect(screen.getByRole('button', { expanded: false })).toBeVisible();
30 expect(screen.queryByText('Foo')).not.toBeInTheDocument();
33 it('should display expanded', () => {
34 setupWithProps({ expanded: true });
36 expect(screen.getByRole('button', { expanded: true })).toBeVisible();
37 expect(screen.getByText('Foo')).toBeVisible();
40 it('should display expanded by default', () => {
43 expect(screen.getByRole('button')).toBeVisible();
44 expect(screen.getByText('Foo')).toBeVisible();
47 it('should toggle expand', async () => {
48 const user = userEvent.setup();
49 const onSetExpanded = jest.fn();
50 setupWithProps({ onSetExpanded, expanded: false });
52 expect(onSetExpanded).not.toHaveBeenCalled();
53 await user.click(screen.getByRole('button', { expanded: false }));
55 expect(onSetExpanded).toHaveBeenCalledWith(true);
58 it('should toggle collapse', async () => {
59 const user = userEvent.setup();
60 const onSetExpanded = jest.fn();
61 setupWithProps({ onSetExpanded, expanded: true });
63 expect(onSetExpanded).not.toHaveBeenCalled();
64 await user.click(screen.getByRole('button', { expanded: true }));
66 expect(onSetExpanded).toHaveBeenCalledWith(false);
69 function setupWithProps(props: Partial<FCProps<typeof SubnavigationAccordion>> = {}) {
71 <SubnavigationAccordion header="Header" id="test" {...props}>
73 </SubnavigationAccordion>