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

134 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-07-25 13:27:58 +00:00
import * as Vue from 'vue';
2019-01-12 03:33:27 +00:00
import { mount } from '@vue/test-utils';
import createStore from '../createStore';
import SelectionBox from '../SelectionBox';
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
const getDefaultStore = selectedRowKeys => {
2018-06-01 13:25:16 +00:00
return createStore({
selectedRowKeys: selectedRowKeys || [],
selectionDirty: false,
2019-01-12 03:33:27 +00:00
});
};
2018-06-01 13:25:16 +00:00
describe('SelectionBox', () => {
it('unchecked by selectedRowKeys ', () => {
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
2018-06-01 13:25:16 +00:00
store: getDefaultStore(),
rowIndex: '1',
disabled: false,
onChange: () => {},
defaultSelection: [],
},
listeners: {
change: () => {},
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.$data).toEqual({ checked: false });
});
2018-06-01 13:25:16 +00:00
it('checked by selectedRowKeys ', () => {
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
2018-06-01 13:25:16 +00:00
store: getDefaultStore(['1']),
rowIndex: '1',
disabled: false,
onChange: () => {},
defaultSelection: [],
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.$data).toEqual({ checked: true });
});
2018-06-01 13:25:16 +00:00
it('checked by defaultSelection', () => {
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
2018-06-01 13:25:16 +00:00
store: getDefaultStore(),
rowIndex: '1',
disabled: false,
onChange: () => {},
defaultSelection: ['1'],
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.$data).toEqual({ checked: true });
});
2018-06-01 13:25:16 +00:00
it('checked when store change', () => {
2019-01-12 03:33:27 +00:00
const store = getDefaultStore();
2018-06-01 13:25:16 +00:00
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
store,
2018-06-01 13:25:16 +00:00
rowIndex: '1',
disabled: false,
2020-08-12 08:30:13 +00:00
onChange: () => {},
2018-06-01 13:25:16 +00:00
defaultSelection: [],
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
store.setState({
selectedRowKeys: ['1'],
selectionDirty: true,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
expect(wrapper.vm.$data).toEqual({ checked: true });
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
it('passes props to Checkbox', done => {
2018-06-01 13:25:16 +00:00
const checkboxProps = {
name: 'testName',
id: 'testId',
2019-01-12 03:33:27 +00:00
};
2018-06-01 13:25:16 +00:00
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
2018-06-01 13:25:16 +00:00
store: getDefaultStore(),
rowIndex: '1',
disabled: false,
2020-08-12 08:30:13 +00:00
onChange: () => {},
2018-06-01 13:25:16 +00:00
defaultSelection: ['1'],
...checkboxProps,
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
Vue.nextTick(() => {
2020-08-12 08:30:13 +00:00
wrapper.findAllComponents({ name: 'ACheckbox' }).forEach(box => {
2019-01-12 03:33:27 +00:00
expect(box.props().name).toEqual(checkboxProps.name);
expect(box.props().id).toEqual(checkboxProps.id);
});
done();
});
});
2018-06-01 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
it('passes props to Radios', done => {
2018-06-01 13:25:16 +00:00
const radioProps = {
name: 'testName',
id: 'testId',
2019-01-12 03:33:27 +00:00
};
2018-06-01 13:25:16 +00:00
const wrapper = mount(SelectionBox, {
2020-07-25 07:46:49 +00:00
props: {
2018-06-01 13:25:16 +00:00
store: getDefaultStore(),
rowIndex: '1',
disabled: false,
2020-08-12 08:30:13 +00:00
onChange: () => {},
2018-06-01 13:25:16 +00:00
defaultSelection: ['1'],
type: 'radio',
...radioProps,
},
sync: false,
2019-01-12 03:33:27 +00:00
});
2018-06-01 13:25:16 +00:00
Vue.nextTick(() => {
2020-08-12 08:30:13 +00:00
wrapper.findAllComponents({ name: 'ARadio' }).forEach(radio => {
2019-01-12 03:33:27 +00:00
expect(radio.props().name).toEqual(radioProps.name);
expect(radio.props().id).toEqual(radioProps.id);
});
done();
});
});
});