Skip to content

Commit

Permalink
Merge pull request #1844 from epam/apply-comments-for-input-tests
Browse files Browse the repository at this point in the history
[Inputs] Apply comments for input tests
  • Loading branch information
AlekseyManetov authored Dec 12, 2023
2 parents 4e4d466 + 10a6896 commit 443e1eb
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 28 deletions.
38 changes: 37 additions & 1 deletion uui-components/src/inputs/__tests__/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Checkbox, CheckboxProps } from '../Checkbox';
import { render, screen, fireEvent, setupComponentForTest } from '@epam/uui-test-utils';
import { render, screen, fireEvent, setupComponentForTest, userEvent } from '@epam/uui-test-utils';

async function setupCheckbox(params: Partial<CheckboxProps>) {
const { mocks, setProps } = await setupComponentForTest<CheckboxProps>(
Expand Down Expand Up @@ -38,6 +38,39 @@ describe('Checkbox', () => {
expect(getValueChangeAnalyticsEvent).toHaveBeenCalled();
});

it('should handle keyboard click', async () => {
const onValueChange = jest.fn();
const getValueChangeAnalyticsEvent = jest.fn();
await setupCheckbox({
value: false,
onValueChange,
getValueChangeAnalyticsEvent,
});
const input = screen.getByRole('checkbox');

await userEvent.type(input, '{space}');

expect(onValueChange).toHaveBeenCalledWith(true);
expect(getValueChangeAnalyticsEvent).toHaveBeenCalled();
});

it('should handle label click', async () => {
const onValueChange = jest.fn();
const getValueChangeAnalyticsEvent = jest.fn();
await setupCheckbox({
value: false,
onValueChange,
getValueChangeAnalyticsEvent,
label: 'Label',
});
const label = screen.getByText('Label');

label.click();

expect(onValueChange).toHaveBeenCalledWith(true);
expect(getValueChangeAnalyticsEvent).toHaveBeenCalled();
});

it('should not handle change event when readonly', () => {
const onValueChange = jest.fn();
render(<Checkbox value={ false } onValueChange={ onValueChange } isReadonly />);
Expand All @@ -55,6 +88,7 @@ describe('Checkbox', () => {
input.focus();

expect(onFocus).toHaveBeenCalled();
expect(input).toHaveFocus();
});

it('should handle blur event', () => {
Expand All @@ -63,8 +97,10 @@ describe('Checkbox', () => {
const input = screen.getByRole('checkbox');

input.focus();
expect(input).toHaveFocus();
input.blur();

expect(onBlur).toHaveBeenCalled();
expect(input).not.toHaveFocus();
});
});
22 changes: 0 additions & 22 deletions uui-components/src/inputs/__tests__/InputAddon.test.tsx

This file was deleted.

41 changes: 40 additions & 1 deletion uui-components/src/inputs/__tests__/RadioInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { RadioInput, RadioInputProps } from '../RadioInput';
import { screen, setupComponentForTest } from '@epam/uui-test-utils';
import { screen, setupComponentForTest, userEvent } from '@epam/uui-test-utils';

async function setupRadioInput(params: Partial<RadioInputProps>) {
const { mocks, setProps } = await setupComponentForTest<RadioInputProps>(
Expand Down Expand Up @@ -30,6 +30,26 @@ describe('RadioInput', () => {
expect(onValueChange).toHaveBeenCalledWith(true);
});

it('should call onValueChange on keyboard click', async () => {
const onValueChange = jest.fn();
await setupRadioInput({ value: false, onValueChange });
const input = screen.getByRole('radio');

await userEvent.type(input, '{space}');

expect(onValueChange).toHaveBeenCalledWith(true);
});

it('should call onValueChange on label click', async () => {
const onValueChange = jest.fn();
await setupRadioInput({ value: false, onValueChange, label: 'RadioLabel' });
const label = screen.getByText('RadioLabel');

label.click();

expect(onValueChange).toHaveBeenCalledWith(true);
});

it('should not call onValueChange when clicked if isReadonly is true', async () => {
const onValueChange = jest.fn();

Expand All @@ -48,4 +68,23 @@ describe('RadioInput', () => {
input.click();
expect(getValueChangeAnalyticsEvent).toHaveBeenCalled();
});

it('should handle focus event', async () => {
const onFocus = jest.fn();
await setupRadioInput({ value: false, onFocus });
const input = screen.getByRole('radio');
input.focus();
expect(onFocus).toHaveBeenCalled();
expect(input).toHaveFocus();
});

it('should handle blur event', async () => {
const onBlur = jest.fn();
await setupRadioInput({ value: false, onBlur });
const input = screen.getByRole('radio');
input.focus();
input.blur();
expect(onBlur).toHaveBeenCalled();
expect(input).not.toHaveFocus();
});
});
47 changes: 45 additions & 2 deletions uui-components/src/inputs/__tests__/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { fireEvent, setupComponentForTest, screen } from '@epam/uui-test-utils';
import { fireEvent, setupComponentForTest, screen, userEvent } from '@epam/uui-test-utils';
import { Switch, SwitchProps } from '../Switch';

async function setupSwitch(params: Partial<SwitchProps>) {
Expand Down Expand Up @@ -29,7 +29,7 @@ describe('Switch', () => {
expect(switchElement).toBeInTheDocument();
});

it('should toggle the switch when clicked', async () => {
it('should toggle switch when clicked', async () => {
await setupSwitch({ label: 'Test Switch' });
const switchElement: HTMLInputElement = screen.getByLabelText('Test Switch');

Expand All @@ -38,6 +38,15 @@ describe('Switch', () => {
expect(switchElement.checked).toBe(true);
});

it('should toggle switch on keyboard click', async () => {
await setupSwitch({ label: 'Test Switch' });
const switchElement: HTMLInputElement = screen.getByLabelText('Test Switch');

await userEvent.type(switchElement, '{space}');

expect(switchElement.checked).toBe(true);
});

it('should call onChange handler when the switch is toggled', async () => {
const handleChange = jest.fn();
await setupSwitch({ label: 'Test Switch', onValueChange: handleChange });
Expand All @@ -59,4 +68,38 @@ describe('Switch', () => {

expect(getValueChangeAnalyticsEvent).toHaveBeenCalled();
});

it('should handle focus event', async () => {
const onFocus = jest.fn();
await setupSwitch({ label: 'Test Switch', onFocus });
const switchElement = screen.getByLabelText('Test Switch');

switchElement.focus();

expect(onFocus).toHaveBeenCalled();
expect(switchElement).toHaveFocus();
});

it('should handle blur event', async () => {
const onBlur = jest.fn();
await setupSwitch({ label: 'Test Switch', onBlur });
const switchElement = screen.getByLabelText('Test Switch');

switchElement.focus();
switchElement.blur();

expect(onBlur).toHaveBeenCalled();
expect(switchElement).not.toHaveFocus();
});

it('should handle label click', async () => {
const handleChange = jest.fn();
await setupSwitch({ label: 'Test Switch', onValueChange: handleChange });
const label = screen.getByText('Test Switch');

label.click();

expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledWith(true);
});
});
7 changes: 5 additions & 2 deletions uui-editor/src/plugins/tablePlugin/MergeToolbarContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import React from 'react';

import { ReactComponent as TableMerge } from '../../icons/table-merge.svg';
import { ToolbarButton } from '../../implementation/ToolbarButton';
import { useEditorState } from '@udecode/plate-common';
import { collapseSelection, useEditorState } from '@udecode/plate-common';
import { mergeTableCells } from '@udecode/plate-table';

export function MergeToolbarContent() {
const editor = useEditorState();

return (
<ToolbarButton
onClick={ () => mergeTableCells(editor) }
onClick={ () => {
mergeTableCells(editor);
collapseSelection(editor);
} }
icon={ TableMerge }
/>
);
Expand Down
12 changes: 12 additions & 0 deletions uui/components/inputs/__tests__/InputAddon.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { renderer } from '@epam/uui-test-utils';
import { InputAddon } from '../InputAddon';

describe('InputAddon', () => {
describe('InputAddon', () => {
it('should be rendered correctly', () => {
const tree = renderer.create(<InputAddon content="Test Content" cx="custom-class" />).toJSON();
expect(tree).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`InputAddon InputAddon should be rendered correctly 1`] = `
<div
className="prefixWrapper root custom-class uui-input-addon"
>
Test Content
</div>
`;

0 comments on commit 443e1eb

Please sign in to comment.