diff --git a/packages/react/src/components/Checkbox/Checkbox.test.tsx b/packages/react/src/components/Checkbox/Checkbox.test.tsx
index 14e5ea382d163..eb3ab0990e11c 100644
--- a/packages/react/src/components/Checkbox/Checkbox.test.tsx
+++ b/packages/react/src/components/Checkbox/Checkbox.test.tsx
@@ -1,11 +1,10 @@
import * as React from 'react';
-import { create } from '@fluentui/utilities/lib/test';
-import { mount, ReactWrapper } from 'enzyme';
+import { render } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import { Checkbox } from './Checkbox';
import { isConformant } from '../../common/isConformant';
import { resetIds } from '@fluentui/utilities';
-import type { ReactTestRenderer } from 'react-test-renderer';
import type { IRefObject } from '@fluentui/utilities';
import type { ICheckbox } from './Checkbox.types';
@@ -27,41 +26,27 @@ const IndeterminateControlledCheckbox: React.FunctionComponent = () => {
};
describe('Checkbox', () => {
- let renderedComponent: ReactTestRenderer | undefined;
- let component: ReactWrapper | undefined;
-
beforeEach(() => {
resetIds();
});
afterEach(() => {
checkbox = undefined;
- if (renderedComponent) {
- renderedComponent.unmount();
- renderedComponent = undefined;
- }
- if (component) {
- component.unmount();
- component = undefined;
- }
});
it('renders unchecked correctly', () => {
- renderedComponent = create();
- const tree = renderedComponent.toJSON();
- expect(tree).toMatchSnapshot();
+ const { container } = render();
+ expect(container).toMatchSnapshot();
});
it('renders checked correctly', () => {
- renderedComponent = create();
- const tree = renderedComponent.toJSON();
- expect(tree).toMatchSnapshot();
+ const { container } = render();
+ expect(container).toMatchSnapshot();
});
it('renders indeterminate correctly', () => {
- renderedComponent = create();
- const tree = renderedComponent.toJSON();
- expect(tree).toMatchSnapshot();
+ const { container } = render();
+ expect(container).toMatchSnapshot();
});
isConformant({
@@ -70,160 +55,150 @@ describe('Checkbox', () => {
});
it('respects id prop', () => {
- component = mount();
- expect(component.find('input').prop('id')).toEqual('my-checkbox');
+ const { getByRole } = render(
+ ,
+ );
+ expect(getByRole('checkbox').id).toEqual('my-checkbox');
});
it('defaults to unchecked non-indeterminate', () => {
- component = mount();
+ const { getByRole } = render();
- const input = component.find('input');
- expect(input.prop('checked')).toBe(false);
+ expect(getByRole('checkbox').getAttribute('checked')).toBeNull();
expect(checkbox!.checked).toBe(false);
expect(checkbox!.indeterminate).toBe(false);
});
it('respects defaultChecked prop', () => {
- component = mount();
+ const { getByRole } = render();
- const input = component.find('input');
- expect(input.prop('checked')).toBe(true);
+ expect(getByRole('checkbox').getAttribute('checked')).not.toBeNull();
expect(checkbox!.checked).toBe(true);
});
it('ignores defaultChecked updates', () => {
- component = mount();
- component.setProps({ defaultChecked: false });
- expect(component.find('input').prop('checked')).toBe(true);
+ const firstCheckbox = render();
+ firstCheckbox.rerender();
+ expect(firstCheckbox.container.querySelector('.is-checked')).toBeTruthy();
expect(checkbox!.checked).toBe(true);
- component.unmount();
- component = mount();
- component.setProps({ defaultChecked: true });
- expect(component.find('input').prop('checked')).toBe(false);
+ const secondCheckbox = render();
+ secondCheckbox.rerender();
+ expect(secondCheckbox.container.querySelector('.is-checked')).toBeFalsy();
expect(checkbox!.checked).toBe(false);
});
it('respects checked prop', () => {
- component = mount();
+ const { getByRole } = render();
- const input = component.find('input');
- expect(input.prop('checked')).toBe(true);
+ expect(getByRole('checkbox').getAttribute('checked')).not.toBeNull();
expect(checkbox!.checked).toBe(true);
});
it('respects checked updates', () => {
- component = mount();
+ const { container, rerender } = render();
+ rerender();
- component.setProps({ checked: false });
- expect(component.find('input').prop('checked')).toBe(false);
+ expect(container.querySelector('.is-checked')).toBeFalsy();
expect(checkbox!.checked).toBe(false);
});
it('automatically updates on change when uncontrolled', () => {
const onChange = jest.fn();
- component = mount();
+ const { container, getByRole } = render();
- component.find('input').simulate('change');
+ userEvent.click(getByRole('checkbox'));
- expect(component.find('input').prop('checked')).toBe(true);
+ expect(container.querySelector('.is-checked')).toBeTruthy();
expect(checkbox!.checked).toBe(true);
expect(onChange).toHaveBeenCalledTimes(1);
});
it('does not automatically update on change when controlled', () => {
const onChange = jest.fn();
- component = mount();
+ const { container, getByRole, rerender } = render(
+ ,
+ );
- component.find('input').simulate('change');
+ userEvent.click(getByRole('checkbox'));
// doesn't update automatically (but calls onChange)
- expect(component.find('input').prop('checked')).toBe(false);
+ expect(container.querySelector('.is-checked')).toBeFalsy();
expect(checkbox!.checked).toBe(false);
expect(onChange).toHaveBeenCalledTimes(1);
// updates when props update
- component.setProps({ checked: true });
- expect(component.find('input').prop('checked')).toBe(true);
+ rerender();
+ expect(container.querySelector('.is-checked')).toBeTruthy();
expect(checkbox!.checked).toBe(true);
// doesn't call onChange for props update
expect(onChange).toHaveBeenCalledTimes(1);
});
it('respects defaultIndeterminate prop', () => {
- component = mount();
+ render();
expect(checkbox!.indeterminate).toEqual(true);
});
it('respects defaultIndeterminate prop when defaultChecked is true', () => {
- component = mount();
+ render();
expect(checkbox!.indeterminate).toEqual(true);
});
it('ignores defaultIndeterminate updates', () => {
- component = mount();
- component.setProps({ defaultIndeterminate: false });
+ const firstCheckbox = render();
+ firstCheckbox.rerender();
expect(checkbox!.indeterminate).toEqual(true);
- component.unmount();
- component = mount();
- component.setProps({ defaultIndeterminate: true });
+ const secondCheckbox = render();
+ secondCheckbox.rerender();
expect(checkbox!.checked).toBe(false);
expect(checkbox!.indeterminate).toEqual(false);
});
it('removes uncontrolled indeterminate state', () => {
- component = mount();
+ const { container, getByRole } = render();
- let input = component.find('input');
- expect(input.prop('checked')).toBe(false);
+ expect(container.querySelector('.is-checked')).toBeFalsy();
expect(checkbox!.indeterminate).toEqual(true);
- input.simulate('change');
+ userEvent.click(getByRole('checkbox'));
- // get an updated ReactWrapper for the input (otherwise it would be out of sync)
- input = component.find('input');
- expect(input.prop('checked')).toBe(false);
+ expect(container.querySelector('.is-checked')).toBeFalsy();
expect(checkbox!.indeterminate).toEqual(false);
});
it('renders with indeterminate when controlled', () => {
- component = mount();
+ const { getByRole } = render();
- let input = component.find('input');
expect(checkbox!.indeterminate).toEqual(true);
- input.simulate('change', { target: { checked: true } });
+ userEvent.click(getByRole('checkbox'));
- input = component.find('input');
expect(checkbox!.indeterminate).toEqual(false);
});
it('removes controlled indeterminate', () => {
- component = mount();
+ const { getByRole } = render();
- let input = component.find('input');
expect(checkbox!.indeterminate).toEqual(true);
expect(checkbox!.checked).toEqual(false);
- input.simulate('change');
+ userEvent.click(getByRole('checkbox'));
- input = component.find('input');
expect(checkbox!.indeterminate).toEqual(false);
expect(checkbox!.checked).toEqual(false);
});
it("doesn't remove controlled indeterminate when no onChange provided", () => {
- component = mount();
+ const { getByRole } = render();
- let input = component.find('input');
expect(checkbox!.indeterminate).toEqual(true);
- input.simulate('change');
+ userEvent.click(getByRole('checkbox'));
- input = component.find('input');
expect(checkbox!.indeterminate).toEqual(true);
});
});
diff --git a/packages/react/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap b/packages/react/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap
index c42bd6f70e72c..438b502b96a8d 100644
--- a/packages/react/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap
+++ b/packages/react/src/components/Checkbox/__snapshots__/Checkbox.test.tsx.snap
@@ -1,514 +1,515 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Checkbox renders checked correctly 1`] = `
-
-
-
+
+
+
+
+
+ Standard checkbox
+
+
+
`;
exports[`Checkbox renders indeterminate correctly 1`] = `
-
-
-
+
+
+
+
+
+ Standard checkbox
+
+
+
`;
exports[`Checkbox renders unchecked correctly 1`] = `
-
-
- Standard checkbox
-
-
+
+
+
+
+
+ Standard checkbox
+
+
+
`;