Skip to content

Commit

Permalink
test(@uform/react-schema-renderer): fix test case uncompat bugs (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Nov 6, 2019
1 parent 579a91e commit ff454e9
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 175 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export function createForm<FieldProps, VirtualFieldProps, FormProps>(
state.props = props
state.required = required
state.rules = rules as any
state.editable = editable
state.selfEditable = editable
state.formEditable = options.editable
})
batchRunTaskQueue(field, nodePath)
Expand Down Expand Up @@ -700,14 +700,17 @@ export function createForm<FieldProps, VirtualFieldProps, FormProps>(
state.submitting = true
})
env.submittingTask = validate()
.then(validated => {
const { errors } = validated
if (errors.length) {
.then(() => {
const validated = state.getState(({ errors, warnings }) => ({
errors,
warnings
})) //因为要合并effectErrors/effectWarnings,所以不能直接读validate的结果
if (validated.errors.length) {
state.setState(state => {
state.submitting = false
})
heart.publish(LifeCycleTypes.ON_FORM_SUBMIT_END, state)
return Promise.reject(errors)
return Promise.reject(validated.errors)
}
if (isFn(onSubmit)) {
return Promise.resolve(
Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/state/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ export const FieldState = createStateModel<IFieldState, IFieldStateProps>(
readRules({ rules, required }: IFieldStateProps) {
let newRules = isValid(rules) ? clone(toArr(rules)) : this.state.rules
if (isValid(required)) {
if (required) {
if (!newRules.some(rule => rule && rule.required)) {
newRules.push({ required: true })
}
} else {
newRules = newRules.filter(rule => rule && !rule.required)
if (
required &&
!newRules.some(rule => rule && rule.required !== undefined)
) {
newRules.push({ required })
}
} else {
required = newRules.some(rule => rule && rule.required)
required = newRules.some(rule => rule && rule.required === true)
}
return {
rules: newRules,
Expand Down
43 changes: 21 additions & 22 deletions packages/react-schema-renderer/src/__tests__/dynamic.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
import { toArr } from '@uform/shared'
import { render, fireEvent, wait, act } from '@testing-library/react'

const sleep = (time) => {
return wait(() => {}, { timeout: time })
const sleep = timeout => {
return new Promise(resolve => {
setTimeout(resolve, timeout)
})
}

let FormCard
Expand Down Expand Up @@ -442,9 +444,9 @@ test('invalid schema', async () => {
)
}
const { queryByText, queryAllByTestId } = render(<TestComponent />)
await wait();
await sleep(33)
fireEvent.click(queryByText('Add Field'))
await wait();
await wait()
expect(queryAllByTestId('item').length).toBe(2)
expect(queryAllByTestId('input').length).toBe(4)
})
Expand Down Expand Up @@ -474,16 +476,14 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn
onReset={() => {
if (constState.testA !== '123') {
act(() => setState({ testC: '456' }))
console.log('执行重置')
}
}}
onSubmit={() => {
if (constState.testA !== '123') {
act(() => setState({ testD: '456' }))
}
}}
onValidateFailed={(p) => {
console.log(p)
onValidateFailed={p => {
if (constState.testA !== '123') {
act(() => setState({ testE: '456' }))
}
Expand Down Expand Up @@ -518,12 +518,11 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn
)
}
const { queryByTestId, queryByText } = render(<TestComponent />)

await sleep(33)
await sleep(100)
fireEvent.click(queryByTestId('radio-a2'))
await wait()
expect(queryByText('valueB-456')).toBeVisible()
actions.reset({validate:false})
actions.reset({ validate: false })
await wait()
expect(queryByText('valueC-456')).toBeVisible()
fireEvent.click(queryByText('Submit'))
Expand Down Expand Up @@ -573,22 +572,22 @@ test('dynamic remove field and relationship needs to be retained', async () => {
const { queryAllByTestId, queryByText, queryAllByText } = render(
<TestComponent />
)
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(2)
let removes
await wait();
await wait()
removes = queryAllByText('Remove Field')
fireEvent.click(removes[removes.length - 1])
await wait();
await wait()
removes = queryAllByText('Remove Field')
fireEvent.click(removes[removes.length - 1])
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(0)
await wait();
await wait()
fireEvent.click(queryByText('Add Field'))
await wait();
await wait()
fireEvent.click(queryByText('Add Field'))
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(4)
expect(queryAllByTestId('input')[0].getAttribute('value')).toBe('')
expect(queryAllByTestId('input')[1].getAttribute('value')).toBe('')
Expand Down Expand Up @@ -628,20 +627,20 @@ test('after deleting a component should not be sync an default value', async ()
const { queryAllByTestId, queryByText, queryAllByText } = render(
<TestComponent />
)
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(4)
let removes
await wait();
await wait()
removes = queryAllByText('Remove Field')
fireEvent.click(removes[removes.length - 1])
await wait();
await wait()
removes = queryAllByText('Remove Field')
fireEvent.click(removes[removes.length - 1])
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(0)
fireEvent.click(queryByText('Add Field'))
fireEvent.click(queryByText('Add Field'))
await wait();
await wait()
expect(queryAllByTestId('input').length).toBe(2)
expect(queryAllByTestId('input')[0].getAttribute('value')).toBe('')
expect(queryAllByTestId('input')[1].getAttribute('value')).toBe('')
Expand Down
45 changes: 21 additions & 24 deletions packages/react-schema-renderer/src/__tests__/editable.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SchemaMarkupForm as SchemaForm,
SchemaMarkupField as Field,
createFormActions,
// createVirtualBox,
registerFieldMiddleware
} from '../index'
import { toArr } from '@uform/shared'
Expand Down Expand Up @@ -89,10 +88,9 @@ test('update editable by setFieldState', async () => {
</SchemaForm>
)

const { queryByText,baseElement } = render(<TestComponent />)
const { queryByText } = render(<TestComponent />)
await wait()
expect(queryByText('text')).toBeNull()
console.log(baseElement.innerHTML)
actions.setFieldState('aaa', state => {
state.editable = true
})
Expand Down Expand Up @@ -124,12 +122,12 @@ test('update editable by setFieldState with initalState is not editable', async
)

const { queryByText } = render(<TestComponent />)
await wait();
await wait()
expect(queryByText('text')).toBeNull()
actions.setFieldState('aaa', state => {
state.editable = true
})
await wait();
await wait()
expect(queryByText('text')).toBeVisible()
})

Expand All @@ -146,13 +144,13 @@ test('update editable in controlled', async () => {
}

const { queryByText } = render(<TestComponent />)
await wait();
await wait()
expect(queryByText('text')).toBeVisible()
act(() => updateEditable(false))
await wait();
await wait()
expect(queryByText('text')).toBeNull()
act(() => updateEditable(true))
await wait();
await wait()
expect(queryByText('text')).toBeVisible()
})

Expand All @@ -172,12 +170,12 @@ test('editable with x-props', async () => {
}

const { queryByText } = render(<TestComponent />)
await wait();
await wait()
expect(queryByText('text')).toBeNull()
actions.setFieldState('aaa', state => {
state.editable = true
})
await wait();
await wait()
expect(queryByText('text')).toBeVisible()
})

Expand All @@ -204,12 +202,12 @@ test('editable with x-props in array field', async () => {
}

const { queryByText } = render(<TestComponent />)
await wait();
await wait()
expect(queryByText('empty')).toBeVisible()
actions.setFieldState('array.0.aa', state => {
state.editable = true
})
await wait();
await wait()
expect(queryByText('empty')).toBeNull()
})

Expand Down Expand Up @@ -237,12 +235,12 @@ test('editable with x-props is affected by global editable', async () => {
}

const { queryByText } = render(<TestComponent />)
await wait();
await wait()
expect(queryByText('empty')).toBeNull()
actions.setFieldState('array.0.aa', state => {
state.editable = false
})
await wait();
await wait()
expect(queryByText('empty')).toBeVisible()
})

Expand Down Expand Up @@ -281,16 +279,16 @@ test('editable conflicts that global editable props with setFieldState', async (
}

const { queryByTestId } = render(<TestComponent />)
await wait();
await wait()
expect(queryByTestId('this is aaa')).toBeVisible()
expect(queryByTestId('this is bbb')).toBeVisible()
expect(queryByTestId('this is ccc')).toBeVisible()
fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()

fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()
})

Expand Down Expand Up @@ -329,19 +327,18 @@ test('editable conflicts that props editable props with setFieldState', async ()
}

const { queryByTestId } = render(<TestComponent />)
await wait();
await wait()
expect(queryByTestId('this is aaa')).toBeVisible()
expect(queryByTestId('this is bbb')).toBeVisible()
expect(queryByTestId('this is ccc')).toBeVisible()
fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()
fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()
})


test('editable conflicts that x-props editable props with setFieldState', async () => {
const TestComponent = () => {
return (
Expand Down Expand Up @@ -376,14 +373,14 @@ test('editable conflicts that x-props editable props with setFieldState', async
}

const { queryByTestId } = render(<TestComponent />)
await wait();
await wait()
expect(queryByTestId('this is aaa')).toBeVisible()
expect(queryByTestId('this is bbb')).toBeVisible()
expect(queryByTestId('this is ccc')).toBeVisible()
fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()
fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } })
await wait();
await wait()
expect(queryByTestId('this is bbb')).toBeVisible()
})
Loading

0 comments on commit ff454e9

Please sign in to comment.