Files
react/components/table/__tests__/index.test.tsx
witt bb575498fe chore: release v1.8.0 (#367)
* feat(snippet): add custom symbol and toast

* docs(snippet): add docs for custom symbol and toast

* fix(snippet): default toast type as normal type

* test(snippet): add custom symbol and toast

* docs(snippet): add enum type of APIs

* docs(toast): add normal-types

* chore: release v1.8.0-canary.1

* feat(modal): optimize the animation of layer

* test: update snapshots

* chore: release v1.8.0-canary.2

* docs: format import statement

* feat(modal): optimize the max width, height and alignment of layer

* test: update snapshots

* chore: release v1.8.0-canary.3

* Button (#319)

* feat(button): center the icon if the button is empty

* test(button): add testcase for icon only

* docs(button): add example for icon only

* chore: release v1.8.0-canary.4

* fix(snippet): remove space when the symbol is empty (#325)

* fix(snippet): remove space when the symbol is empty

style(snippet): remove unnecessary escape symbols

* fix(snippet): ignore spaces in symbol

style: fix lint warning

* chore: release v1.8.0-canary.5

* feat(tabs): sync the label and set value to required (#334)

* feat(tabs): sync the label and set value to required

* test(tabs): add testcase for label sync

* docs(tabs): update value to required

* feat(modal): use Button to reconstrust Modal.Action (#332)

* feat(modal): use Button to reconstrust Modal.Action

* docs(modal): add example for action loading

* test: update snapshots

* chore: release v1.8.0-canary.6

* fix(input): always synchronize external value and ignore changes (#336)

* fix(input): always synchronize external value and ignore changes

* feat(input): support imperative API to update

* fix(textarea): imperative api same as input (#341)

* feat(dropdown): allow dropdown to set the specified container (#344)

* feat(dropdown): allow dropdown to set the specified container

* test(modal): update snapshots

* docs(select): add example for custom popup container

* fix(dropdown): fix type of getPopupContainer

* test(dropdown): add testcase for specified container rendering

* chore: release v1.8.0-canary.7

* fix(link): fix hard-coded background color (#347)

* style(description): fix title font-weight (#348)

* docs(link): fix duplicate examples (#346)

* style(popover-item): the whole item should be clickable when using with link (#345)

* fix(modal): fix type of Modal.Action (#351)

* chore: release v1.8.0-canary.8

* feat(modal): lock tab action inside modal (#354)

* feat(button): add style to focus buttons

* feat(collections): add util function

* feat(modal): lock tab action inside modal

* test(modal): add tests for modal focus

* test: update style of button

* fix(table): fix column's props are not tracked (#362)

* chore: release v1.8.0-canary.9

* fix(table): children of column should be kept in sync (#364)

* chore: release v1.8.0-canary.10

Co-authored-by: Augusto <augustoconti10@gmail.com>
Co-authored-by: yqrashawn <namy.19@gmail.com>
Co-authored-by: Zhao Lei <firede@firede.us>
2020-08-14 16:59:49 +08:00

207 lines
7.1 KiB
TypeScript

import React from 'react'
import { mount } from 'enzyme'
import { Table, Code } from 'components'
import { cellActions } from 'components/table/table-cell'
import { nativeEvent, updateWrapper } from 'tests/utils'
import { act } from 'react-dom/test-utils'
const data = [
{ property: 'type', description: 'Content type', default: '-' },
{ property: 'Component', description: 'DOM element to use', default: '-' },
{ property: 'bold', description: 'Bold style', default: 'true' },
]
describe('Table', () => {
it('should render correctly', () => {
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" />
</Table>,
)
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
})
it('should re-render when data changed', async () => {
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" />
</Table>,
)
expect(wrapper.find('tbody').find('tr').length).toBe(data.length)
wrapper.setProps({ data: [] })
await updateWrapper(wrapper, 350)
expect(wrapper.find('tbody').find('tr').length).toBe(0)
})
it('should set width automatically', () => {
window.getComputedStyle = jest.fn().mockImplementation(() => ({
width: '100px',
}))
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" width={50} />
</Table>,
)
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
;(window.getComputedStyle as jest.Mock).mockClear()
})
it('should be no erros when width is too large', () => {
window.getComputedStyle = jest.fn().mockImplementation(() => ({
width: '10px',
}))
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" width={50} />
</Table>,
)
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
;(window.getComputedStyle as jest.Mock).mockClear()
})
it('should work with other components', () => {
const dataWithNodes = [
...data,
{ property: 'bold', description: <Code>boolean</Code>, default: 'true' },
]
const wrapper = mount(
<Table data={dataWithNodes}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" />
</Table>,
)
expect(wrapper.html()).toMatchSnapshot()
expect(wrapper.find('code').length).not.toBe(0)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should work without hover effect', () => {
const wrapper = mount(
<Table data={data} hover={false}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="default" label="default" />
</Table>,
)
expect(wrapper.html()).toMatchSnapshot()
expect(() => wrapper.unmount()).not.toThrow()
})
it('should be possible to remove the row', () => {
const operation = (actions: cellActions) => {
return <button onClick={() => actions.remove()}>Remove</button>
}
const data = [{ property: 'bold', description: 'boolean', operation }]
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="operation" label="operation" />
</Table>,
)
expect(wrapper.find('tbody').find('tr').length).toBe(1)
wrapper.find('tbody').find('button').simulate('click')
expect(wrapper.find('tbody').find('tr').length).toBe(0)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should render emptyText when data missing', () => {
const data = [{ property: 'bold', description: 'boolean' }]
const wrapper = mount(
<Table data={data} emptyText="test-not-found">
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
<Table.Column prop="operation" label="operation" />
</Table>,
)
expect(wrapper.find('tbody').text()).toContain('test-not-found')
})
it('should trigger events when cell clicked', () => {
const rowHandler = jest.fn()
const cellHandler = jest.fn()
const data = [{ property: 'bold', description: 'boolean' }]
const wrapper = mount(
<Table data={data} emptyText="test-not-found" onRow={rowHandler} onCell={cellHandler}>
<Table.Column prop="property" label="property" />
<Table.Column prop="description" label="description" />
</Table>,
)
wrapper.find('tbody').find('tr').find('td').at(0).simulate('click', nativeEvent)
expect(rowHandler).toHaveBeenCalled()
expect(cellHandler).toHaveBeenCalled()
})
it('should wraning when prop missing', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
mount(
<Table data={data}>
<Table.Column prop="" label="property" />
<Table.Column prop="description" label="description" />
</Table>,
)
expect(errorSpy).toHaveBeenCalled()
errorSpy.mockRestore()
})
it('should render children for table head', () => {
const wrapper = mount(
<Table data={data}>
<Table.Column prop="property">
<Code>property</Code>
</Table.Column>
</Table>,
)
expect(wrapper.find('thead').find('code').length).not.toBe(0)
expect(wrapper.html()).toMatchSnapshot()
})
it('the changes of column should be tracked', () => {
const Mock = ({ label }: { label: string }) => {
return (
<Table data={data}>
<Table.Column prop="description" label={label} />
</Table>
)
}
const wrapper = mount(<Mock label="test1" />)
expect(wrapper.find('thead').find('tr').at(0).text()).toBe('test1')
act(() => {
wrapper.setProps({ label: 'test2' })
})
expect(wrapper.find('thead').find('tr').at(0).text()).toBe('test2')
expect(() => wrapper.unmount()).not.toThrow()
})
it('the changes of children should be tracked', () => {
const Mock = ({ label }: { label: string }) => {
return (
<Table data={data}>
<Table.Column prop="description">{label}</Table.Column>
</Table>
)
}
const wrapper = mount(<Mock label="test1" />)
expect(wrapper.find('thead').find('tr').at(0).text()).toBe('test1')
act(() => {
wrapper.setProps({ label: 'test2' })
})
expect(wrapper.find('thead').find('tr').at(0).text()).toBe('test2')
expect(() => wrapper.unmount()).not.toThrow()
})
})