|
1 | | -import { mount } from 'enzyme'; |
| 1 | +import { mount, ReactWrapper } from 'enzyme'; |
2 | 2 | import React from 'react'; |
| 3 | +import TableContext, { ITableContext } from '../../TableContext'; |
| 4 | +import TableSectionContext, { TableSection } from '../../TableSectionContext'; |
| 5 | +import TableCell from '../TableCell'; |
3 | 6 | import TableRow from '../TableRow'; |
4 | 7 |
|
| 8 | +const assertCellText = (c: ReactWrapper, cellNumber: number, text: string) => { |
| 9 | + expect(c.find(`[data-test="cell-${cellNumber}"]`).first().text()).toEqual(text); |
| 10 | +} |
| 11 | + |
5 | 12 | describe('Table.Row', () => { |
6 | 13 | it('matches snapshot', () => { |
7 | 14 | const wrapper = mount( |
8 | 15 | <table> |
9 | | - <TableRow /> |
| 16 | + <tbody> |
| 17 | + <TableRow /> |
| 18 | + </tbody> |
10 | 19 | </table>, |
11 | 20 | ); |
12 | 21 | expect(wrapper).toMatchSnapshot(); |
13 | 22 | wrapper.unmount(); |
14 | 23 | }); |
| 24 | + |
| 25 | + it('renders headers in the first column if responsive', () => { |
| 26 | + const contextValue: ITableContext = { |
| 27 | + isResponsive: true, |
| 28 | + headings: ['a', 'b', 'c'], |
| 29 | + setHeadings: jest.fn(), |
| 30 | + }; |
| 31 | + const wrapper = mount( |
| 32 | + <table> |
| 33 | + <TableContext.Provider value={contextValue}> |
| 34 | + <TableSectionContext.Provider value={TableSection.BODY}> |
| 35 | + <tbody> |
| 36 | + <TableRow> |
| 37 | + <TableCell data-test="cell-1">1</TableCell> |
| 38 | + <TableCell data-test="cell-2">2</TableCell> |
| 39 | + <TableCell data-test="cell-3">3</TableCell> |
| 40 | + </TableRow> |
| 41 | + </tbody> |
| 42 | + </TableSectionContext.Provider> |
| 43 | + </TableContext.Provider> |
| 44 | + </table> |
| 45 | + ); |
| 46 | + |
| 47 | + assertCellText(wrapper, 1, 'a 1'); |
| 48 | + assertCellText(wrapper, 2, 'b 2'); |
| 49 | + assertCellText(wrapper, 3, 'c 3'); |
| 50 | + expect(wrapper.find('.nhsuk-table-responsive__heading').length).toBe(3); |
| 51 | + |
| 52 | + wrapper.unmount(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders row contents without headers in responsive mode if they are not cells', () => { |
| 56 | + const contextValue: ITableContext = { |
| 57 | + isResponsive: true, |
| 58 | + headings: ['a', 'b', 'c'], |
| 59 | + setHeadings: jest.fn(), |
| 60 | + }; |
| 61 | + const wrapper = mount( |
| 62 | + <table> |
| 63 | + <TableContext.Provider value={contextValue}> |
| 64 | + <TableSectionContext.Provider value={TableSection.BODY}> |
| 65 | + <tbody> |
| 66 | + <TableRow> |
| 67 | + <td data-test="cell-1">1</td> |
| 68 | + <td data-test="cell-2">2</td> |
| 69 | + <td data-test="cell-3">3</td> |
| 70 | + </TableRow> |
| 71 | + </tbody> |
| 72 | + </TableSectionContext.Provider> |
| 73 | + </TableContext.Provider> |
| 74 | + </table> |
| 75 | + ); |
| 76 | + |
| 77 | + assertCellText(wrapper, 1, '1'); |
| 78 | + assertCellText(wrapper, 2, '2'); |
| 79 | + assertCellText(wrapper, 3, '3'); |
| 80 | + expect(wrapper.find('.nhsuk-table-responsive__heading').length).toBe(0); |
| 81 | + |
| 82 | + wrapper.unmount(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('renders row contents as headers in head section in responsive mode', () => { |
| 86 | + const setHeadings = jest.fn(); |
| 87 | + const contextValue: ITableContext = { |
| 88 | + isResponsive: true, |
| 89 | + headings: ['a', 'b', 'c'], |
| 90 | + setHeadings, |
| 91 | + }; |
| 92 | + const wrapper = mount( |
| 93 | + <table> |
| 94 | + <TableContext.Provider value={contextValue}> |
| 95 | + <TableSectionContext.Provider value={TableSection.HEAD}> |
| 96 | + <thead> |
| 97 | + <TableRow> |
| 98 | + <TableCell data-test="cell-1">1</TableCell> |
| 99 | + <TableCell data-test="cell-2">2</TableCell> |
| 100 | + <TableCell data-test="cell-3">3</TableCell> |
| 101 | + </TableRow> |
| 102 | + </thead> |
| 103 | + </TableSectionContext.Provider> |
| 104 | + </TableContext.Provider> |
| 105 | + </table> |
| 106 | + ); |
| 107 | + |
| 108 | + expect(setHeadings).toHaveBeenCalledWith(['1', '2', '3']); |
| 109 | + |
| 110 | + wrapper.unmount(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('sets headers, skipping contents outside of table cells in responsive mode', () => { |
| 114 | + const setHeadings = jest.fn(); |
| 115 | + const contextValue: ITableContext = { |
| 116 | + isResponsive: true, |
| 117 | + headings: ['a', 'b', 'c'], |
| 118 | + setHeadings, |
| 119 | + }; |
| 120 | + const wrapper = mount( |
| 121 | + <table> |
| 122 | + <TableContext.Provider value={contextValue}> |
| 123 | + <TableSectionContext.Provider value={TableSection.HEAD}> |
| 124 | + <thead> |
| 125 | + <TableRow> |
| 126 | + <td data-test="cell-1">1</td> |
| 127 | + <TableCell data-test="cell-2">2</TableCell> |
| 128 | + <td data-test="cell-3">3</td> |
| 129 | + </TableRow> |
| 130 | + </thead> |
| 131 | + </TableSectionContext.Provider> |
| 132 | + </TableContext.Provider> |
| 133 | + </table> |
| 134 | + ); |
| 135 | + |
| 136 | + expect(setHeadings).toHaveBeenCalledWith(['2']); |
| 137 | + |
| 138 | + wrapper.unmount(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('does not render row contents as headers in head section in normal mode', () => { |
| 142 | + const contextValue: ITableContext = { |
| 143 | + isResponsive: false, |
| 144 | + headings: ['a', 'b', 'c'], |
| 145 | + setHeadings: jest.fn(), |
| 146 | + }; |
| 147 | + const wrapper = mount( |
| 148 | + <table> |
| 149 | + <TableContext.Provider value={contextValue}> |
| 150 | + <TableSectionContext.Provider value={TableSection.HEAD}> |
| 151 | + <thead> |
| 152 | + <TableRow> |
| 153 | + <TableCell data-test="cell-1">1</TableCell> |
| 154 | + <TableCell data-test="cell-2">2</TableCell> |
| 155 | + <TableCell data-test="cell-3">3</TableCell> |
| 156 | + </TableRow> |
| 157 | + </thead> |
| 158 | + </TableSectionContext.Provider> |
| 159 | + </TableContext.Provider> |
| 160 | + </table> |
| 161 | + ); |
| 162 | + |
| 163 | + assertCellText(wrapper, 1, '1'); |
| 164 | + assertCellText(wrapper, 2, '2'); |
| 165 | + assertCellText(wrapper, 3, '3'); |
| 166 | + expect(wrapper.find('.nhsuk-table-responsive__heading').length).toBe(0); |
| 167 | + |
| 168 | + wrapper.unmount(); |
| 169 | + }); |
15 | 170 | }); |
0 commit comments