Skip to content

Commit

Permalink
refactor(core): mark sub-functions of PrepareFormState as internal
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Sep 17, 2024
1 parent e3b2bd8 commit 35c17a7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 88 deletions.
95 changes: 49 additions & 46 deletions packages/sanity/src/core/form/store/__tests__/formState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {
import {startsWith, toString} from '@sanity/util/paths'

import {createSchema} from '../../../schema/createSchema'
import {
createCallbackResolver,
type RootCallbackResolver,
} from '../conditional-property/createCallbackResolver'
import {
createPrepareFormState,
type PrepareFormState,
Expand All @@ -35,8 +31,8 @@ import {
import {type StateTree} from '../types/state'

let prepareFormState!: PrepareFormState
let prepareHiddenState!: RootCallbackResolver<'hidden'>
let prepareReadOnlyState!: RootCallbackResolver<'readOnly'>

type RemoveFirstChar<S extends string> = S extends `${infer _}${infer R}` ? R : S

beforeEach(() => {
prepareFormState = createPrepareFormState({
Expand All @@ -50,8 +46,6 @@ beforeEach(() => {
preparePrimitiveInputState: jest.fn,
},
})
prepareHiddenState = createCallbackResolver({property: 'hidden'})
prepareReadOnlyState = createCallbackResolver({property: 'readOnly'})
})
const schema = createSchema({
name: 'default',
Expand Down Expand Up @@ -334,7 +328,10 @@ const rootFormNodeOptions: Partial<{
},
}

const paths: {path: Path; expectedCalls: {[K in keyof PrepareFormState]: number}}[] = [
const paths: {
path: Path
expectedCalls: {[K in RemoveFirstChar<keyof PrepareFormState>]: number}
}[] = [
{
path: ['title'],
expectedCalls: {
Expand Down Expand Up @@ -467,32 +464,35 @@ describe.each(
expect(startsWith(differentNode.path, path)).toBe(true)
}

expect(prepareFormState.prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsInputState,
)
expect(prepareFormState.prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsMember,
)
expect(prepareFormState.prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesInputState,
)
expect(prepareFormState.prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesMember,
)
expect(prepareFormState.prepareFieldMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareFieldMember).toHaveBeenCalledTimes(
expectedCalls.prepareFieldMember,
)
expect(prepareFormState.prepareObjectInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareObjectInputState).toHaveBeenCalledTimes(
expectedCalls.prepareObjectInputState,
)
expect(prepareFormState.preparePrimitiveInputState).toHaveBeenCalledTimes(
expect(prepareFormState._preparePrimitiveInputState).toHaveBeenCalledTimes(
expectedCalls.preparePrimitiveInputState,
)
})
})

describe('hidden', () => {
const pathsToTest: {path: Path; expectedCalls: {[K in keyof PrepareFormState]: number}}[] = [
const pathsToTest: {
path: Path
expectedCalls: {[K in RemoveFirstChar<keyof PrepareFormState>]: number}
}[] = [
{
path: ['title'],
expectedCalls: {
Expand Down Expand Up @@ -565,32 +565,35 @@ describe('hidden', () => {
}

// Verify memoization: functions should be called only for affected nodes
expect(prepareFormState.prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsInputState,
)
expect(prepareFormState.prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsMember,
)
expect(prepareFormState.prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesInputState,
)
expect(prepareFormState.prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesMember,
)
expect(prepareFormState.prepareFieldMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareFieldMember).toHaveBeenCalledTimes(
expectedCalls.prepareFieldMember,
)
expect(prepareFormState.prepareObjectInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareObjectInputState).toHaveBeenCalledTimes(
expectedCalls.prepareObjectInputState,
)
expect(prepareFormState.preparePrimitiveInputState).toHaveBeenCalledTimes(
expect(prepareFormState._preparePrimitiveInputState).toHaveBeenCalledTimes(
expectedCalls.preparePrimitiveInputState,
)
})
})

describe('collapsedPaths', () => {
const pathsToTest: {path: Path; expectedCalls: {[K in keyof PrepareFormState]: number}}[] = [
const pathsToTest: {
path: Path
expectedCalls: {[K in RemoveFirstChar<keyof PrepareFormState>]: number}
}[] = [
{
path: ['simpleObject'],
expectedCalls: {
Expand Down Expand Up @@ -657,25 +660,25 @@ describe('collapsedPaths', () => {
expect(member && 'collapsed' in member && member.collapsed).toBe(true)

// Verify memoization: functions should be called only for affected nodes
expect(prepareFormState.prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsInputState,
)
expect(prepareFormState.prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfObjectsMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfObjectsMember,
)
expect(prepareFormState.prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesInputState,
)
expect(prepareFormState.prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(
expectedCalls.prepareArrayOfPrimitivesMember,
)
expect(prepareFormState.prepareFieldMember).toHaveBeenCalledTimes(
expect(prepareFormState._prepareFieldMember).toHaveBeenCalledTimes(
expectedCalls.prepareFieldMember,
)
expect(prepareFormState.prepareObjectInputState).toHaveBeenCalledTimes(
expect(prepareFormState._prepareObjectInputState).toHaveBeenCalledTimes(
expectedCalls.prepareObjectInputState,
)
expect(prepareFormState.preparePrimitiveInputState).toHaveBeenCalledTimes(
expect(prepareFormState._preparePrimitiveInputState).toHaveBeenCalledTimes(
expectedCalls.preparePrimitiveInputState,
)
})
Expand Down Expand Up @@ -711,13 +714,13 @@ describe('collapsedFieldSets', () => {
expect(fieldset?.collapsed).toBe(true)

// Verify memoization: functions should be called only for affected nodes
expect(prepareFormState.prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareArrayOfObjectsMember).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareFieldMember).toHaveBeenCalledTimes(8)
expect(prepareFormState.prepareObjectInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState.preparePrimitiveInputState).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareArrayOfObjectsMember).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareFieldMember).toHaveBeenCalledTimes(8)
expect(prepareFormState._prepareObjectInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState._preparePrimitiveInputState).toHaveBeenCalledTimes(0)
})
})

Expand Down Expand Up @@ -747,12 +750,12 @@ describe('fieldGroupState', () => {
])

// Verify memoization: functions should be called only for affected nodes
expect(prepareFormState.prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState.prepareArrayOfObjectsMember).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState.prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(0)
expect(prepareFormState.prepareFieldMember).toHaveBeenCalledTimes(8)
expect(prepareFormState.prepareObjectInputState).toHaveBeenCalledTimes(3)
expect(prepareFormState.preparePrimitiveInputState).toHaveBeenCalledTimes(4)
expect(prepareFormState._prepareArrayOfObjectsInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState._prepareArrayOfObjectsMember).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareArrayOfPrimitivesInputState).toHaveBeenCalledTimes(1)
expect(prepareFormState._prepareArrayOfPrimitivesMember).toHaveBeenCalledTimes(0)
expect(prepareFormState._prepareFieldMember).toHaveBeenCalledTimes(8)
expect(prepareFormState._prepareObjectInputState).toHaveBeenCalledTimes(3)
expect(prepareFormState._preparePrimitiveInputState).toHaveBeenCalledTimes(4)
})
})
92 changes: 50 additions & 42 deletions packages/sanity/src/core/form/store/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,21 @@ export interface RootFormStateOptions {

export interface PrepareFormState {
(options: RootFormStateOptions): ObjectFormNode | null
prepareFieldMember: PrepareFieldMember
prepareObjectInputState: PrepareObjectInputState
prepareArrayOfPrimitivesInputState: PrepareArrayOfPrimitivesInputState
prepareArrayOfObjectsInputState: PrepareArrayOfObjectsInputState
prepareArrayOfObjectsMember: PrepareArrayOfObjectsMember
prepareArrayOfPrimitivesMember: PrepareArrayOfPrimitivesMember
preparePrimitiveInputState: PreparePrimitiveInputState

/** @internal */
_prepareFieldMember: PrepareFieldMember
/** @internal */
_prepareObjectInputState: PrepareObjectInputState
/** @internal */
_prepareArrayOfPrimitivesInputState: PrepareArrayOfPrimitivesInputState
/** @internal */
_prepareArrayOfObjectsInputState: PrepareArrayOfObjectsInputState
/** @internal */
_prepareArrayOfObjectsMember: PrepareArrayOfObjectsMember
/** @internal */
_prepareArrayOfPrimitivesMember: PrepareArrayOfPrimitivesMember
/** @internal */
_preparePrimitiveInputState: PreparePrimitiveInputState
}

export function createPrepareFormState({
Expand Down Expand Up @@ -1247,50 +1255,50 @@ export function createPrepareFormState({
},
)

return Object.assign(
function prepareFormState({
function prepareFormState({
collapsedFieldSets,
collapsedPaths,
comparisonValue,
currentUser,
documentValue,
fieldGroupState,
focusPath,
hidden,
openPath,
presence,
readOnly,
schemaType,
validation,
changesOpen,
}: RootFormStateOptions): ObjectFormNode | null {
return prepareObjectInputState({
collapsedFieldSets,
collapsedPaths,
comparisonValue,
currentUser,
documentValue,
value: documentValue,
fieldGroupState,
focusPath,
hidden,
hidden: hidden === false ? EMPTY_OBJECT : hidden,
openPath,
presence,
readOnly,
readOnly: readOnly === false ? EMPTY_OBJECT : readOnly,
schemaType,
validation,
changesOpen,
}: RootFormStateOptions): ObjectFormNode | null {
return prepareObjectInputState({
collapsedFieldSets,
collapsedPaths,
comparisonValue,
currentUser,
value: documentValue,
fieldGroupState,
focusPath,
hidden: hidden === false ? EMPTY_OBJECT : hidden,
openPath,
presence,
readOnly: readOnly === false ? EMPTY_OBJECT : readOnly,
schemaType,
validation,
changesOpen,
level: 0,
path: [],
})
},
{
prepareFieldMember,
prepareObjectInputState,
prepareArrayOfPrimitivesInputState,
prepareArrayOfObjectsInputState,
prepareArrayOfObjectsMember,
prepareArrayOfPrimitivesMember,
preparePrimitiveInputState,
},
)
level: 0,
path: [],
})
}

prepareFormState._prepareFieldMember = prepareFieldMember
prepareFormState._prepareFieldMember = prepareFieldMember
prepareFormState._prepareObjectInputState = prepareObjectInputState
prepareFormState._prepareArrayOfPrimitivesInputState = prepareArrayOfPrimitivesInputState
prepareFormState._prepareArrayOfObjectsInputState = prepareArrayOfObjectsInputState
prepareFormState._prepareArrayOfObjectsMember = prepareArrayOfObjectsMember
prepareFormState._prepareArrayOfPrimitivesMember = prepareArrayOfPrimitivesMember
prepareFormState._preparePrimitiveInputState = preparePrimitiveInputState

return prepareFormState
}

0 comments on commit 35c17a7

Please sign in to comment.