Skip to content

Commit

Permalink
test(project): add large test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Mar 6, 2019
1 parent e68a4fe commit 68fd2e1
Show file tree
Hide file tree
Showing 15 changed files with 464 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
},
"globals": {
"sleep": true
"sleep": true,
"prettyFormat": true
},
"rules": {
// don't force es6 functions to include space before paren
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"json5": "^2.1.0",
"lerna": "^3.10.1",
"prettier-eslint-cli": "^4.7.1",
"pretty-format": "^24.0.0",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-testing-library": "^6.0.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export class Field {
const rules = this.getRulesFromProps(options.props)
this.rules = !isEmpty(rules) ? rules : this.rules
this.required = hasRequired(this.rules)

this.props = isEmpty(options.props)
? clone(this.props, filterSchema)
: clone({ ...this.props, ...options.props }, filterSchema)

this.props = !isEmpty(options.props)
? !isEmpty(this.props)
? { ...this.props, ...clone(options.props) }
: clone(options.props)
: this.props
if (isFn(options.onChange)) {
this.onChange(options.onChange)
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Form {
errors: [],
pristine: true,
initialValues: clone(values) || {},
values: values || {},
values: clone(values) || {},
dirty:
lastDirty || (this.initialized ? !isEqual(values, lastValues) : false)
}
Expand Down Expand Up @@ -337,7 +337,6 @@ export class Form {
value: value !== undefined ? value : initialValue,
path: options.path,
initialValue: initialValue,
rules: options.rules,
props: options.props
})
let field = this.fields[name]
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const createForm = ({

fields = fields.map(({ name, path, schemaPath, schema, value }) => {
return form.registerField(name || schemaPath.join('.'), {
rules: schema['x-rules'],
path: schemaPath,
props: schema
})
Expand Down
65 changes: 65 additions & 0 deletions packages/react/src/__tests__/destruct.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { Fragment } from 'react'
import SchemaForm, { Field, registerFormField, connect } from '../index'
import { toArr } from '@uform/utils'
import { render } from 'react-testing-library'

beforeEach(() => {
registerFormField('string', connect()(props => <div>{props.value}</div>))

registerFormField('array', props => {
const { value, mutators, renderField } = props
return (
<Fragment>
{toArr(value).map((item, index) => {
return (
<div data-testid='item' key={index}>
{renderField(index)}
</div>
)
})}
<button
type='button'
onClick={() => {
mutators.push()
}}
>
Add Field
</button>
</Fragment>
)
})
})

test('destruct with initial values', async () => {
const TestComponent = () => {
return (
<SchemaForm initialValues={{ aa: 123, bb: 321 }}>
<Field type='string' name='[aa,bb]' />
</SchemaForm>
)
}

const { queryByText } = render(<TestComponent />)

await sleep(33)
expect(queryByText('123321')).toBeVisible()
})

test('destruct with initial values in array', async () => {
const TestComponent = () => {
return (
<SchemaForm initialValues={{ array: [{ aa: 123, bb: 321 }] }}>
<Field type='array' name='array'>
<Field type='object'>
<Field type='string' name='[aa,bb]' />
</Field>
</Field>
</SchemaForm>
)
}

const { queryByText } = render(<TestComponent />)

await sleep(33)
expect(queryByText('123321')).toBeVisible()
})
Loading

0 comments on commit 68fd2e1

Please sign in to comment.