File size: 1,404 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React from 'react';
import { renderToString } from 'react-dom/server';

import { useController } from '../useController';
import { useForm } from '../useForm';
import { FormProvider, useFormContext } from '../useFormContext';
import { useFormState } from '../useFormState';
import { useWatch } from '../useWatch';

describe('FormProvider', () => {
  it('should work correctly with Controller, useWatch, useFormState.', () => {
    const App = () => {
      const { field } = useController({
        name: 'test',
        defaultValue: '',
      });
      return <input {...field} />;
    };

    const TestWatch = () => {
      const value = useWatch({
        name: 'test',
      });

      return <p>{value}</p>;
    };

    const TestFormState = () => {
      const { isDirty } = useFormState();

      return <div>{isDirty ? 'yes' : 'no'}</div>;
    };

    const TestUseFormContext = () => {
      const methods = useFormContext();
      methods.register('test');
      return null;
    };

    const Component = () => {
      const methods = useForm();

      return (
        <FormProvider {...methods}>
          <App />
          <TestUseFormContext />
          <TestWatch />
          <TestFormState />
        </FormProvider>
      );
    };

    const output = renderToString(<Component />);

    expect(output).toEqual('<input name="test" value=""/><p></p><div>no</div>');
  });
});