From ca5189d8ad9faa56a1444c2cfcf2be6a5e06d622 Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sat, 5 May 2018 18:18:21 +0800 Subject: [PATCH] patch tests for rich editors --- package.json | 3 +- .../test/editing-cell.test.js | 176 +++++++++++++++++- 2 files changed, 174 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 45f4029..56c17c7 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,8 @@ }, "jest": { "collectCoverageFrom": [ - "packages/**/*.js" + "packages/*/src/*.js", + "packages/*/index.js" ], "roots": [ "/packages" diff --git a/packages/react-bootstrap-table2-editor/test/editing-cell.test.js b/packages/react-bootstrap-table2-editor/test/editing-cell.test.js index 2741ee1..ee9853d 100644 --- a/packages/react-bootstrap-table2-editor/test/editing-cell.test.js +++ b/packages/react-bootstrap-table2-editor/test/editing-cell.test.js @@ -6,7 +6,12 @@ import { shallow, mount } from 'enzyme'; import _ from 'react-bootstrap-table-next/src/utils'; import editingCellFactory from '../src/editing-cell'; +import * as constants from '../src/const'; import TextEditor from '../src/text-editor'; +import DateEditor from '../src/date-editor'; +import DropDownEditor from '../src/dropdown-editor'; +import TextAreaEditor from '../src/textarea-editor'; +import CheckBoxEditor from '../src/checkbox-editor'; import EditorIndicator from '../src/editor-indicator'; const EditingCell = editingCellFactory(_); @@ -39,7 +44,7 @@ describe('EditingCell', () => { beforeEach(() => { onEscape = sinon.stub(); onUpdate = sinon.stub(); - wrapper = shallow( + wrapper = mount( { it('when press ENTER on TextEditor should call onUpdate correctly', () => { const newValue = 'test'; const textEditor = wrapper.find(TextEditor); - textEditor.simulate('keyDown', { keyCode: 13, currentTarget: { value: newValue } }); + sinon.stub(textEditor.instance(), 'getValue').returns(newValue); + textEditor.simulate('keyDown', { keyCode: 13 }); expect(onUpdate.callCount).toBe(1); expect(onUpdate.calledWith(row, column, newValue)).toBe(true); }); @@ -311,7 +317,7 @@ describe('EditingCell', () => { onEscape={ onEscape } /> ); - wrapper.instance().beforeComplete(row, column, newValue); + wrapper.instance().beforeComplete(newValue); }); it('should call column.validator successfully', () => { @@ -357,7 +363,17 @@ describe('EditingCell', () => { text: 'ID', validator: validatorCallBack }; - wrapper.instance().beforeComplete(row, column, newValue); + wrapper = mount( + + ); + wrapper.instance().beforeComplete(newValue); }); it('should call column.validator successfully', () => { @@ -370,4 +386,156 @@ describe('EditingCell', () => { }); }); }); + + describe('if column.editorRenderer is defined', () => { + const TestEditor = () => ; + + beforeEach(() => { + column = { + dataField: 'id', + text: 'ID', + editorRenderer: sinon.stub().returns() + }; + + wrapper = mount( + + ); + }); + + it('should call column.editorRenderer correctly', () => { + expect(column.editorRenderer.callCount).toBe(1); + }); + + it('should render correctly', () => { + expect(wrapper.find(TestEditor)).toHaveLength(1); + }); + }); + + describe('if column.editor is select', () => { + beforeEach(() => { + column = { + dataField: 'id', + text: 'ID', + editor: { + type: constants.EDITTYPE.SELECT, + options: [{ + value: 1, + label: 'A' + }] + } + }; + + wrapper = mount( + + ); + }); + + it('should render dropdown editor successfully', () => { + const editor = wrapper.find(DropDownEditor); + expect(wrapper.length).toBe(1); + expect(editor.length).toBe(1); + expect(editor.props().options).toEqual(column.editor.options); + }); + }); + + describe('if column.editor is textarea', () => { + beforeEach(() => { + column = { + dataField: 'id', + text: 'ID', + editor: { + type: constants.EDITTYPE.TEXTAREA + } + }; + + wrapper = mount( + + ); + }); + + it('should render textarea editor successfully', () => { + const editor = wrapper.find(TextAreaEditor); + expect(wrapper.length).toBe(1); + expect(editor.length).toBe(1); + }); + }); + + describe('if column.editor is checkbox', () => { + beforeEach(() => { + column = { + dataField: 'id', + text: 'ID', + editor: { + type: constants.EDITTYPE.CHECKBOX + } + }; + + wrapper = mount( + + ); + }); + + it('should render checkbox editor successfully', () => { + const editor = wrapper.find(CheckBoxEditor); + expect(wrapper.length).toBe(1); + expect(editor.length).toBe(1); + }); + }); + + describe('if column.editor is date', () => { + beforeEach(() => { + column = { + dataField: 'id', + text: 'ID', + editor: { + type: constants.EDITTYPE.DATE + } + }; + + wrapper = mount( + + ); + }); + + it('should render date editor successfully', () => { + const editor = wrapper.find(DateEditor); + expect(wrapper.length).toBe(1); + expect(editor.length).toBe(1); + }); + }); });