patch test for row selection consumer refactoring

This commit is contained in:
AllenFang
2018-09-21 15:13:27 +08:00
parent 709d59ce62
commit fa13550d8c
4 changed files with 80 additions and 21 deletions

View File

@@ -4,10 +4,10 @@ import sinon from 'sinon';
import { shallow, mount } from 'enzyme';
import Body from '../src/body';
import Row from '../src/row';
import RowAggregator from '../src/row-aggregator';
import Row from '../src/row/simple-row';
import RowAggregator from '../src/row/aggregate-row';
import Const from '../src/const';
import RowSection from '../src/row-section';
import RowSection from '../src/row/row-section';
import SelectionContext from '../src/contexts/selection-context';
import ExpansionContext from '../src/contexts/row-expand-context';
import mockBodyResolvedProps from './test-helpers/mock/body-resolved-props';
@@ -255,12 +255,11 @@ describe('Body', () => {
});
describe('when cellEdit.createContext props is defined', () => {
const CellComponent = () => null;
const EditingCellComponent = () => null;
const RowComponent = props => <Row { ...props } />;
const cellEdit = {
options: { onStartEdit: jest.fn() },
createContext: jest.fn(),
bindCellLevelCellEdit: jest.fn().mockReturnValue(CellComponent),
createEditingCell: jest.fn().mockReturnValue(EditingCellComponent),
bindRowLevelCellEdit: jest.fn().mockReturnValue(RowComponent)
};
@@ -278,12 +277,10 @@ describe('Body', () => {
it('should render Row Component correctly', () => {
expect(wrapper.length).toBe(1);
expect(cellEdit.bindCellLevelCellEdit).toHaveBeenCalledTimes(1);
expect(cellEdit.createEditingCell).toHaveBeenCalledTimes(1);
expect(cellEdit.bindRowLevelCellEdit).toHaveBeenCalledTimes(1);
expect(wrapper.find(RowComponent)).toHaveLength(2);
const aRowElement = wrapper.find(RowComponent).get(0);
expect(aRowElement.props.CellComponent).toBeDefined();
expect(aRowElement.props.EditingCellComponent).toBeDefined();
});
});

View File

@@ -112,6 +112,7 @@ describe('Context', () => {
Provider: CellEditContext.Provider,
Consumer: CellEditContext.Consumer
}),
options: {},
bindCellLevelCellEdit: jest.fn().mockReturnValue(() => null),
createEditingCell: jest.fn().mockReturnValue(() => null),
bindRowLevelCellEdit: jest.fn().mockReturnValue(() => null)

View File

@@ -3,13 +3,13 @@ import React from 'react';
import { mount } from 'enzyme';
import SelectionContext from '../../src/contexts/selection-context';
import bindSelection from '../../src/row-selection/row-binder';
import withSelectionConsumer from '../../src/row-selection/row-consumer';
describe('Selection Row Binder', () => {
describe('withSelectionConsumer', () => {
let wrapper;
let selectRow;
const BaseComponent = () => null;
const WithSelectionComponent = bindSelection(props => <BaseComponent { ...props } />);
const WithSelectionComponent = withSelectionConsumer(props => <BaseComponent { ...props } />);
const data = [{
id: 1,

View File

@@ -14,24 +14,85 @@ describe('<SelectionCell />', () => {
let wrapper;
describe('shouldComponentUpdate', () => {
const selected = true;
let props;
let nextProps;
describe('when selected prop has not been changed', () => {
it('should not update component', () => {
const nextProps = { selected };
describe('when selected prop has been changed', () => {
beforeEach(() => {
props = {
selected: false,
mode,
rowIndex,
disabled: false,
rowKey: 1
};
wrapper = shallow(
<SelectionCell { ...props } />
);
});
wrapper = shallow(<SelectionCell rowKey={ 1 } mode={ mode } selected={ selected } />);
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(false);
it('should return true', () => {
nextProps = { ...props, selected: true };
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
});
});
describe('when selected prop has been changed', () => {
it('should update component', () => {
const nextProps = { selected: !selected };
describe('when rowIndex prop has been changed', () => {
beforeEach(() => {
props = {
selected: false,
mode,
rowIndex,
disabled: false,
rowKey: 1
};
wrapper = shallow(
<SelectionCell { ...props } />
);
});
wrapper = shallow(<SelectionCell rowKey={ 1 } mode={ mode } selected={ selected } />);
it('should return true', () => {
nextProps = { ...props, rowIndex: 2 };
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
});
});
describe('when disabled prop has been changed', () => {
beforeEach(() => {
props = {
selected: false,
mode,
rowIndex,
disabled: false,
rowKey: 1
};
wrapper = shallow(
<SelectionCell { ...props } />
);
});
it('should return true', () => {
nextProps = { ...props, disabled: true };
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
});
});
describe('when rowKey prop has been changed', () => {
beforeEach(() => {
props = {
selected: false,
mode,
rowIndex,
disabled: false,
rowKey: 1
};
wrapper = shallow(
<SelectionCell { ...props } />
);
});
it('should return true', () => {
nextProps = { ...props, rowKey: '1' };
expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true);
});
});