import React from 'react'; import { renderToString } from 'react-dom/server'; import { useForm } from '../useForm'; describe('useForm with SSR', () => { it('should not output error', () => { const Component = () => { const { register } = useForm<{ test: string; }>(); return (
); }; const spy = jest.spyOn(console, 'error'); expect(renderToString()).toEqual( '
', ); expect(spy).not.toHaveBeenCalled(); }); it('should display error with errors prop', () => { const App = () => { const { register, formState: { errors }, } = useForm<{ test: string; }>({ errors: { test: { type: 'test', message: 'test error' }, }, }); return (
{errors.test && errors.test.message}
); }; expect(renderToString()).toEqual( '
test error
', ); }); it('should not pass down constrained API for server side rendering', () => { const App = () => { const { register } = useForm<{ test: string; }>(); return (
); }; expect(renderToString()).toEqual('
'); }); it('should pass down constrained API for server side rendering', () => { const App = () => { const { register } = useForm<{ test: string; }>({ shouldUseNativeValidation: true, }); return (
); }; expect(renderToString()).toEqual('
'); }); it('should support progress enhancement for form', () => { const App = () => { const { register } = useForm<{ test: string; }>({ progressive: true, }); return (
); }; expect(renderToString()).toEqual( '
', ); }); });