Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/packages/cascader/__tests__/cascader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, fireEvent, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import { CascaderOption } from '@nutui/nutui-react'
import { Cascader } from '../cascader'
import { normalizeOptions } from '../utils'

const mockOptions = [
{
Expand Down Expand Up @@ -86,6 +87,26 @@ const mockConvertOptions = [
]

describe('Cascader', () => {
it('normalizes mapped fields after preserving unrelated option fields', () => {
const options = [{ text: '北京', value: '021' }]

expect(
normalizeOptions(options, {
textKey: 'text',
valueKey: 'text',
childrenKey: 'children',
})
).toEqual([{ text: '北京', value: '北京', children: undefined }])

expect(
normalizeOptions(options, {
textKey: 'value',
valueKey: 'value',
childrenKey: 'children',
})
).toEqual([{ text: '021', value: '021', children: undefined }])
})

it('options', async () => {
const { container } = render(
<Cascader value={['福建', '福州', '鼓楼区']} options={mockOptions} />
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cascader/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const normalizeOptions = (
...others
} = opt
return {
...others,
text,
value,
children: normalizeOptions(children, keyMap),
...others,
} as CascaderOption
})
}
Expand Down
Loading