generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for DS components - refs #254313
* test: add e2e tests for Card.stories, Header, HeaderMenuPopUp, HeaderSearchPopUp - refs #254313 * chore: ESlint code cleanup - refs #254313 * test: add unit tests for Banner, Card and Header - refs #254313 * chore: fix ESlint failing - refs #254313 * test: add unit tests for Button - refs #254313 * test: add unit tests for Statistic - refs #254313 * test: add unit tests for Popup - refs #254313 * test: add unit tests for Message - refs #254313 * test: add unit tests for RelatedContent - refs #254313 * chore: cleanup code for Card tests - refs #254313 * chore: fix Jenkins unit tests failing - refs #254313 * test: add unit tests for LanguageLabeledIcon - refs #254313 * test: add unit tests for Table - refs #254313 * chore: fix failing Jenkins test - refs #254313 * test: add unit tests for Radio, Input - refs #254313 * test: add unit tests for Timeline - refs #254313 * test: add unit tests for Item - refs #254313 * test: add unit tests for CallToAction - refs #254313 * refactor: delete content.hidden from LanguageLabeledIcon - refs #254313 * test: add unit tests for Checkbox - refs #254313 * test: add unit tests for Footer - refs #254313 * test: add unit tests for Logo - refs #254313 * test: add unit tests for Accordion - refs #254313 * test: add unit tests for ItemGroupWithIcons - refs #254313 * test: add unit tests for useFirstVisited, Confirm, DownloadLabeledIcon, Dropdown, InpageNavigation, Label, Progress - refs #254313 * chore: fix ESlint - refs #254313 * test: add unit tests - refs #254313 * chore: fix ESlint - refs #254313
- Loading branch information
Showing
47 changed files
with
3,035 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import useClickOutside from './useClickOutside'; | ||
import { doesNodeContainClick } from 'semantic-ui-react/dist/commonjs/lib'; | ||
|
||
jest.mock('semantic-ui-react/dist/commonjs/lib', () => ({ | ||
doesNodeContainClick: jest.fn(), | ||
})); | ||
|
||
describe('useClickOutside', () => { | ||
let ref; | ||
let callback; | ||
|
||
beforeEach(() => { | ||
ref = { current: {} }; | ||
callback = jest.fn(); | ||
}); | ||
|
||
it('does not call callback function when clicked inside', () => { | ||
doesNodeContainClick.mockImplementation(() => true); | ||
|
||
const { rerender } = renderHook(() => | ||
useClickOutside({ targetRefs: [ref], callback }), | ||
); | ||
|
||
act(() => { | ||
document.dispatchEvent(new MouseEvent('mousedown')); | ||
}); | ||
|
||
rerender(); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls callback function when clicked outside', () => { | ||
doesNodeContainClick.mockImplementation(() => false); | ||
|
||
const { rerender } = renderHook(() => | ||
useClickOutside({ targetRefs: [ref], callback }), | ||
); | ||
|
||
act(() => { | ||
document.dispatchEvent(new MouseEvent('mousedown')); | ||
}); | ||
|
||
rerender(); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls callback function when clicked outside and no refs', () => { | ||
doesNodeContainClick.mockImplementation(() => false); | ||
|
||
const { rerender } = renderHook(() => useClickOutside({ callback })); | ||
|
||
act(() => { | ||
document.dispatchEvent(new MouseEvent('mousedown')); | ||
}); | ||
|
||
rerender(); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useFirstVisited from './useFirstVisited'; | ||
|
||
describe('useFirstVisited', () => { | ||
let observe; | ||
let unobserve; | ||
let callback; | ||
let disconnect; | ||
|
||
beforeEach(() => { | ||
observe = jest.fn(); | ||
unobserve = jest.fn(); | ||
disconnect = jest.fn(); | ||
|
||
window.IntersectionObserver = jest.fn(function (cb) { | ||
this.observe = observe; | ||
this.unobserve = unobserve; | ||
this.disconnect = disconnect; | ||
callback = cb; | ||
}); | ||
}); | ||
it('should set intersected to true when element is intersecting', async () => { | ||
const ref = { current: document.createElement('div') }; | ||
const { result } = renderHook(() => useFirstVisited(ref)); | ||
const entry = { isIntersecting: true }; | ||
|
||
expect(result.current).toBe(false); // Initial state | ||
callback([entry]); | ||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should unobserve and disconnect when ref changes or component unmounts', () => { | ||
const ref = { current: document.createElement('div') }; | ||
const newRef = { current: document.createElement('div') }; | ||
const { unmount, rerender } = renderHook( | ||
({ ref }) => useFirstVisited(ref), | ||
{ | ||
initialProps: { ref }, | ||
}, | ||
); | ||
|
||
expect(unobserve).not.toHaveBeenCalled(); | ||
expect(disconnect).not.toHaveBeenCalled(); | ||
|
||
rerender({ ref: newRef }); | ||
expect(unobserve).toHaveBeenCalled(); | ||
expect(disconnect).toHaveBeenCalled(); | ||
|
||
unmount(); | ||
|
||
expect(unobserve).toHaveBeenCalledTimes(2); | ||
expect(disconnect).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { Default } from './Accordion.stories'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
describe('Default component', () => { | ||
it('renders correctly and fires keyDown Enter events', () => { | ||
const { getByText, getAllByText } = render(<Default {...Default.args} />); | ||
|
||
Default.args.panels.forEach((panel) => { | ||
expect(getByText(panel.title)).toBeInTheDocument(); | ||
}); | ||
|
||
expect(getAllByText(Default.args.panels[0].content)).toHaveLength(3); | ||
fireEvent.keyDown(getByText(Default.args.panels[0].title), { | ||
key: 'Enter', | ||
code: 'Enter', | ||
keyCode: 13, | ||
charCode: 13, | ||
}); | ||
|
||
fireEvent.keyDown(getByText(Default.args.panels[0].title), { | ||
key: 'Enter', | ||
code: 'Enter', | ||
keyCode: 13, | ||
charCode: 13, | ||
}); | ||
}); | ||
|
||
it('renders correctly and fires keyDown A event', () => { | ||
const { getByText, getAllByText } = render( | ||
<Default {...Default.args} title_size={'h1'} />, | ||
); | ||
|
||
Default.args.panels.forEach((panel) => { | ||
expect(getByText(panel.title)).toBeInTheDocument(); | ||
}); | ||
|
||
expect(getAllByText(Default.args.panels[0].content)).toHaveLength(3); | ||
fireEvent.keyDown(getByText(Default.args.panels[0].title), { | ||
key: 'A', | ||
code: 'KeyA', | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.