Skip to content

Commit

Permalink
test: remove first level sleep in tests and use fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
Balastrong committed Jul 2, 2024
1 parent 0b38c3e commit f561cc2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
12 changes: 7 additions & 5 deletions packages/valibot-form-adapter/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { FieldApi, FormApi } from '@tanstack/form-core'
import * as v from 'valibot'
import { valibotValidator } from '../src/index'
Expand Down Expand Up @@ -61,6 +61,7 @@ describe('valibot field api', () => {
})

it('should run an onChangeAsync with string validation', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand All @@ -87,14 +88,15 @@ describe('valibot field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Testing 123'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

it('should run an onChangeAsyc fn with valibot validation option enabled', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand All @@ -118,10 +120,10 @@ describe('valibot field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Test'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

Expand Down
12 changes: 7 additions & 5 deletions packages/yup-form-adapter/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { FieldApi, FormApi } from '@tanstack/form-core'
import yup from 'yup'
import { yupValidator } from '../src/index'
Expand Down Expand Up @@ -58,6 +58,7 @@ describe('yup field api', () => {
})

it('should run an onChangeAsync with z.string validation', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand All @@ -83,14 +84,15 @@ describe('yup field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Testing 123'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

it('should run an onChangeAsyc fn with zod validation option enabled', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand All @@ -114,10 +116,10 @@ describe('yup field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Test'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

Expand Down
12 changes: 7 additions & 5 deletions packages/zod-form-adapter/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'

import { FieldApi, FormApi } from '@tanstack/form-core'
import { z } from 'zod'
Expand Down Expand Up @@ -59,6 +59,7 @@ describe('zod field api', () => {
})

it('should run an onChangeAsync with z.string validation', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand Down Expand Up @@ -87,14 +88,15 @@ describe('zod field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Testing 123'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

it('should run an onChangeAsyc fn with zod validation option enabled', async () => {
vi.useFakeTimers()
const form = new FormApi({
defaultValues: {
name: '',
Expand All @@ -118,10 +120,10 @@ describe('zod field api', () => {

expect(field.getMeta().errors).toEqual([])
field.setValue('a', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual(['Test'])
field.setValue('asdf', { touch: true })
await sleep(10)
await vi.advanceTimersByTimeAsync(10)
expect(field.getMeta().errors).toEqual([])
})

Expand Down

0 comments on commit f561cc2

Please sign in to comment.