import * as React from 'react'; import * as enzyme from 'enzyme'; import DayjsUtils from '@date-io/dayjs'; import LuxonUtils from '@date-io/luxon'; import MomentUtils from '@date-io/moment'; import DateFnsUtils from '@date-io/date-fns'; import TextField from '@material-ui/core/TextField'; import { IUtils } from '@date-io/core/IUtils'; import { TransitionProps } from '@material-ui/core/transitions'; import { queryHelpers, Matcher, MatcherOptions } from '@testing-library/react/pure'; import LocalizationProvider from '../LocalizationProvider'; import { DatePickerProps } from '../DatePicker'; import { BasePickerProps } from '../typings/BasePicker'; import { createClientRender } from './createClientRender'; export const queryByMuiTest = queryHelpers.queryByAttribute.bind(null, 'data-mui-test'); export const queryAllByMuiTest = queryHelpers.queryAllByAttribute.bind(null, 'data-mui-test'); export function getAllByMuiTest( id: Matcher, container: HTMLElement = document.body, options?: MatcherOptions ): Element[] { const els = queryAllByMuiTest(container, id, options); if (!els.length) { throw queryHelpers.getElementError( `Unable to find an element by: [data-mui-test="${id}"]`, container ); } return els; } export function getByMuiTest(...args: Parameters): Element { const result = getAllByMuiTest(...args); if (result.length > 0) { return result[0]; } throw queryHelpers.getElementError( `Unable to find an element by: [data-mui-test="${args[0]}"]`, document.body ); } export const FakeTransitionComponent = React.forwardRef( function FakeTransitionComponent({ children }, ref) { // set tabIndex in case it is used as a child of return (
{children}
); } ); interface WithUtilsProps { utils: IUtils; } function getUtilClass() { switch (process.env.UTILS) { case 'date-fns': return DateFnsUtils; case 'dayjs': return DayjsUtils; case 'luxon': return LuxonUtils; case 'moment': return MomentUtils; default: return DateFnsUtils; } } export const UtilClassToUse: any = getUtilClass(); export const utilsToUse: IUtils = new UtilClassToUse(); const getComponentWithUtils =

(element: React.ReactElement

) => React.cloneElement(element, { utils: utilsToUse } as any); export const shallow =

(element: React.ReactElement

) => enzyme.shallow(getComponentWithUtils(element)); export const mount =

(element: React.ReactElement

) => enzyme.mount({element}); type RenderPicker = ( props: Pick, 'onChange' | 'value'> & { renderInput: DatePickerProps['renderInput']; } ) => React.ReactElement; function createPickerWithState(defaultValue: TValue, renderFn: RenderPicker) { const PickerWithState = () => { const [value, setDate] = React.useState(defaultValue); return renderFn({ value, onChange: (date) => setDate(date), renderInput: (props) => , }); }; return ; } export function mountPickerWithState( defaultValue: TValue, renderPicker: RenderPicker ) { return mount(createPickerWithState(defaultValue, renderPicker)); } export function renderPickerWithState( defaultValue: TValue, renderPicker: RenderPicker, clientRenderOptions?: { strict?: boolean } ) { const render = createClientRender(clientRenderOptions); return render(createPickerWithState(defaultValue, renderPicker)); } export function shallowRender(render: (props: any) => React.ReactElement) { return enzyme.shallow(render({ utils: utilsToUse, classes: {} as any, theme: {} as any })); } // toHaveBeenCalledWith doesn't work with moment because of changing some internal props export function toHaveBeenCalledExceptMoment(mock: jest.Mock, params: any[]) { if (process.env.UTILS === 'moment') { return expect(mock).toHaveBeenCalled(); } return expect(mock).toHaveBeenCalledWith(...params); } export function itOnlyIf(condition: boolean) { return condition ? it : it.skip; }