File size: 4,979 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import iterateFieldsByAction from '../../logic/iterateFieldsByAction';

describe('iterateFieldsByAction', () => {
  it('should focus on the first error it encounter', () => {
    const focus = jest.fn();
    iterateFieldsByAction(
      {
        test: {
          _f: {
            name: 'test',
            ref: {
              name: 'test',
              focus,
            },
          },
        },
      },
      (ref) => {
        ref.focus && ref.focus();
        return 1;
      },
    );

    expect(focus).toBeCalled();
  });

  it('should focus on first option when options input error encounters', () => {
    const focus = jest.fn();
    iterateFieldsByAction(
      {
        test: {
          _f: {
            name: 'test',
            ref: {
              name: 'test',
            },
            refs: [
              {
                focus,
              } as unknown as HTMLInputElement,
            ],
          },
        },
      },
      (ref) => {
        ref.focus && ref.focus();
        return 1;
      },
    );

    expect(focus).toBeCalled();
  });

  it('should not call focus when field is undefined', () => {
    expect(() => {
      iterateFieldsByAction(
        {
          test: undefined,
        },
        (ref) => {
          ref.focus && ref.focus();
          return 1;
        },
      );
    }).not.toThrow();
  });

  it('should focus on the first error it encounter and not the second', () => {
    const focus = jest.fn();
    iterateFieldsByAction(
      {
        first: {
          _f: {
            name: 'first',
            ref: {
              name: 'first',
              focus,
            },
          },
        },
        second: {
          _f: {
            name: 'second',
            ref: {
              name: 'second',
              focus,
            },
          },
        },
      },
      (ref) => {
        // @ts-expect-error we want to test with what focus was called
        ref.focus && ref.focus(ref.name);
        return 1;
      },
    );
    expect(focus).toBeCalledWith('first');
    expect(focus).not.toBeCalledWith('second');
  });

  it('should recursively drill into objects', () => {
    const focus = jest.fn();
    iterateFieldsByAction(
      {
        test: {
          name: {
            first: {
              _f: {
                name: 'name.first',
                ref: {
                  name: 'first',
                  focus,
                },
              },
            },
            last: {
              _f: {
                name: 'name.last',
                ref: {
                  name: 'last',
                  focus,
                },
              },
            },
          },
        },
      },
      (ref, key) => {
        if (key === 'name.last') {
          // @ts-expect-error we want to test with what focus was called
          ref.focus && ref.focus(ref.name);
          return 1;
        }
        return;
      },
    );
    expect(focus).not.toBeCalledWith('first');
    expect(focus).toBeCalledWith('last');
  });

  it('should should recursively drill into objects and break out of all loops on first focus', () => {
    const focus = jest.fn();
    const notFocus = jest.fn();
    iterateFieldsByAction(
      {
        personal: {
          name: {
            first: {
              _f: {
                name: 'name.first',
                ref: {
                  name: 'first',
                  focus: notFocus,
                },
              },
            },
            last: {
              _f: {
                name: 'name.last',
                ref: {
                  name: 'last',
                  focus,
                },
              },
            },
          },
          phone: {
            _f: {
              name: 'phone',
              ref: {
                name: 'phone',
                focus: notFocus,
              },
            },
          },
          address: {
            line1: {
              _f: {
                name: 'address.line1',
                ref: {
                  name: 'line1',
                  focus: notFocus,
                },
              },
            },
          },
        },
      },
      (ref, key) => {
        // @ts-expect-error we want to test with what focus was called
        ref.focus && ref.focus(ref.name);
        return key === 'name.last' ? 1 : undefined;
      },
    );
    // 'focus' should be called on 'last' and never again
    expect(focus).not.toBeCalledWith('first'); // not valid
    expect(focus).toBeCalledWith('last'); // valid
    expect(focus).not.toBeCalledWith('phone'); // stopped
    expect(focus).not.toBeCalledWith('line1');
    // 'notFocus' should be called on the first, then never again
    expect(notFocus).toBeCalledWith('first'); // not valid
    expect(notFocus).not.toBeCalledWith('last'); // valid
    expect(notFocus).not.toBeCalledWith('phone'); // stopped
    expect(notFocus).not.toBeCalledWith('line1');
  });
});