From fa13550d8c184a4daff696c5789824829caebc4b Mon Sep 17 00:00:00 2001 From: AllenFang Date: Fri, 21 Sep 2018 15:13:27 +0800 Subject: [PATCH] patch test for row selection consumer refactoring --- .../react-bootstrap-table2/test/body.test.js | 11 +-- .../test/contexts/index.test.js | 1 + ...ow-binder.test.js => row-consumer.test.js} | 6 +- .../test/row-selection/selection-cell.test.js | 83 ++++++++++++++++--- 4 files changed, 80 insertions(+), 21 deletions(-) rename packages/react-bootstrap-table2/test/row-selection/{row-binder.test.js => row-consumer.test.js} (98%) diff --git a/packages/react-bootstrap-table2/test/body.test.js b/packages/react-bootstrap-table2/test/body.test.js index e449877..c7d8894 100644 --- a/packages/react-bootstrap-table2/test/body.test.js +++ b/packages/react-bootstrap-table2/test/body.test.js @@ -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 => ; 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(); }); }); diff --git a/packages/react-bootstrap-table2/test/contexts/index.test.js b/packages/react-bootstrap-table2/test/contexts/index.test.js index 90e8a69..52ae08b 100644 --- a/packages/react-bootstrap-table2/test/contexts/index.test.js +++ b/packages/react-bootstrap-table2/test/contexts/index.test.js @@ -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) diff --git a/packages/react-bootstrap-table2/test/row-selection/row-binder.test.js b/packages/react-bootstrap-table2/test/row-selection/row-consumer.test.js similarity index 98% rename from packages/react-bootstrap-table2/test/row-selection/row-binder.test.js rename to packages/react-bootstrap-table2/test/row-selection/row-consumer.test.js index 0905eee..53cc804 100644 --- a/packages/react-bootstrap-table2/test/row-selection/row-binder.test.js +++ b/packages/react-bootstrap-table2/test/row-selection/row-consumer.test.js @@ -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 => ); + const WithSelectionComponent = withSelectionConsumer(props => ); const data = [{ id: 1, diff --git a/packages/react-bootstrap-table2/test/row-selection/selection-cell.test.js b/packages/react-bootstrap-table2/test/row-selection/selection-cell.test.js index 311c361..7de9a40 100644 --- a/packages/react-bootstrap-table2/test/row-selection/selection-cell.test.js +++ b/packages/react-bootstrap-table2/test/row-selection/selection-cell.test.js @@ -14,24 +14,85 @@ describe('', () => { 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( + + ); + }); - wrapper = shallow(); - - 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( + + ); + }); - wrapper = shallow(); + 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( + + ); + }); + + 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( + + ); + }); + + it('should return true', () => { + nextProps = { ...props, rowKey: '1' }; expect(wrapper.instance().shouldComponentUpdate(nextProps)).toBe(true); }); });