|
1 | | -// @TODO |
| 1 | +import React from 'react' |
| 2 | +import { findDOMNode } from 'react-dom' |
| 3 | +import { render } from '../TestUtils' |
| 4 | +import CellMeasurer from './CellMeasurer' |
| 5 | + |
| 6 | +const HEIGHTS = [75, 50, 125, 100, 150] |
| 7 | +const WIDTHS = [125, 50, 200, 175, 100] |
| 8 | + |
| 9 | +function createCellRenderer () { |
| 10 | + const cellRendererParams = [] |
| 11 | + const cellRenderer = (params) => { |
| 12 | + cellRendererParams.push(params) |
| 13 | + return ( |
| 14 | + <div style={{ |
| 15 | + height: HEIGHTS[params.columnIndex], |
| 16 | + width: WIDTHS[params.rowIndex] |
| 17 | + }}> |
| 18 | + cell |
| 19 | + </div> |
| 20 | + ) |
| 21 | + } |
| 22 | + |
| 23 | + return { |
| 24 | + cellRenderer, |
| 25 | + cellRendererParams |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function renderHelper ({ |
| 30 | + cellRenderer, |
| 31 | + columnCount = 1, |
| 32 | + columnWidth, |
| 33 | + rowCount = 1, |
| 34 | + rowHeight |
| 35 | +} = {}) { |
| 36 | + const params = {} |
| 37 | + findDOMNode(render( |
| 38 | + <div> |
| 39 | + <CellMeasurer |
| 40 | + cellRenderer={cellRenderer} |
| 41 | + columnCount={columnCount} |
| 42 | + height={rowHeight} |
| 43 | + rowCount={rowCount} |
| 44 | + width={columnWidth} |
| 45 | + > |
| 46 | + {({ getColumnWidth, getRowHeight }) => { |
| 47 | + params.getColumnWidth = getColumnWidth |
| 48 | + params.getRowHeight = getRowHeight |
| 49 | + |
| 50 | + return <div>foo</div> |
| 51 | + }} |
| 52 | + </CellMeasurer> |
| 53 | + </div> |
| 54 | + )) |
| 55 | + |
| 56 | + return params |
| 57 | +} |
| 58 | + |
| 59 | +describe('CellMeasurer', () => { |
| 60 | + it('should calculate the height of a single-column row', () => { |
| 61 | + const { |
| 62 | + cellRenderer, |
| 63 | + cellRendererParams |
| 64 | + } = createCellRenderer() |
| 65 | + const params = renderHelper({ |
| 66 | + cellRenderer, |
| 67 | + columnWidth: 100 |
| 68 | + }) |
| 69 | + expect(cellRendererParams).toEqual([]) |
| 70 | + expect(params.getRowHeight({ index: 0 })).toEqual(75) |
| 71 | + expect(cellRendererParams).toEqual([{ columnIndex: 0, rowIndex: 0 }]) |
| 72 | + expect(params.getColumnWidth({ index: 0 })).toEqual(100) |
| 73 | + |
| 74 | + // For some reason this explicit unmount is necessary. |
| 75 | + // Without it, Jasmine's :afterEach doesn't pick up and unmount the component correctly. |
| 76 | + render.unmount() |
| 77 | + }) |
| 78 | + |
| 79 | + it('should calculate the width of a single-row column', () => { |
| 80 | + const { |
| 81 | + cellRenderer, |
| 82 | + cellRendererParams |
| 83 | + } = createCellRenderer() |
| 84 | + const params = renderHelper({ |
| 85 | + cellRenderer, |
| 86 | + rowHeight: 50 |
| 87 | + }) |
| 88 | + expect(cellRendererParams).toEqual([]) |
| 89 | + expect(params.getColumnWidth({ index: 0 })).toEqual(125) |
| 90 | + expect(cellRendererParams).toEqual([{ columnIndex: 0, rowIndex: 0 }]) |
| 91 | + expect(params.getRowHeight({ index: 0 })).toEqual(50) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should calculate the height of a multi-column row based on the tallest column-cell', () => { |
| 95 | + const { |
| 96 | + cellRenderer, |
| 97 | + cellRendererParams |
| 98 | + } = createCellRenderer() |
| 99 | + const params = renderHelper({ |
| 100 | + cellRenderer, |
| 101 | + columnCount: 5, |
| 102 | + columnWidth: 100 |
| 103 | + }) |
| 104 | + expect(cellRendererParams.length).toEqual(0) |
| 105 | + expect(params.getRowHeight({ index: 0 })).toEqual(150) |
| 106 | + expect(cellRendererParams.length).toEqual(5) |
| 107 | + expect(params.getColumnWidth({ index: 0 })).toEqual(100) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should calculate the width of a multi-row column based on the widest row-cell', () => { |
| 111 | + const { |
| 112 | + cellRenderer, |
| 113 | + cellRendererParams |
| 114 | + } = createCellRenderer() |
| 115 | + const params = renderHelper({ |
| 116 | + cellRenderer, |
| 117 | + rowCount: 5, |
| 118 | + rowHeight: 50 |
| 119 | + }) |
| 120 | + expect(cellRendererParams.length).toEqual(0) |
| 121 | + expect(params.getColumnWidth({ index: 0 })).toEqual(200) |
| 122 | + expect(cellRendererParams.length).toEqual(5) |
| 123 | + expect(params.getRowHeight({ index: 0 })).toEqual(50) |
| 124 | + }) |
| 125 | +}) |
0 commit comments