Skip to content

Commit

Permalink
test(core): improve core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jan 20, 2021
1 parent 02d5572 commit c643d8b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
22 changes: 22 additions & 0 deletions packages/core/src/__tests__/form.spec.ts
Expand Up @@ -336,6 +336,28 @@ test('setState/getState/setFormState/getFormState/setFieldState/getFieldState',
state.display = 'none'
})
expect(form.getFieldState('aa', (state) => state.visible)).toBeFalsy()
const update = (value: any) => (state: any) => {
state.value = value
}
const update2 = (state: any) => {
state.value = 123
}
form.setFieldState('kk', update(123))
form.setFieldState('kk', update(321))
form.setFieldState('oo', update2)
form.setFieldState('oo', update2)
const oo = attach(
form.createField({
name: 'oo',
})
)
const kk = attach(
form.createField({
name: 'kk',
})
)
expect(oo.value).toEqual(123)
expect(kk.value).toEqual(321)
})

test('validate/valid/invalid/errors/warnings/successes/clearErrors/clearWarnings/clearSuccesses/queryFeedbacks', async () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/__tests__/internals.spec.ts
Expand Up @@ -2,6 +2,7 @@ import {
getValuesFromEvent,
matchFeedback,
applyFieldPatches,
setModelState,
} from '../shared/internals'

test('getValuesFromEvent', () => {
Expand All @@ -25,3 +26,15 @@ test('applyFieldPatches', () => {
])
expect(fields).toEqual({})
})

test('setModelState', () => {
expect(setModelState(null, null)).toBeUndefined()
expect(
setModelState(
{},
{
parent: null,
}
)
).toEqual({})
})
7 changes: 4 additions & 3 deletions packages/core/src/shared/internals.ts
Expand Up @@ -284,8 +284,7 @@ export const exchangeArrayState = (
const moveIndex = (identifier: string) => {
const preStr = identifier.slice(0, address.length)
const afterStr = identifier.slice(address.length)
const number = afterStr.match(/^\.(\d+)/)?.[1]
if (number === undefined) return identifier
const number = afterStr.match(/^\.(\d+)/)[1]
const current = Number(number)
let index = current
if (index === fromIndex) {
Expand Down Expand Up @@ -376,12 +375,13 @@ export const subscribeUpdate = (
}

export const setModelState = (model: any, setter: any) => {
if (!model) return
const isSkipProperty = (key: string) => {
if (key === 'address' || key === 'path') return true
if (key === 'valid' || key === 'invalid') return true
if (key === 'validateStatus') return true
if (key === 'errors' || key === 'warnings' || key === 'successes') {
if (model?.displayName === 'Form') return true
if (model.displayName === 'Form') return true
if (setter.feedbacks?.length) {
return true
}
Expand Down Expand Up @@ -422,6 +422,7 @@ export const setModelState = (model: any, setter: any) => {
model[key] = value
})
}
return model
}

export const getModelState = (model: any, getter?: any) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/shared/observer.ts
Expand Up @@ -21,6 +21,7 @@ type Entry = {
}

function buildPath(entry: Entry | undefined): string {
/* istanbul ignore next */
if (!entry) return 'ROOT'
const res: string[] = []
while (entry.parent) {
Expand Down Expand Up @@ -100,6 +101,7 @@ export function observer<T = any>(
) {
if (isRecursivelyObservable(change.object[i])) {
const entry = entrySet.get(change.object[i])
/* istanbul ignore next */
if (entry) entry.path = '' + i
}
}
Expand All @@ -113,7 +115,9 @@ export function observer<T = any>(
path: string
) {
if (isRecursivelyObservable(thing)) {
/* istanbul ignore next */
const entry = entrySet.get(thing)
/* istanbul ignore next */
if (entry) {
/* istanbul ignore next */
if (entry.parent !== parent || entry.path !== path)
Expand Down Expand Up @@ -143,6 +147,7 @@ export function observer<T = any>(
}

function unobserveRecursively(thing: any) {
/* istanbul ignore next */
if (isRecursivelyObservable(thing)) {
const entry = entrySet.get(thing)
if (!entry) return
Expand Down

0 comments on commit c643d8b

Please sign in to comment.