|
1 | 1 | import move from './move' |
2 | | -import { getIn, setIn, MutableState, Tools } from 'final-form' |
| 2 | +import { getIn, setIn, MutableState } from 'final-form' |
| 3 | +import { createMockTools } from './testUtils' |
3 | 4 |
|
4 | 5 | describe('move', () => { |
5 | | - const getOp = (from, to: any) => { |
| 6 | + const getOp = (from: any, to: any) => { |
6 | 7 | const changeValue = jest.fn() |
7 | | - move(['foo', from, to], { fields: {} }, { changeValue } as unknown as Tools<any>) |
| 8 | + const mockTools = createMockTools({ changeValue }) |
| 9 | + move(['foo', from, to], { fields: {} } as any, mockTools) |
8 | 10 | return changeValue.mock.calls[0][2] |
9 | 11 | } |
10 | 12 |
|
11 | 13 | it('should do nothing if from and to are equal', () => { |
12 | 14 | const changeValue = jest.fn() |
13 | | - const result = move(['foo', 1, 1], { fields: {} }, { changeValue } as unknown as Tools<any>) |
| 15 | + const mockTools = createMockTools({ changeValue }) |
| 16 | + const result = move(['foo', 1, 1], { fields: {} } as any, mockTools) |
14 | 17 | expect(result).toBeUndefined() |
15 | 18 | expect(changeValue).not.toHaveBeenCalled() |
16 | 19 | }) |
17 | 20 |
|
18 | 21 | it('should call changeValue once', () => { |
19 | 22 | const changeValue = jest.fn() |
20 | | - const state: MutableState<any> = { fields: {} } |
21 | | - const result = move(['foo', 0, 2], state, { changeValue } as unknown as Tools<any>) |
| 23 | + const state: MutableState<any> = { fields: {} } as any |
| 24 | + const mockTools = createMockTools({ changeValue }) |
| 25 | + const result = move(['foo', 0, 2], state, mockTools) |
22 | 26 | expect(result).toBeUndefined() |
23 | 27 | expect(changeValue).toHaveBeenCalled() |
24 | 28 | expect(changeValue).toHaveBeenCalledTimes(1) |
@@ -85,7 +89,7 @@ describe('move', () => { |
85 | 89 | } |
86 | 90 | } |
87 | 91 | } |
88 | | - move(['foo', 0, 2], state, { changeValue } as unknown as Tools<any>) |
| 92 | + move(['foo', 0, 2], state, createMockTools({ changeValue })) |
89 | 93 | expect(state).toEqual({ |
90 | 94 | formState: { |
91 | 95 | values: { |
@@ -161,7 +165,7 @@ describe('move', () => { |
161 | 165 | } |
162 | 166 | } |
163 | 167 | } |
164 | | - move(['foo', 2, 0], state, { changeValue } as unknown as Tools<any>) |
| 168 | + move(['foo', 2, 0], state, createMockTools({ changeValue })) |
165 | 169 | expect(state).toEqual({ |
166 | 170 | formState: { |
167 | 171 | values: { |
@@ -258,7 +262,7 @@ describe('move', () => { |
258 | 262 | } as any |
259 | 263 | } |
260 | 264 | } |
261 | | - move(['foo', 0, 2], state, { changeValue } as unknown as Tools<any>) |
| 265 | + move(['foo', 0, 2], state, createMockTools({ changeValue })) |
262 | 266 | expect(state).toMatchObject({ |
263 | 267 | formState: { |
264 | 268 | values: { |
@@ -379,7 +383,7 @@ describe('move', () => { |
379 | 383 | } as any |
380 | 384 | } |
381 | 385 | } |
382 | | - move(['foo', 2, 0], state, { changeValue } as unknown as Tools<any>) |
| 386 | + move(['foo', 2, 0], state, createMockTools({ changeValue })) |
383 | 387 | expect(state).toMatchObject({ |
384 | 388 | formState: { |
385 | 389 | values: { |
@@ -471,7 +475,7 @@ describe('move', () => { |
471 | 475 | } as any |
472 | 476 | } |
473 | 477 | } |
474 | | - move(['foo', 0, 1], state, { changeValue } as unknown as Tools<any>) |
| 478 | + move(['foo', 0, 1], state, createMockTools({ changeValue })) |
475 | 479 | expect(state).toMatchObject({ |
476 | 480 | formState: { |
477 | 481 | values: { |
@@ -510,8 +514,8 @@ describe('move', () => { |
510 | 514 | const state: MutableState<any> = { |
511 | 515 | formState: { |
512 | 516 | values: { |
513 | | - foo: [{ dog: 'apple dog', cat: 'apple cat', colors: [{ name: 'red'}, { name: 'blue'}], deep: { inside: { rock: 'black'}} }, |
514 | | - { dog: 'banana dog', mouse: 'mickey', deep: { inside: { axe: 'golden' }} }] |
| 517 | + foo: [{ dog: 'apple dog', cat: 'apple cat', colors: [{ name: 'red' }, { name: 'blue' }], deep: { inside: { rock: 'black' } } }, |
| 518 | + { dog: 'banana dog', mouse: 'mickey', deep: { inside: { axe: 'golden' } } }] |
515 | 519 | } |
516 | 520 | }, |
517 | 521 | fields: { |
@@ -557,12 +561,12 @@ describe('move', () => { |
557 | 561 | } as any, |
558 | 562 | } |
559 | 563 | } |
560 | | - move(['foo', 0, 1], state, { changeValue } as unknown as Tools<any>) |
| 564 | + move(['foo', 0, 1], state, createMockTools({ changeValue })) |
561 | 565 | expect(state).toMatchObject({ |
562 | 566 | formState: { |
563 | 567 | values: { |
564 | | - foo: [{ dog: 'banana dog', mouse: 'mickey', deep: { inside: { axe: 'golden' }} }, |
565 | | - { dog: 'apple dog', cat: 'apple cat', colors: [{ name: 'red'}, { name: 'blue'}], deep: { inside: { rock: 'black'}} }] |
| 568 | + foo: [{ dog: 'banana dog', mouse: 'mickey', deep: { inside: { axe: 'golden' } } }, |
| 569 | + { dog: 'apple dog', cat: 'apple cat', colors: [{ name: 'red' }, { name: 'blue' }], deep: { inside: { rock: 'black' } } }] |
566 | 570 | } |
567 | 571 | }, |
568 | 572 | fields: { |
@@ -660,7 +664,7 @@ describe('move', () => { |
660 | 664 | } |
661 | 665 | } |
662 | 666 | } |
663 | | - move(['foo', 0, 2], state, { changeValue } as unknown as Tools<any>) |
| 667 | + move(['foo', 0, 2], state, createMockTools({ changeValue })) |
664 | 668 | expect(state.fields['foo[0]'].change()).toBe('foo[0]') |
665 | 669 | expect(state.fields['foo[1]'].change()).toBe('foo[1]') |
666 | 670 | expect(state.fields['foo[2]'].change()).toBe('foo[2]') |
|
0 commit comments