Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increase test coverage #366

Merged
merged 28 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5f8ca89
test: add e2e tests for Card.stories, Header, HeaderMenuPopUp, Header…
ana-oprea Jul 4, 2023
c9fb963
chore: ESlint code cleanup - refs #254313
ana-oprea Jul 5, 2023
8f5fe38
test: add unit tests for Banner, Card and Header - refs #254313
ana-oprea Jul 5, 2023
77f234c
chore: fix ESlint failing - refs #254313
ana-oprea Jul 5, 2023
a42d9fb
test: add unit tests for Button - refs #254313
ana-oprea Jul 6, 2023
f543201
test: add unit tests for Statistic - refs #254313
ana-oprea Jul 6, 2023
9f23813
test: add unit tests for Popup - refs #254313
ana-oprea Jul 6, 2023
b1542d6
test: add unit tests for Message - refs #254313
ana-oprea Jul 6, 2023
dfe887e
test: add unit tests for RelatedContent - refs #254313
ana-oprea Jul 6, 2023
372236f
chore: cleanup code for Card tests - refs #254313
ana-oprea Jul 6, 2023
53a5a11
chore: fix Jenkins unit tests failing - refs #254313
ana-oprea Jul 7, 2023
4eea29e
test: add unit tests for LanguageLabeledIcon - refs #254313
ana-oprea Jul 7, 2023
9bb68f8
test: add unit tests for Table - refs #254313
ana-oprea Jul 7, 2023
72883ac
chore: fix failing Jenkins test - refs #254313
ana-oprea Jul 7, 2023
a93872d
test: add unit tests for Radio, Input - refs #254313
ana-oprea Jul 7, 2023
d547f7c
test: add unit tests for Timeline - refs #254313
ana-oprea Jul 7, 2023
24c6343
test: add unit tests for Item - refs #254313
ana-oprea Jul 7, 2023
0b68a0d
test: add unit tests for CallToAction - refs #254313
ana-oprea Jul 7, 2023
dc6ef8a
refactor: delete content.hidden from LanguageLabeledIcon - refs #254313
ana-oprea Jul 7, 2023
ba68182
test: add unit tests for Checkbox - refs #254313
ana-oprea Jul 7, 2023
7cfeebe
test: add unit tests for Footer - refs #254313
ana-oprea Jul 10, 2023
a5bf5f6
test: add unit tests for Logo - refs #254313
ana-oprea Jul 10, 2023
0f61c8c
test: add unit tests for Accordion - refs #254313
ana-oprea Jul 10, 2023
8b05f73
test: add unit tests for ItemGroupWithIcons - refs #254313
ana-oprea Jul 10, 2023
cc174fa
test: add unit tests for useFirstVisited, Confirm, DownloadLabeledIco…
ana-oprea Jul 10, 2023
c9458b8
chore: fix ESlint - refs #254313
ana-oprea Jul 10, 2023
0fcd925
test: add unit tests - refs #254313
ana-oprea Jul 11, 2023
1ae2f61
chore: fix ESlint - refs #254313
ana-oprea Jul 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/helpers/useClickOutside.test.js
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();
});
});
54 changes: 54 additions & 0 deletions src/helpers/useFirstVisited.test.js
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);
});
});
45 changes: 45 additions & 0 deletions src/ui/Accordion/Accordion.stories.test.js
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',
});
});
});
Loading