Skip to content

Commit

Permalink
refactor(react): update radio tile tests to rtl (#12108)
Browse files Browse the repository at this point in the history
* refactor(react): update radio tile tests to rtl

* chore(react): remove screen.debug()

* chore(react): remove old tests

* chore(react): update snapshots

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
abbeyhrt and kodiakhq[bot] committed Sep 23, 2022
1 parent dca8f9f commit 04853f0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 141 deletions.
3 changes: 0 additions & 3 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Expand Up @@ -5724,9 +5724,6 @@ Map {
"className": Object {
"type": "string",
},
"defaultChecked": Object {
"type": "bool",
},
"disabled": Object {
"type": "bool",
},
Expand Down
184 changes: 59 additions & 125 deletions packages/react/src/components/RadioTile/RadioTile-test.js
@@ -1,156 +1,90 @@
/**
* Copyright IBM Corp. 2016, 2018
* Copyright IBM Corp. 2022
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import RadioButton from '../RadioButton';
import { mount } from 'enzyme';

const prefix = 'cds';

const render = (props) =>
mount(
<RadioButton
{...props}
className="extra-class"
name="test-name"
value="test-value"
labelText="testlabel"
/>
);

describe('RadioButton', () => {
describe('renders as expected', () => {
const wrapper = render({
checked: true,
import RadioTile from './RadioTile';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';

describe('RadioTile', () => {
describe('renders as expected - Component API', () => {
it('should spread extra props onto outermost element', () => {
render(<RadioTile value="standard" data-testid="test-id" />);

expect(document.querySelector('.cds--tile')).toHaveAttribute(
'data-testid',
'test-id'
);
});

const input = wrapper.find('input');
const label = wrapper.find('label');
const div = wrapper.find('div');
it('should respect checked prop', () => {
const { container } = render(
<RadioTile value="standard" checked data-testid="test-id" />
);

describe('input', () => {
it('is of type radio', () => {
expect(input.props().type).toEqual('radio');
});

it('has the expected class', () => {
expect(input.hasClass(`${prefix}--radio-button`)).toEqual(true);
});

it('has a unique id set by default', () => {
expect(input.props().id).toBeDefined();
});

it('should have checked set when checked is passed', () => {
wrapper.setProps({ checked: true });
expect(input.props().checked).toEqual(true);
});

it('should set the name prop as expected', () => {
expect(input.props().name).toEqual('test-name');
});
expect(container.firstChild).toHaveAttribute('checked');
expect(screen.getByTestId('test-id')).toHaveClass(
'cds--tile--is-selected'
);
});

describe('label', () => {
it('should set htmlFor', () => {
expect(label.props().htmlFor).toEqual(input.props().id);
});

it('should set the correct class', () => {
expect(label.props().className).toEqual(
`${prefix}--radio-button__label`
);
});

it('should render a span with the correct class for radio style', () => {
const span = label.find('span');
expect(
span.at(0).hasClass(`${prefix}--radio-button__appearance`)
).toEqual(true);
});

it('should render a span for the label text', () => {
const span = label.find('span');
expect(span.at(1).hasClass('')).toEqual(true);
expect(span.at(1).text()).toEqual('testlabel');
});

it('should render label text', () => {
wrapper.setProps({ labelText: 'test label text' });
expect(label.text()).toMatch(/test label text/);
});

it('should not have the className when no class is passed through props', () => {
expect(label.hasClass(label.props.className)).toEqual(false);
});

it('should have the className passed through props', () => {
const label = render({
className: 'extra-class',
});
expect(label.hasClass('extra-class')).toEqual(true);
});
it('should support a custom `className` prop on the outermost element', () => {
render(
<RadioTile
value="standard"
className="custom-class"
data-testid="test-id"
/>
);

expect(screen.getByTestId('test-id')).toHaveClass('custom-class');
});

describe('wrapper', () => {
it('should have the correct class', () => {
expect(div.hasClass(`${prefix}--radio-button-wrapper`)).toEqual(true);
});
it('should respect disabled prop', () => {
const { container } = render(
<RadioTile value="standard" disabled data-testid="test-id" />
);

it('should have extra classes applied', () => {
expect(div.hasClass('extra-class')).toEqual(true);
});
expect(container.firstChild).toHaveAttribute('disabled');
expect(screen.getByTestId('test-id')).toHaveClass('cds--tile--disabled');
});
});

it('should set defaultChecked as expected', () => {
const wrapper = render({
defaultChecked: true,
it('should respect id prop', () => {
render(<RadioTile value="standard" id="tile-1" />);

expect(screen.getByRole('radio')).toHaveAttribute('id', 'tile-1');
});

const input = () => wrapper.find('input');
expect(input().props().defaultChecked).toEqual(true);
wrapper.setProps({ defaultChecked: false });
expect(input().props().defaultChecked).toEqual(false);
});
it('should add name to input', () => {
render(<RadioTile value="standard" name="tile" />);

it('should set id if one is passed in', () => {
const wrapper = render({
id: 'unique-id',
expect(screen.getByRole('radio')).toHaveAttribute('name', 'tile');
});

const input = wrapper.find('input');
expect(input.props().id).toEqual('unique-id');
});

describe('events', () => {
it('should invoke onChange with expected arguments', () => {
it('should call onChange when expected', () => {
const onChange = jest.fn();
const wrapper = render({ onChange });
const input = wrapper.find('input');
const inputElement = input.instance();
render(<RadioTile value="standard" onChange={onChange} />);

inputElement.checked = true;
wrapper.find('input').simulate('change');
userEvent.click(screen.getByRole('radio'));
userEvent.type(screen.getByRole('radio'), '{space}');

const call = onChange.mock.calls[0];

expect(call[0]).toEqual('test-value');
expect(call[1]).toEqual('test-name');
expect(call[2].target).toBe(inputElement);
expect(onChange).toHaveBeenCalledTimes(2);
});
});

it('supports disabled state', () => {
const wrapper = render({
disabled: true,
it('should respect tabIndex prop', () => {
render(<RadioTile value="standard" tabIndex={-1} />);

expect(screen.getByRole('radio')).toHaveAttribute('tabIndex', '-1');
});

const input = () => wrapper.find('input');
expect(input().props().disabled).toEqual(true);
it('should respect value prop', () => {
render(<RadioTile value="standard" />);

expect(screen.getByRole('radio')).toHaveAttribute('value', 'standard');
});
});
});
12 changes: 1 addition & 11 deletions packages/react/src/components/RadioTile/RadioTile.js
Expand Up @@ -11,7 +11,6 @@ import PropTypes from 'prop-types';
import React from 'react';
import { keys, matches } from '../../internal/keyboard';
import { useFallbackId } from '../../internal/useId';
import { useFeatureFlag } from '../FeatureFlags';
import { usePrefix } from '../../internal/usePrefix';
import deprecate from '../../prop-types/deprecate';

Expand All @@ -31,7 +30,6 @@ function RadioTile({
}) {
const prefix = usePrefix();
const inputId = useFallbackId(id);
const enabled = useFeatureFlag('enable-v11-release');
const className = cx(
customClassName,
`${prefix}--tile`,
Expand All @@ -42,8 +40,6 @@ function RadioTile({
[`${prefix}--tile--disabled`]: disabled,
}
);
const inputProps = enabled ? {} : rest;
const labelProps = enabled ? rest : {};

function handleOnChange(evt) {
onChange(value, name, evt);
Expand All @@ -59,7 +55,6 @@ function RadioTile({
return (
<>
<input
{...inputProps}
checked={checked}
className={`${prefix}--tile-input`}
disabled={disabled}
Expand All @@ -71,7 +66,7 @@ function RadioTile({
type="radio"
value={value}
/>
<label {...labelProps} htmlFor={inputId} className={className}>
<label {...rest} htmlFor={inputId} className={className}>
<span className={`${prefix}--tile__checkmark`}>
<CheckmarkFilled />
</span>
Expand All @@ -97,11 +92,6 @@ RadioTile.propTypes = {
*/
className: PropTypes.string,

/**
* `true` if the `<input>` should be checked at initialization.
*/
defaultChecked: PropTypes.bool,

/**
* Specify whether the RadioTile should be disabled
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/components/Tile/next/Tile.stories.js
Expand Up @@ -150,7 +150,10 @@ export const Radio = () => {
<RadioTile value="standard" style={{ marginBottom: '.5rem' }}>
Option 1
</RadioTile>
<RadioTile value="default-selected" id="tile-2">
<RadioTile
value="default-selected"
id="tile-2"
style={{ marginBottom: '.5rem' }}>
Option 2
</RadioTile>
<RadioTile value="selected" id="tile-3">
Expand Down
2 changes: 1 addition & 1 deletion packages/react/tasks/build-test-rtl.js
Expand Up @@ -38,7 +38,7 @@ function writeTestFile(props, componentName, isSubComponent) {
// perform action to call ${prop}
expect($prop).toHaveBeenCalled();
expect(${prop}).toHaveBeenCalled();
});\n\n`;
} else {
test = `it('should respect ${prop} prop', () => {
Expand Down

0 comments on commit 04853f0

Please sign in to comment.