import { fireEvent, queryByAttribute } from '@testing-library/react' import userEvent from '@testing-library/user-event' import pick from 'lodash/pick' import * as React from 'react' import renderWithTheme from '../../tests/helpers/renderWithTheme' import CheckBoxField from './CheckBoxField' describe('component: CheckBoxField', () => { test('should generate unique id when one is not provided', () => { const { container, getByText } = renderWithTheme( ) const getById = queryByAttribute.bind(null, 'id') const labelElement = getByText('Scoreboard') as HTMLLabelElement expect(labelElement.htmlFor).toContain('scoreboard') expect(getById(container, labelElement.htmlFor)).not.toBeNull() }) test('should support margin space props', () => { const { getByTestId } = renderWithTheme( <> ) const blank = { marginTop: '', marginRight: '', marginBottom: '', marginLeft: '' } expect(getByTestId('first').parentElement?.parentElement).toHaveStyle(blank) expect(getByTestId('default').parentElement?.parentElement).toHaveStyle({ ...blank, marginTop: '8px', marginBottom: '4px', }) expect(getByTestId('override').parentElement?.parentElement).toHaveStyle({ ...blank, marginTop: '16px', }) }) test('should support flex item props', () => { const { getByTestId } = renderWithTheme( ) const checkField = getByTestId('flex-checkbox').parentElement?.parentElement as HTMLElement expect(checkField).toHaveStyle('flex: 1') expect(checkField).toHaveStyle('flex-grow: 1') expect(checkField).toHaveStyle('flex-shrink: 0') expect(checkField).toHaveStyle('flex-basis: 0') }) test('should support ref prop', () => { const ref = React.createRef() const { getByLabelText } = renderWithTheme( ) expect(getByLabelText('Oui')).toBe(ref.current) expect(ref.current).toBeChecked() }) test('should trigger onChange event', () => { const box: any = {} const onChange = jest.fn((e) => Object.assign(box, pick(e.target, ['name', 'checked']))) const { getByLabelText } = renderWithTheme( ) fireEvent.click(getByLabelText('Katastro')) expect(onChange).toHaveBeenCalledTimes(1) expect(box).toEqual({ name: 'katastro', checked: false }) }) test('should trigger onFocus event', () => { const onFocus = jest.fn() const { getByLabelText } = renderWithTheme( ) fireEvent.focus(getByLabelText('Strange Nights')) expect(onFocus).toHaveBeenCalled() }) test('should trigger onBlur event', () => { const onBlur = jest.fn() const { getByLabelText } = renderWithTheme( ) fireEvent.blur(getByLabelText('Washed.')) expect(onBlur).toHaveBeenCalled() }) describe('when disabled', () => { test('should not trigger onChange event', () => { const onChange = jest.fn() const { getByLabelText } = renderWithTheme( ) userEvent.click(getByLabelText('Flow')) expect(onChange).not.toHaveBeenCalled() }) test('should not trigger onFocus event', () => { const onFocus = jest.fn() const { getByLabelText } = renderWithTheme( ) userEvent.click(getByLabelText('Not For Sale')) expect(onFocus).not.toHaveBeenCalled() }) }) })