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

feat(segmented-control): [DSM-800] add tests #792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion malty/atoms/Chip/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const Chip = ({
disabled={disabled}
readOnly={readOnly}
data-testid={dataTestId}
onClick={handleClick}
onClick={!disabled ? handleClick : undefined}
selected={selected}
height={chipSize}
size={size}
Expand Down
46 changes: 41 additions & 5 deletions malty/molecules/SegmentedControl/SegmentedControl.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from '@carlsberggroup/malty.utils.test';
import { fireEvent, screen } from '@testing-library/react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { SegmentedControl } from './SegmentedControl';
import { SegmentedControlOptions } from './SegmentedControl.types';
Expand All @@ -20,17 +21,52 @@ const segmentedControlOptions: SegmentedControlOptions[] = [
];

describe('SegmentedControl', () => {
it('renders chips', () => {
const onChange = jest.fn();

afterEach(() => {
jest.clearAllMocks();
});
jpcav21 marked this conversation as resolved.
Show resolved Hide resolved

it('should render chips', () => {
render(<SegmentedControl options={segmentedControlOptions} />);

expect(screen.getByText(segmentedControlOptions[0].label)).toBeInTheDocument();
expect(screen.getByText(segmentedControlOptions[1].label)).toBeInTheDocument();
expect(screen.getByText(segmentedControlOptions[2].label)).toBeInTheDocument();
});

it('calls function onChange', () => {
const onChange = jest.fn();
it('should call function onChange', () => {
render(<SegmentedControl options={segmentedControlOptions} onChange={onChange} />);
fireEvent.click(screen.getByText(segmentedControlOptions[1].label));

userEvent.click(screen.getByText(segmentedControlOptions[1].label));

expect(onChange).toHaveBeenCalledTimes(1);
jpcav21 marked this conversation as resolved.
Show resolved Hide resolved
});

it('should be disabled', () => {
render(
<SegmentedControl options={segmentedControlOptions} onChange={onChange} dataQaId="segmentedcontrol" disabled />
);

jpcav21 marked this conversation as resolved.
Show resolved Hide resolved
userEvent.click(screen.getByText(segmentedControlOptions[1].label), undefined, { skipPointerEventsCheck: true });

segmentedControlOptions.forEach((_, index) => {
expect(screen.getByTestId(`segmentedcontrol-chip-${index}`)).toHaveAttribute('disabled');
});

expect(onChange).not.toHaveBeenCalled();
});

it('should click on third option', () => {
render(<SegmentedControl options={segmentedControlOptions} onChange={onChange} />);

const option2 = screen.getByText(segmentedControlOptions[2].label);

expect(option2).toBeInTheDocument();

userEvent.click(option2);

expect(onChange).toHaveBeenCalledTimes(1);
jpcav21 marked this conversation as resolved.
Show resolved Hide resolved
expect(onChange).toHaveBeenCalledWith(segmentedControlOptions[2].value);
});
});
Loading