import React from 'react'; import { render, fireEvent } from '@testing-library/react'; import { axe } from 'jest-axe'; import 'jest-styled-components'; import 'jest-axe/extend-expect'; import 'regenerator-runtime/runtime'; import { Grommet } from '../../Grommet'; import { CheckBox } from '..'; describe('CheckBox', () => { test('should not have accessibility violations', async () => { const { container } = render( , ); const results = await axe(container); expect(container.firstChild).toMatchSnapshot(); expect(results).toHaveNoViolations(); }); test('label should not have accessibility violations', async () => { const { container } = render( , ); const results = await axe(container); expect(container.firstChild).toMatchSnapshot(); expect(results).toHaveNoViolations(); }); test('renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('label renders', () => { const { container } = render( test label} /> , ); expect(container.firstChild).toMatchSnapshot(); }); test('checked renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('defaultChecked', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('disabled renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('reverse renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('toggle renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('reverse toggle fill', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('indeterminate renders', () => { const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('indeterminate checked warns', () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); render( , ); expect(warnSpy).toBeCalledWith( 'Checkbox cannot be "checked" and "indeterminate" at the same time.', ); warnSpy.mockReset(); warnSpy.mockRestore(); }); test('indeterminate toggle warns', () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); render( , ); expect(warnSpy).toBeCalledWith( 'Checkbox of type toggle does not have "indeterminate" state.', ); warnSpy.mockReset(); warnSpy.mockRestore(); }); test('controlled', () => { const { container, getByText } = render( , ); expect(container.firstChild).toMatchSnapshot(); fireEvent.click(getByText('test-label')); expect(container.firstChild).toMatchSnapshot(); }); test('custom theme', () => { const customTheme = { checkBox: { pad: { horizontal: 'small', vertical: 'xsmall', }, }, }; const { container } = render( , ); expect(container.firstChild).toMatchSnapshot(); }); test('renders custom checked icon', () => { const CustomCheckedIcon = () => ; const customTheme = { checkBox: { icons: { checked: CustomCheckedIcon, }, }, }; const { container, getByTestId } = render( , ); expect(container.firstChild).toMatchSnapshot(); expect(getByTestId('custom-checkBox-icon')).toBeDefined(); }); test('renders a11yTitle and aria-label', async () => { const LABEL = 'Label'; const { container, getByLabelText } = render( , ); expect(getByLabelText(LABEL)).toBeTruthy(); expect(getByLabelText(`${LABEL}-2`)).toBeTruthy(); expect(container.firstChild).toMatchSnapshot(); }); });