|
1 | 1 | import React from 'react';
|
| 2 | +import sinon from 'sinon'; |
2 | 3 | import { shallow } from 'enzyme';
|
3 | 4 |
|
4 | 5 | import Cell from '../src/cell';
|
5 | 6 |
|
6 | 7 | describe('Cell', () => {
|
7 | 8 | let wrapper;
|
8 | | - const value = 'test'; |
| 9 | + const row = { |
| 10 | + id: 1, |
| 11 | + name: 'A' |
| 12 | + }; |
9 | 13 |
|
10 | 14 | describe('simplest cell', () => {
|
| 15 | + const column = { |
| 16 | + dataField: 'id', |
| 17 | + text: 'ID' |
| 18 | + }; |
| 19 | + |
11 | 20 | beforeEach(() => {
|
12 | | - wrapper = shallow(<Cell value={ value } />); |
| 21 | + wrapper = shallow(<Cell row={ row}rowIndex={1}column={column } />); |
13 | 22 | });
|
14 | 23 |
|
15 | 24 | it('should render successfully', () => {
|
16 | 25 | expect(wrapper.length).toBe(1);
|
17 | | - expect(wrapper.contains(<td>{ value }</td>)).toBe(true); |
| 26 | + expect(wrapper.contains(<td>{ row[column.dataField] }</td>)).toBe(true); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('when formatter prop is defined', () => { |
| 31 | + const rowIndex = 1; |
| 32 | + const column = { |
| 33 | + dataField: 'id', |
| 34 | + text: 'ID', |
| 35 | + formatExtraData: [] |
| 36 | + }; |
| 37 | + const formatterResult = (<h3>{ row[column.dataField] }</h3>); |
| 38 | + const formatter = sinon.stub() |
| 39 | + .withArgs(row[column.dataField], row, rowIndex, column.formatExtraData) |
| 40 | + .returns(formatterResult); |
| 41 | + column.formatter = formatter; // defined column formatter |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + wrapper = shallow(<Cell row={ row } rowIndex={ rowIndex } column={ column } />); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { formatter.reset(); }); |
| 48 | + |
| 49 | + it('should render successfully', () => { |
| 50 | + expect(wrapper.length).toBe(1); |
| 51 | + expect(wrapper.contains(<td><h3>{ row[column.dataField] }</h3></td>)).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should call custom formatter correctly', () => { |
| 55 | + expect(formatter.callCount).toBe(1); |
| 56 | + expect(formatter.calledWith(row[column.dataField], |
| 57 | + row, rowIndex, column.formatExtraData)).toBe(true); |
18 | 58 | });
|
19 | 59 | });
|
20 | 60 | });
|
0 commit comments