Skip to content

Commit

Permalink
test(core): add designable tests (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Aug 9, 2021
1 parent 4118b8d commit 6a437c8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 39 deletions.
34 changes: 34 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Expand Up @@ -1167,3 +1167,37 @@ test('exception validate', async () => {
expect(form.invalid).toBeTruthy()
expect(form.validating).toBeFalsy()
})

test('designable form', () => {
const form = attach(
createForm({
designable: true,
})
)
attach(
form.createField({
name: 'bb',
initialValue: 123,
})
)
attach(
form.createField({
name: 'bb',
initialValue: 321,
})
)
attach(
form.createField({
name: 'aa',
value: 123,
})
)
attach(
form.createField({
name: 'aa',
value: 321,
})
)
expect(form.values.aa).toEqual(321)
expect(form.initialValues.bb).toEqual(321)
})
6 changes: 3 additions & 3 deletions packages/core/src/models/Field.ts
Expand Up @@ -109,7 +109,7 @@ export class Field<
this.makeIndexes(address)
this.makeObservable(designable)
this.makeReactive(designable)
this.onInit()
this.onInit(designable)
}

protected makeIndexes(address: FormPathPattern) {
Expand Down Expand Up @@ -668,10 +668,10 @@ export class Field<

getState: IModelGetter<IFieldState> = modelStateGetter(this)

onInit = () => {
onInit = (designable: boolean) => {
this.initialized = true
batch.scope(() => {
initFieldValue(this)
initFieldValue(this, designable)
})
batch.scope(() => {
initFieldUpdate(this)
Expand Down
81 changes: 45 additions & 36 deletions packages/core/src/shared/internals.ts
Expand Up @@ -411,47 +411,56 @@ export const cleanupObjectChildren = (field: ObjectField, keys: string[]) => {
})
}

export const initFieldValue = (field: Field) => {
export const initFieldValue = (field: Field, designable: boolean) => {
GlobalState.initializing = true
const fromValue = (value: any) => [
isValid(value),
isEmpty(value, true),
value,
]
const [, isEmptySelfValue, selfValue] = fromValue(field.value)
const [, isEmptySelfInitialValue, selfInitialValue] = fromValue(
field.initialValue
)
const [isValidPropsValue, isEmptyPropsValue, propsValue] = fromValue(
field.props.value
)
const [
isValidPropsInitialValue,
isEmptyPropsInitialValue,
propsInitialValue,
] = fromValue(field.props.initialValue)
if (isEmptySelfInitialValue) {
if (isEmptyPropsInitialValue) {
if (!isEqual(selfInitialValue, propsInitialValue)) {
field.initialValue = shallowClone(propsInitialValue)
}
} else if (isValidPropsInitialValue) {
field.initialValue = shallowClone(propsInitialValue)
if (designable) {
if (isValid(field.props.initialValue)) {
field.initialValue = shallowClone(field.props.initialValue)
}
}
if (isEmptySelfValue) {
if (!isEmptyPropsValue) {
field.value = shallowClone(propsValue)
} else {
if (!isEmptyPropsInitialValue) {
field.value = shallowClone(propsInitialValue)
} else if (isValidPropsValue) {
if (!isEqual(selfValue, propsValue)) {
field.value = shallowClone(propsValue)
if (isValid(field.props.value)) {
field.value = field.props.value
}
} else {
const fromValue = (value: any) => [
isValid(value),
isEmpty(value, true),
value,
]
const [, isEmptySelfValue, selfValue] = fromValue(field.value)
const [, isEmptySelfInitialValue, selfInitialValue] = fromValue(
field.initialValue
)
const [isValidPropsValue, isEmptyPropsValue, propsValue] = fromValue(
field.props.value
)
const [
isValidPropsInitialValue,
isEmptyPropsInitialValue,
propsInitialValue,
] = fromValue(field.props.initialValue)
if (isEmptySelfInitialValue) {
if (isEmptyPropsInitialValue) {
if (!isEqual(selfInitialValue, propsInitialValue)) {
field.initialValue = shallowClone(propsInitialValue)
}
} else if (isValidPropsInitialValue) {
if (!isEqual(selfValue, propsInitialValue)) {
field.initialValue = shallowClone(propsInitialValue)
}
}
if (isEmptySelfValue) {
if (!isEmptyPropsValue) {
field.value = shallowClone(propsValue)
} else {
if (!isEmptyPropsInitialValue) {
field.value = shallowClone(propsInitialValue)
} else if (isValidPropsValue) {
if (!isEqual(selfValue, propsValue)) {
field.value = shallowClone(propsValue)
}
} else if (isValidPropsInitialValue) {
if (!isEqual(selfValue, propsInitialValue)) {
field.value = shallowClone(propsInitialValue)
}
}
}
}
Expand Down

0 comments on commit 6a437c8

Please sign in to comment.