Skip to content

Commit 60e4da6

Browse files
committed
test(Form): fix race conditions in touchedFields and dirtyFields
1 parent e5c11e6 commit 60e4da6

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

test/components/Form.spec.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { nextTick, watch } from 'vue'
2-
import { describe, expect, it, beforeEach, vi } from 'vitest'
2+
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
33
import { axe } from 'vitest-axe'
44
import { flushPromises } from '@vue/test-utils'
55
import * as z from 'zod'
@@ -148,6 +148,19 @@ describe('Form', () => {
148148
})
149149
form = wrapper.setupState.form.value
150150
state = wrapper.setupState.state
151+
152+
// Wait for all mounting and initialization to complete
153+
await nextTick()
154+
await flushPromises()
155+
156+
// Ensure form state is clean
157+
form.touchedFields.clear()
158+
form.dirtyFields.clear()
159+
form.blurredFields.clear()
160+
})
161+
162+
afterEach(() => {
163+
wrapper?.unmount()
151164
})
152165

153166
it('setErrors works', async () => {
@@ -327,16 +340,18 @@ describe('Form', () => {
327340
const email = wrapper.find('#email')
328341

329342
email.trigger('focus')
343+
await nextTick()
330344
await flushPromises()
331345

332346
expect(form.touchedFields.has('email')).toBe(true)
333347
expect(form.touchedFields.has('password')).toBe(false)
334348
})
335349

336-
it('touchedFields works', async () => {
350+
it('dirtyFields works', async () => {
337351
const email = wrapper.find('#email')
338352

339353
email.trigger('change')
354+
await nextTick()
340355
await flushPromises()
341356

342357
expect(form.dirtyFields.has('email')).toBe(true)
@@ -350,6 +365,7 @@ describe('Form', () => {
350365
const email = wrapper.find('#email')
351366

352367
email.trigger('blur')
368+
await nextTick()
353369
await flushPromises()
354370

355371
expect(form.blurredFields.has('email')).toBe(true)
@@ -363,6 +379,7 @@ describe('Form', () => {
363379
watch(() => form.touchedFields, mockWatchCallback, { deep: true })
364380

365381
email.trigger('focus')
382+
await nextTick()
366383
await flushPromises()
367384
expect(mockWatchCallback).toHaveBeenCalled()
368385
})
@@ -374,6 +391,7 @@ describe('Form', () => {
374391
watch(() => form.touchedFields, mockWatchCallback, { deep: true })
375392

376393
email.trigger('change')
394+
await nextTick()
377395
await flushPromises()
378396
expect(mockWatchCallback).toHaveBeenCalled()
379397
})
@@ -385,6 +403,7 @@ describe('Form', () => {
385403
watch(() => form.blurredFields, mockWatchCallback, { deep: true })
386404

387405
email.trigger('blur')
406+
await nextTick()
388407
await flushPromises()
389408
expect(mockWatchCallback).toHaveBeenCalled()
390409
})
@@ -395,6 +414,7 @@ describe('Form', () => {
395414
watch(() => form.dirtyFields, mockWatchCallback, { deep: true })
396415

397416
email.trigger('change')
417+
await nextTick()
398418
await flushPromises()
399419
expect(mockWatchCallback).toHaveBeenCalled()
400420
})
@@ -404,6 +424,7 @@ describe('Form', () => {
404424
expect(form.dirty).toBe(false)
405425

406426
email.trigger('change')
427+
await nextTick()
407428
await flushPromises()
408429

409430
expect(form.dirty).toBe(true)
@@ -429,6 +450,19 @@ describe('Form', () => {
429450
})
430451
form = wrapper.setupState.form.value
431452
state = wrapper.setupState.state
453+
454+
// Wait for all mounting and initialization to complete
455+
await nextTick()
456+
await flushPromises()
457+
458+
// Ensure form state is clean
459+
form.touchedFields.clear()
460+
form.dirtyFields.clear()
461+
form.blurredFields.clear()
462+
})
463+
464+
afterEach(() => {
465+
wrapper?.unmount()
432466
})
433467

434468
it('submit error works', async () => {
@@ -483,6 +517,19 @@ describe('Form', () => {
483517
vi.clearAllMocks()
484518
wrapper = await renderForm({ fixture: 'FormNested', props: { onSubmit, onError } })
485519
form = wrapper.setupState.form.value
520+
521+
// Wait for all mounting and initialization to complete
522+
await nextTick()
523+
await flushPromises()
524+
525+
// Ensure form state is clean
526+
form.touchedFields.clear()
527+
form.dirtyFields.clear()
528+
form.blurredFields.clear()
529+
})
530+
531+
afterEach(() => {
532+
wrapper?.unmount()
486533
})
487534

488535
it('getErrors returns nested errors', async () => {

0 commit comments

Comments
 (0)