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

146 lines
3.3 KiB
JavaScript
Raw Normal View History

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