Skip to content

Commit

Permalink
test: update jest config (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuweiGL authored Jun 23, 2021
1 parent 2d07630 commit f228a40
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
"build:docs": "dumi build",
"start": "dumi dev",
"test": "jest --coverage",
"test:sc": "jest packages/react-shared-components",
"test:sc:watch": "npm run test:sc -- --watch",
"test:reactive": "jest packages/reactive",
"test:core": "jest packages/core",
"test:core:watch": "npm run test:core --- --watch",
Expand All @@ -27,8 +25,6 @@
"test:shared": "jest packages/shared",
"test:path": "jest packages/path",
"test:react:watch": "npm run test:react --- --watch",
"test:react-schema": "jest packages/react-schema-field",
"test:react-schema:watch": "npm run test:react-schema --- --watch",
"test:vue": "jest packages/vue",
"test:vue:watch": "npm run test:vue --- --watch",
"test:reactive-vue": "jest packages/reactive-vue",
Expand Down
4 changes: 3 additions & 1 deletion packages/json-schema/src/__tests__/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ describe('transform component', () => {
expect(schema.properties.c.toFieldProps(options).component[0]).toEqual(
Number
)
expect(schema.properties.e.toFieldProps(options).component).toBeUndefined()
expect(() => {
schema.properties.e.toFieldProps(options)
}).toThrowError()
})
})
51 changes: 28 additions & 23 deletions packages/react/src/__tests__/field.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import { act } from 'react-dom/test-utils'
import { render, fireEvent, waitFor } from '@testing-library/react'
import { createForm } from '@formily/core'
import { isField, isVoidField, onFieldChange } from '@formily/core'
import {
FormProvider,
ArrayField,
Expand All @@ -13,10 +15,10 @@ import {
connect,
mapProps,
mapReadPretty,
} from '../'
} from '..'
import { ReactiveField } from '../components/ReactiveField'
import { expectThrowError } from './shared'
import { isField, isVoidField, onFieldChange } from '@formily/core'

type InputProps = {
value?: string
onChange?: (...args: any) => void
Expand Down Expand Up @@ -142,7 +144,7 @@ test('useAttch', () => {

test('useFormEffects', async () => {
const form = createForm()
const CustomField = observer((props: { tag?: string }) => {
const CustomField = observer(() => {
const field = useField<Formily.Core.Models.Field>()
useFormEffects(() => {
onFieldChange('aa', ['value'], (target) => {
Expand All @@ -152,27 +154,30 @@ test('useFormEffects', async () => {
})
return <div data-testid="custom-value">{field.value}</div>
})
const { queryByTestId, rerender } = render(
<FormProvider form={form}>
<Field name="aa" decorator={[Decorator]} component={[Input]} />
<Field name="bb" component={[CustomField, { tag: 'xxx' }]} />
</FormProvider>
)
expect(queryByTestId('custom-value').textContent).toEqual('')
form.query('aa').take((aa) => {
if (isField(aa)) {
aa.setValue('123')
}
})
await waitFor(() => {
expect(queryByTestId('custom-value').textContent).toEqual('123')
act(async () => {
const { queryByTestId, rerender } = render(
<FormProvider form={form}>
<Field name="aa" decorator={[Decorator]} component={[Input]} />
<Field name="bb" component={[CustomField, { tag: 'xxx' }]} />
</FormProvider>
)

expect(queryByTestId('custom-value').textContent).toEqual('')
form.query('aa').take((aa) => {
if (isField(aa)) {
aa.setValue('123')
}
})
await waitFor(() => {
expect(queryByTestId('custom-value').textContent).toEqual('123')
})
rerender(
<FormProvider form={form}>
<Field name="aa" decorator={[Decorator]} component={[Input]} />
<Field name="bb" component={[CustomField, { tag: 'yyy' }]} />
</FormProvider>
)
})
rerender(
<FormProvider form={form}>
<Field name="aa" decorator={[Decorator]} component={[Input]} />
<Field name="bb" component={[CustomField, { tag: 'yyy' }]} />
</FormProvider>
)
})

test('connect', async () => {
Expand Down
21 changes: 18 additions & 3 deletions scripts/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ import prettyFormat from 'pretty-format'

global['prettyFormat'] = prettyFormat

global['sleep'] = time => {
return new Promise(resolve => setTimeout(resolve, time))
global['sleep'] = (time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}

global['requestAnimationFrame'] = fn => setTimeout(fn)
global['requestAnimationFrame'] = (fn) => setTimeout(fn)

global.document.documentElement.style['grid-column-gap'] = true

// 把 console.error 转换成 error,方便断言
;(() => {
const spy = jest.spyOn(console, 'error')
beforeAll(() => {
spy.mockImplementation((message) => {
console.log(message)
throw new Error(message)
})
})

afterAll(() => {
spy.mockRestore()
})
})()

0 comments on commit f228a40

Please sign in to comment.