Skip to content

Commit 9d6d24b

Browse files
committed
Added additional isEqual test for #40
1 parent d5f945d commit 9d6d24b

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/FieldArray.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,59 @@ describe('FieldArray', () => {
315315
expect(result).toEqual(['FOO[0]', 'FOO[1]', 'FOO[2]'])
316316
})
317317

318+
it('calculate dirty/pristine using provided isEqual predicate', async () => {
319+
const renderInput = jest.fn(({ input }) => <input {...input} />)
320+
const renderFields = jest.fn(({ fields }) =>
321+
fields.map(field => (
322+
<Field
323+
name={`${field}.bar`}
324+
key={`${field}.bar`}
325+
component={renderInput}
326+
/>
327+
))
328+
)
329+
const isEqual = jest.fn(
330+
(aArray, bArray) =>
331+
!aArray.some((a, index) => a.bar !== bArray[index].bar)
332+
)
333+
TestUtils.renderIntoDocument(
334+
<Form
335+
onSubmit={onSubmitMock}
336+
initialValues={{ foo: [{ bar: 'a' }, { bar: 'b' }] }}
337+
>
338+
{() => (
339+
<form>
340+
<FieldArray
341+
name="foo"
342+
subscription={{ dirty: true }}
343+
isEqual={isEqual}
344+
>
345+
{renderFields}
346+
</FieldArray>
347+
</form>
348+
)}
349+
</Form>
350+
)
351+
expect(renderInput).toHaveBeenCalled()
352+
expect(renderInput).toHaveBeenCalledTimes(2)
353+
expect(renderInput.mock.calls[0][0].input.value).toBe('a')
354+
expect(renderInput.mock.calls[1][0].input.value).toBe('b')
355+
356+
expect(renderFields).toHaveBeenCalledTimes(1)
357+
expect(renderFields.mock.calls[0][0].meta.dirty).toBe(false)
358+
359+
// change value
360+
renderInput.mock.calls[1][0].input.onChange('c')
361+
362+
expect(renderInput).toHaveBeenCalledTimes(5)
363+
expect(renderInput.mock.calls[4][0].input.value).toBe('c')
364+
expect(renderInput.mock.calls[4][0].meta.dirty).toBe(true)
365+
366+
await sleep(1)
367+
expect(renderFields).toHaveBeenCalledTimes(3)
368+
expect(renderFields.mock.calls[2][0].meta.dirty).toBe(true)
369+
})
370+
318371
it('should allow Field components to be rendered', async () => {
319372
const renderInput = jest.fn(({ input }) => <input {...input} />)
320373
const dom = TestUtils.renderIntoDocument(

0 commit comments

Comments
 (0)