mirror of
https://github.com/langgenius/dify.git
synced 2026-04-25 01:26:57 +08:00
Migrate 172 files from es-toolkit/compat to native es-toolkit. Migrated: - noop → es-toolkit/function - cloneDeep, omit, clone, pick → es-toolkit/object - uniq, intersection → es-toolkit/array - capitalize, camelCase, kebabCase, escape → es-toolkit/string - isEqual → es-toolkit/predicate Kept in compat (API differences): - uniqueId, isEmpty - get, merge, flow, curry, debounce - different APIs - uniqBy, groupBy, intersectionBy - property string support - flatten - undefined handling Benefits: 2-3x faster, smaller bundle Part of #30243 Fixes #30244
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { render, screen } from '@testing-library/react'
|
|
import { noop } from 'es-toolkit/function'
|
|
import { z } from 'zod'
|
|
import withValidation from '.'
|
|
|
|
describe('withValidation HOC', () => {
|
|
// schema for validation
|
|
const schema = z.object({ name: z.string() })
|
|
type Props = z.infer<typeof schema> & {
|
|
age: number
|
|
}
|
|
|
|
const TestComponent = ({ name, age }: Props) => (
|
|
<div>
|
|
{name}
|
|
{' '}
|
|
-
|
|
{' '}
|
|
{age}
|
|
</div>
|
|
)
|
|
const WrappedComponent = withValidation(TestComponent, schema)
|
|
|
|
beforeAll(() => {
|
|
vi.spyOn(console, 'error').mockImplementation(noop)
|
|
})
|
|
|
|
afterAll(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('renders the component when validation passes', () => {
|
|
render(<WrappedComponent name="Valid Name" age={30} />)
|
|
expect(screen.getByText('Valid Name - 30')).toBeInTheDocument()
|
|
})
|
|
|
|
it('renders the component when props is invalid but not in schema ', () => {
|
|
render(<WrappedComponent name="Valid Name" age={'aaa' as any} />)
|
|
expect(screen.getByText('Valid Name - aaa')).toBeInTheDocument()
|
|
})
|
|
|
|
it('does not render the component when validation fails', () => {
|
|
render(<WrappedComponent name={123 as any} age={30} />)
|
|
expect(screen.queryByText('123 - 30')).toBeNull()
|
|
})
|
|
})
|