Skip to content

Commit

Permalink
test: improve test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrior committed Jan 22, 2024
1 parent 6b3cbbf commit 000756a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
28 changes: 28 additions & 0 deletions test/core-react/@helpers/CompletedCore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ICore, ICoreRef, IPartPartial } from '@core-react/index'
import Core from '@core-react/index'
import type { Ref } from 'react'
import { forwardRef, useMemo } from 'react'

import renderers from './renderers'
import Vertical from './Vertical'

type IProps = IPartPartial<ICore, 'itemLayout' | 'renderers'>

const CompletedCore = (props: IProps, ref?: Ref<ICoreRef>) => {
// 渲染器集合处理
const innerRenderers = useMemo(
() => ({ ...renderers, ...props.renderers }),
[props.renderers]
)

return (
<Core
{...props}
ref={ref}
itemLayout={props.itemLayout || Vertical}
renderers={innerRenderers}
/>
)
}

export default forwardRef(CompletedCore)
22 changes: 22 additions & 0 deletions test/core-react/@helpers/renderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ const renderers: IRenderers<any, ISchema> = {
)
},
},
// 输出警告的 InputText 渲染器
WarningInputText: {
component: ({ schema, value, disabled, readonly, onChange }) => {
if (readonly) {
return <div className="input-text is-readonly">{value}</div>
}
return (
<input
className={classNames('input-text', { 'is-disabled': disabled })}
{...schema.renderOptions}
value={value ?? ''}
onChange={(e) => onChange(e.target.value)}
/>
)
},
validator: () => {
return {
status: 'warning',
message: 'warning-message-from-WarningInputText',
}
},
},
}

export default renderers
24 changes: 7 additions & 17 deletions test/core-react/rootClassNames.test.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,43 @@
import Core from '@core-react/index'
import { wrapRootSchema } from '@test/@helpers/schema'
import { render } from '@testing-library/react'

import CompletedCore from './@helpers/CompletedCore'

describe('根节点类名验证', () => {
const FakeLayout = () => <>Fake</>
const schema = wrapRootSchema()

test('设置类名前缀为 my-sr, 根节点应该包含该类名', async () => {
const { container } = render(
<Core prefixCls="my-sr" schema={schema} itemLayout={FakeLayout} renderers={{}} />
)
const { container } = render(<CompletedCore prefixCls="my-sr" schema={schema} />)
const rootElem = container.querySelector('.my-sr')
expect(rootElem).toBeInTheDocument()
})

test('设置类名为 my-class, 根节点应该包含该类名', async () => {
const { container } = render(
<Core className="my-class" schema={schema} itemLayout={FakeLayout} renderers={{}} />
)
const { container } = render(<CompletedCore className="my-class" schema={schema} />)
const rootElem = container.querySelector('.my-class')
expect(rootElem).toBeInTheDocument()
})

test('禁用时, 根节点应该包含该 is-disabled 类名', async () => {
const { container } = render(
<Core disabled schema={schema} itemLayout={FakeLayout} renderers={{}} />
)
const { container } = render(<CompletedCore disabled schema={schema} />)
const rootElem = container.querySelector('.is-disabled')
expect(rootElem).toBeInTheDocument()
})

test('只读时, 根节点应该包含该 is-readonly 类名', async () => {
const { container } = render(
<Core readonly schema={schema} itemLayout={FakeLayout} renderers={{}} />
)
const { container } = render(<CompletedCore readonly schema={schema} />)
const rootElem = container.querySelector('.is-readonly')
expect(rootElem).toBeInTheDocument()
})

test('各类状态下,类名应该都包含', async () => {
const { container } = render(
<Core
<CompletedCore
disabled
readonly
prefixCls="my-sr"
className="my-class"
schema={schema}
itemLayout={FakeLayout}
renderers={{}}
/>
)

Expand Down

0 comments on commit 000756a

Please sign in to comment.