Skip to content

Commit

Permalink
test(validation): reproduction of #1371
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-schroeder committed May 24, 2024
1 parent 45e7589 commit d294cd8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/validation/__tests__/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,48 @@ describe('getValidationMessages', () => {
expect(node.store).not.toHaveProperty('rule_required')
expect(textMiddleware).toHaveBeenCalledTimes(4)
})

it.only('can depend on other nodes without stopping', async () => {
const required_if: FormKitValidationRule = vi.fn(
(node: FormKitNode, addr: string) => {
const other = node.at(addr)
if (other!.value === 'foo' && node.value) {
return true
}
return false
}
)
required_if.skipEmpty = false
const length: FormKitValidationRule = vi.fn(
({ value }, length) => ('' + value).length >= parseInt(length)
)
const validation = createValidationPlugin({ required_if, length })
const form = createNode({
type: 'group',
children: [
createNode({ name: 'foo' }),
createNode({
name: 'bar',
value: '',
props: { validation: 'required_if:foo|length:10' },
}),
],
})
const bar = form.at('$self.bar')!
bar.use(validation)
const foo = form.at('$self.foo')!
expect(bar.store).toHaveProperty('rule_required_if')
expect(required_if).toHaveBeenCalledTimes(1)
foo.input('foo', false)
bar.input('123', false)
await new Promise((r) => setTimeout(r, 5))
expect(bar.store).not.toHaveProperty('rule_required_if')
expect(required_if).toHaveBeenCalledTimes(2)
foo.input('bar', false)
await new Promise((r) => setTimeout(r, 5))
expect(required_if).toHaveBeenCalledTimes(3)
expect(bar.store).toHaveProperty('rule_required_if')
})
})

describe('dynamic rules', () => {
Expand Down

0 comments on commit d294cd8

Please sign in to comment.