Skip to content

Commit f69bde6

Browse files
[FEAT] Start implementing tests
1 parent 8125be9 commit f69bde6

File tree

22 files changed

+679
-2438
lines changed

22 files changed

+679
-2438
lines changed

eslint.config.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ export default [
4646
'no-console': process.env.NODE_ENV === 'production' ? 2 : 1,
4747
quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
4848
semi: ['error', 'never']
49-
}
49+
},
50+
overrides: [
51+
{
52+
files: ["**/*.test.jsx"],
53+
rules: {
54+
"import/named": "off"
55+
}
56+
}
57+
]
5058
}
5159
]

src/common/Accordion/Accordion.test.js

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright 2019 Iguazio Systems Ltd.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License") with
5+
an addition restriction as set forth herein. You may not use this
6+
file except in compliance with the License. You may obtain a copy of
7+
the License at http://www.apache.org/licenses/LICENSE-2.0.
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
implied. See the License for the specific language governing
13+
permissions and limitations under the License.
14+
15+
In addition, you may not use the software for any purposes that are
16+
illegal under applicable law, and the grant of the foregoing license
17+
under the Apache 2.0 license is conditioned upon your compliance with
18+
such restriction.
19+
*/
20+
import React from 'react'
21+
import { render, screen, fireEvent } from '@testing-library/react'
22+
import { vi, describe, it, expect, afterEach } from 'vitest'
23+
import Accordion from './Accordion'
24+
25+
vi.mock('igz-controls/components', () => ({
26+
RoundedIcon: (props) => <button data-testid="accordion-btn" {...props} />
27+
}))
28+
29+
describe('Accordion Component', () => {
30+
const defaultChildren = <div data-testid="content">Accordion Content</div>
31+
32+
afterEach(() => {
33+
vi.clearAllMocks()
34+
})
35+
36+
it('renders closed by default', () => {
37+
render(<Accordion>{defaultChildren}</Accordion>)
38+
39+
const accordion = screen.getByTestId('accordion')
40+
41+
expect(accordion).toHaveClass('accordion__container')
42+
expect(accordion).not.toHaveClass('open')
43+
expect(screen.getByText('Accordion Content')).toBeInTheDocument()
44+
})
45+
46+
it('renders opened when openByDefault is true', () => {
47+
render(<Accordion openByDefault>{defaultChildren}</Accordion>)
48+
49+
const accordion = screen.getByTestId('accordion')
50+
expect(accordion).toHaveClass('open')
51+
})
52+
53+
it('toggles state when clicking the container if no icon is provided', () => {
54+
render(<Accordion>{defaultChildren}</Accordion>)
55+
56+
const accordion = screen.getByTestId('accordion')
57+
58+
fireEvent.click(accordion)
59+
expect(accordion).toHaveClass('open')
60+
61+
fireEvent.click(accordion)
62+
expect(accordion).not.toHaveClass('open')
63+
})
64+
65+
it('does not toggle state when clicking container if icon is provided', () => {
66+
render(<Accordion icon={<span>Icon</span>}>{defaultChildren}</Accordion>)
67+
68+
const accordion = screen.getByTestId('accordion')
69+
70+
fireEvent.click(accordion)
71+
expect(accordion).not.toHaveClass('open')
72+
})
73+
74+
it('toggles state when clicking the icon', () => {
75+
render(
76+
<Accordion icon={<span>MY ICON</span>} iconClassName="custom-icon-class">
77+
{defaultChildren}
78+
</Accordion>
79+
)
80+
81+
const accordion = screen.getByTestId('accordion')
82+
const iconBtn = screen.getByTestId('accordion-btn')
83+
84+
expect(iconBtn).toHaveClass('accordion__icon')
85+
expect(iconBtn).toHaveClass('custom-icon-class')
86+
87+
fireEvent.click(iconBtn)
88+
expect(accordion).toHaveClass('open')
89+
expect(iconBtn).toHaveClass('open')
90+
91+
fireEvent.click(iconBtn)
92+
expect(accordion).not.toHaveClass('open')
93+
})
94+
95+
it('does not close if alwaysOpened prop is true', () => {
96+
render(<Accordion alwaysOpened openByDefault>{defaultChildren}</Accordion>)
97+
98+
const accordion = screen.getByTestId('accordion')
99+
expect(accordion).toHaveClass('open')
100+
101+
fireEvent.click(accordion)
102+
103+
expect(accordion).toHaveClass('open')
104+
})
105+
106+
it('handles closeOnBlur (clicking outside)', () => {
107+
const mockCloseOnBlur = vi.fn()
108+
109+
render(
110+
<Accordion
111+
openByDefault={true}
112+
closeOnBlur={mockCloseOnBlur}
113+
>
114+
{defaultChildren}
115+
</Accordion>
116+
)
117+
118+
const accordion = screen.getByTestId('accordion')
119+
expect(accordion).toHaveClass('open')
120+
121+
fireEvent.click(document.body)
122+
123+
expect(accordion).not.toHaveClass('open')
124+
expect(mockCloseOnBlur).toHaveBeenCalledTimes(1)
125+
})
126+
})

src/common/Breadcrumbs/Breadcrumbs.test.js

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)