diff --git a/packages/uniforms-antd/__tests__/DateField.tsx b/packages/uniforms-antd/__tests__/DateField.tsx index 28abf3d5e..f4717d6b6 100644 --- a/packages/uniforms-antd/__tests__/DateField.tsx +++ b/packages/uniforms-antd/__tests__/DateField.tsx @@ -1,149 +1,166 @@ -import DatePicker from 'antd/lib/date-picker'; +import { fireEvent, screen } from '@testing-library/react'; +import userEvent, { + PointerEventsCheckLevel, +} from '@testing-library/user-event'; import moment from 'moment'; import React from 'react'; import { DateField } from 'uniforms-antd'; +import { renderWithZod } from 'uniforms/__suites__'; +import { z } from 'zod'; -import createContext from './_createContext'; -import mount from './_mount'; +const getClosestInput = (text: string) => + screen.getByText(text).closest('.ant-row')?.querySelector('input'); test(' - renders an input', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')).toBeInTheDocument(); }); test(' - default props override', () => { - const pickerProps = { showTime: false, style: {} }; - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker).props()).toEqual( - expect.objectContaining(pickerProps), - ); + const pickerProps = { name: 'y' }; + renderWithZod({ + // @ts-ignore -- passing the same prop for testing purposes + element: , + schema: z.object({ y: z.date() }), + }); + expect(screen.getByText('Y')).toBeInTheDocument(); }); test(' - renders a input with correct id (inherited)', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('id')).toBeTruthy(); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')?.id).toBeTruthy(); }); test(' - renders a input with correct id (specified)', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('id')).toBe('y'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')?.id).toBe('y'); }); test(' - renders a input with correct name', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('name')).toBe('x'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')?.name).toBe('x'); }); test(' - renders an input with correct disabled state', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('disabled')).toBe(true); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')?.disabled).toBe(true); }); -test(' - renders an input with correct readOnly state', () => { +test(' - renders an input with correct readOnly state', async () => { const onChange = jest.fn(); - const now = moment(); - const element = ; - const wrapper = mount( - element, - createContext({ x: { type: Date } }, { onChange }), - ); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - // @ts-expect-error - expect(wrapper.find(DatePicker).prop('onChange')(now)).toBeFalsy(); + renderWithZod({ + element: , + onChange, + schema: z.object({ x: z.date() }), + }); + + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + fireEvent.mouseDown(input!); + fireEvent.change(input!, { target: { value: now } }); expect(onChange).not.toHaveBeenCalled(); }); test(' - renders a input with correct label (specified)', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find('label')).toHaveLength(1); - expect(wrapper.find('label').text()).toBe('DateFieldLabel'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(screen.getByText('DateFieldLabel')).toBeInTheDocument(); }); test(' - renders a input with correct value (default)', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('value')).toBe(undefined); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + expect(input?.value).toBe(''); }); test(' - renders a input with correct value (model)', () => { - const now = moment(); - const element = ; - const wrapper = mount( - element, - createContext({ x: { type: Date } }, { model: { x: now } }), - ); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('value')).toEqual(now); + const now = moment('2024-01-01 12:00:00'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + model: { x: now.toDate() }, + }); + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + expect(input?.value).toBe(moment(now).format('YYYY-MM-DD HH:mm:ss')); }); test(' - renders a input with correct value (specified)', () => { - const now = moment(); - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - expect(wrapper.find(DatePicker).prop('value')).toEqual(now); + const now = moment('2024-01-01 12:00:00'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + expect(input?.value).toBe(moment(now).format('YYYY-MM-DD HH:mm:ss')); }); -test(' - renders a input which correctly reacts on change', () => { +// FIXME: This test is broken. +test(' - renders a input which correctly reacts on change', async () => { const onChange = jest.fn(); - - const now = moment(); - const element = ; - const wrapper = mount( - element, - createContext({ x: { type: Date } }, { onChange }), - ); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - // @ts-expect-error - expect(wrapper.find(DatePicker).prop('onChange')(now)).toBeFalsy(); + const now = moment('2024-01-01 12:00:00'); + renderWithZod({ + element: , + onChange, + schema: z.object({ x: z.date() }), + }); + + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + await userEvent.click(input!); + await userEvent.type(input!, now.format('YYYY-MM-DD HH:mm:ss')); + const ok = screen.getByText('Ok'); + await userEvent.click(ok, { + pointerEventsCheck: PointerEventsCheckLevel.Never, + }); expect(onChange).toHaveBeenLastCalledWith('x', now.toDate()); }); -test(' - renders a input which correctly reacts on change (empty)', () => { +test(' - renders a input which correctly reacts on change (empty)', async () => { const onChange = jest.fn(); - - const element = ; - const wrapper = mount( - element, - createContext({ x: { type: Date } }, { onChange }), - ); - - expect(wrapper.find(DatePicker)).toHaveLength(1); - // @ts-expect-error - expect(wrapper.find(DatePicker).prop('onChange')(undefined)).toBeFalsy(); + renderWithZod({ + element: , + onChange, + schema: z.object({ x: z.date() }), + }); + + const input = getClosestInput('X'); + expect(input).toBeInTheDocument(); + await userEvent.click(input!); + const clear = input?.parentElement?.querySelector('.ant-picker-clear'); + await userEvent.click(clear!); expect(onChange).toHaveBeenLastCalledWith('x', undefined); }); test(' - renders a wrapper with unknown props', () => { - const element = ; - const wrapper = mount(element, createContext({ x: { type: Date } })); - - expect(wrapper.find(DatePicker).prop('data-x')).toBe('x'); - expect(wrapper.find(DatePicker).prop('data-y')).toBe('y'); - expect(wrapper.find(DatePicker).prop('data-z')).toBe('z'); + renderWithZod({ + element: , + schema: z.object({ x: z.date() }), + }); + expect(getClosestInput('X')?.getAttribute('data-x')).toBe('x'); + expect(getClosestInput('X')?.getAttribute('data-y')).toBe('y'); + expect(getClosestInput('X')?.getAttribute('data-z')).toBe('z'); });