|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { shallow } from 'enzyme'; |
| 4 | + |
| 5 | +import { extendTo } from '../test-helpers/mock-component'; |
| 6 | +import baseResolver from '../../src/props-resolver/index'; |
| 7 | +import Const from '../../src/const'; |
| 8 | + |
| 9 | +describe('TableResolver', () => { |
| 10 | + const keyField = 'id'; |
| 11 | + const columns = [{ |
| 12 | + dataField: keyField, |
| 13 | + text: 'ID' |
| 14 | + }, { |
| 15 | + dataField: 'name', |
| 16 | + text: 'Name' |
| 17 | + }]; |
| 18 | + const data = [{ |
| 19 | + id: 1, |
| 20 | + name: 'A' |
| 21 | + }, { |
| 22 | + id: 2, |
| 23 | + name: 'B' |
| 24 | + }]; |
| 25 | + const ExtendBase = baseResolver(Component); |
| 26 | + const BootstrapTableMock = extendTo(ExtendBase); |
| 27 | + let wrapper; |
| 28 | + |
| 29 | + describe('validateProps', () => { |
| 30 | + describe('if keyField is defined and columns is all visible', () => { |
| 31 | + beforeEach(() => { |
| 32 | + const mockElement = React.createElement(BootstrapTableMock, { |
| 33 | + data, columns, keyField |
| 34 | + }, null); |
| 35 | + wrapper = shallow(mockElement); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not throw any errors', () => { |
| 39 | + expect(() => wrapper.instance().validateProps()).not.toThrow(); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('if keyField is not defined on props', () => { |
| 44 | + beforeEach(() => { |
| 45 | + const mockElement = React.createElement(BootstrapTableMock, { |
| 46 | + data, columns |
| 47 | + }, null); |
| 48 | + wrapper = shallow(mockElement); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw error', () => { |
| 52 | + expect(() => |
| 53 | + wrapper.instance().validateProps() |
| 54 | + ).toThrow(new Error('Please specify a field as key via keyField')); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('if columns is all unvisible', () => { |
| 59 | + beforeEach(() => { |
| 60 | + const mockElement = React.createElement(BootstrapTableMock, { |
| 61 | + data, keyField, columns: [] |
| 62 | + }, null); |
| 63 | + wrapper = shallow(mockElement); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw error', () => { |
| 67 | + expect(() => |
| 68 | + wrapper.instance().validateProps() |
| 69 | + ).toThrow(new Error('No any visible columns detect')); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('resolveCellEditProps', () => { |
| 75 | + describe('if cellEdit prop not defined', () => { |
| 76 | + beforeEach(() => { |
| 77 | + const mockElement = React.createElement(BootstrapTableMock, { |
| 78 | + data, keyField, columns |
| 79 | + }, null); |
| 80 | + wrapper = shallow(mockElement); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should resolve a default cellEdit instance', () => { |
| 84 | + const cellEdit = wrapper.instance().resolveCellEditProps(); |
| 85 | + expect(cellEdit).toBeDefined(); |
| 86 | + expect(cellEdit.mode).toEqual(Const.UNABLE_TO_CELL_EDIT); |
| 87 | + expect(cellEdit.nonEditableRows.length).toEqual(0); |
| 88 | + expect(cellEdit.ridx).toBeNull(); |
| 89 | + expect(cellEdit.cidx).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should resolve a default cellEdit instance even if state.currEditCell changed', () => { |
| 93 | + const ridx = 1; |
| 94 | + const cidx = 1; |
| 95 | + wrapper.setState({ currEditCell: { ridx, cidx } }); |
| 96 | + const cellEdit = wrapper.instance().resolveCellEditProps(); |
| 97 | + expect(cellEdit).toBeDefined(); |
| 98 | + expect(cellEdit.ridx).toEqual(ridx); |
| 99 | + expect(cellEdit.cidx).toEqual(cidx); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('if cellEdit prop defined', () => { |
| 105 | + const expectNonEditableRows = [1, 2]; |
| 106 | + const cellEdit = { |
| 107 | + mode: Const.DBCLICK_TO_CELL_EDIT, |
| 108 | + onEditing: sinon.stub(), |
| 109 | + blurToSave: true, |
| 110 | + beforeSaveCell: sinon.stub(), |
| 111 | + afterSaveCell: sinon.stub(), |
| 112 | + nonEditableRows: sinon.stub().returns(expectNonEditableRows) |
| 113 | + }; |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + const mockElement = React.createElement(BootstrapTableMock, { |
| 117 | + data, keyField, columns, cellEdit |
| 118 | + }, null); |
| 119 | + wrapper = shallow(mockElement); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should resolve a cellEdit correctly', () => { |
| 123 | + const cellEditInfo = wrapper.instance().resolveCellEditProps(); |
| 124 | + expect(cellEditInfo).toBeDefined(); |
| 125 | + expect(cellEditInfo.ridx).toBeNull(); |
| 126 | + expect(cellEditInfo.cidx).toBeNull(); |
| 127 | + expect(cellEditInfo.mode).toEqual(cellEdit.mode); |
| 128 | + expect(cellEditInfo.onEditing).toEqual(cellEdit.onEditing); |
| 129 | + expect(cellEditInfo.blurToSave).toEqual(cellEdit.blurToSave); |
| 130 | + expect(cellEditInfo.beforeSaveCell).toEqual(cellEdit.beforeSaveCell); |
| 131 | + expect(cellEditInfo.afterSaveCell).toEqual(cellEdit.afterSaveCell); |
| 132 | + expect(cellEditInfo.nonEditableRows).toEqual(expectNonEditableRows); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should attach options to cellEdit props', () => { |
| 136 | + const something = { |
| 137 | + test: 1, |
| 138 | + cb: sinon.stub() |
| 139 | + }; |
| 140 | + const cellEditInfo = wrapper.instance().resolveCellEditProps(something); |
| 141 | + expect(cellEditInfo).toBeDefined(); |
| 142 | + expect(cellEditInfo.test).toEqual(something.test); |
| 143 | + expect(cellEditInfo.cb).toEqual(something.cb); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments