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

Niloofar Sadeghi / Task - Refactor tests in the toggle-button-group.spec.tsx file #7330

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ToggleButtonGroup from '../toggle-button-group.jsx';
import ToggleButton from '../toggle-button.jsx';

const MockToggleButtonGroup = ({ value, className, multiple, onChange }) => (
<ToggleButtonGroup value={value} className={className} onChange={onChange} multiple={multiple}>
<ToggleButton value='first-button'>first test button</ToggleButton>
{multiple && <ToggleButton value='second-button'>second test button</ToggleButton>}
</ToggleButtonGroup>
);

describe('ToggleButtonGroup', () => {
it('should have "toggle-button-group" class when "className" passed', () => {
render(<MockToggleButtonGroup className='test-class' />);
expect(screen.getByTestId('dt_toggle_button_group')).toHaveClass('toggle-button-group');
niloofar-deriv marked this conversation as resolved.
Show resolved Hide resolved
});

it('should be called when the "ToggleButton" is clicked with value of the button', () => {
const mock_click = jest.fn();
render(<MockToggleButtonGroup onChange={mock_click} />);
const button = screen.getByRole('button', { name: 'first test button' });
userEvent.click(button);
expect(mock_click).toHaveBeenCalledTimes(1);
expect(mock_click.mock.calls[0][1]).toBe('first-button');
});

it('should be called when the "ToggleButton" is clicked with an empty array', () => {
const mock_click = jest.fn();
render(<MockToggleButtonGroup value={['first-button']} multiple onChange={mock_click} />);
const button = screen.getByRole('button', { name: 'first test button' });
userEvent.click(button);
expect(mock_click).toHaveBeenCalledTimes(1);
expect(Array.isArray(mock_click.mock.calls[0][1])).toBeTruthy();
expect(mock_click.mock.calls[0][1]).toHaveLength(0);
});
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const ToggleButtonGroup = ({ children, className, multiple, onChange, value, ...
});

return (
<div className={classNames('toggle-button-group', className)} {...others}>
<div data-testid='dt_toggle_button_group' className={classNames('toggle-button-group', className)} {...others}>
{toggle_buttons}
</div>
);
Expand Down