ant-design/components/table/__tests__/SelectionBox.test.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-12-14 14:48:09 +08:00
import React from 'react';
import { mount } from 'enzyme';
import createStore from '../createStore';
import SelectionBox from '../SelectionBox';
2018-12-07 16:17:45 +08:00
const getDefaultStore = selectedRowKeys =>
createStore({
selectedRowKeys: selectedRowKeys || [],
selectionDirty: false,
});
2016-12-14 14:48:09 +08:00
describe('SelectionBox', () => {
it('unchecked by selectedRowKeys ', () => {
const wrapper = mount(
<SelectionBox
store={getDefaultStore()}
2016-12-14 14:48:09 +08:00
rowIndex="1"
disabled={false}
onChange={() => {}}
defaultSelection={[]}
2018-12-07 16:17:45 +08:00
/>,
2016-12-14 14:48:09 +08:00
);
expect(wrapper.state()).toEqual({ checked: false });
});
it('checked by selectedRowKeys ', () => {
const wrapper = mount(
<SelectionBox
store={getDefaultStore(['1'])}
2016-12-14 14:48:09 +08:00
rowIndex="1"
disabled={false}
onChange={() => {}}
defaultSelection={[]}
2018-12-07 16:17:45 +08:00
/>,
2016-12-14 14:48:09 +08:00
);
expect(wrapper.state()).toEqual({ checked: true });
});
it('checked by defaultSelection', () => {
const wrapper = mount(
<SelectionBox
store={getDefaultStore()}
2016-12-14 14:48:09 +08:00
rowIndex="1"
disabled={false}
onChange={() => {}}
defaultSelection={['1']}
2018-12-07 16:17:45 +08:00
/>,
2016-12-14 14:48:09 +08:00
);
expect(wrapper.state()).toEqual({ checked: true });
});
it('checked when store change', () => {
const store = getDefaultStore();
2016-12-14 14:48:09 +08:00
const wrapper = mount(
<SelectionBox
store={store}
rowIndex="1"
disabled={false}
onChange={() => {}}
defaultSelection={[]}
2018-12-07 16:17:45 +08:00
/>,
2016-12-14 14:48:09 +08:00
);
store.setState({
selectedRowKeys: ['1'],
selectionDirty: true,
});
expect(wrapper.state()).toEqual({ checked: true });
});
it('passes props to Checkbox', () => {
const checkboxProps = {
name: 'testName',
id: 'testId',
};
const wrapper = mount(
<SelectionBox
store={getDefaultStore()}
rowIndex="1"
disabled={false}
2018-12-07 16:17:45 +08:00
onChange={() => {}}
defaultSelection={['1']}
{...checkboxProps}
2018-12-07 16:17:45 +08:00
/>,
);
2018-12-07 16:17:45 +08:00
wrapper.find('Checkbox').forEach(box => {
expect(box.props().name).toEqual(checkboxProps.name);
expect(box.props().id).toEqual(checkboxProps.id);
});
});
it('passes props to Radios', () => {
const radioProps = {
name: 'testName',
id: 'testId',
};
const wrapper = mount(
<SelectionBox
store={getDefaultStore()}
rowIndex="1"
disabled={false}
2018-12-07 16:17:45 +08:00
onChange={() => {}}
defaultSelection={['1']}
type="radio"
{...radioProps}
2018-12-07 16:17:45 +08:00
/>,
);
2018-12-07 16:17:45 +08:00
wrapper.find('Radio').forEach(radio => {
expect(radio.props().name).toEqual(radioProps.name);
expect(radio.props().id).toEqual(radioProps.id);
});
});
2016-12-14 14:48:09 +08:00
});