|
| 1 | +/* |
| 2 | +
|
| 3 | + MIT License |
| 4 | +
|
| 5 | + Copyright (c) 2020 Looker Data Sciences, Inc. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | +
|
| 25 | + */ |
| 26 | + |
| 27 | +import React from 'react' |
| 28 | +import { renderWithTheme } from '@looker/components-test-utils' |
| 29 | +import { fireEvent, screen } from '@testing-library/react' |
| 30 | +import userEvent from '@testing-library/user-event' |
| 31 | +import { DataTableCell } from './DataTableCell' |
| 32 | + |
| 33 | +describe('DataTableCell', () => { |
| 34 | + test('Basic content', () => { |
| 35 | + renderWithTheme( |
| 36 | + <table> |
| 37 | + <tbody> |
| 38 | + <tr> |
| 39 | + <DataTableCell>Cell content</DataTableCell> |
| 40 | + </tr> |
| 41 | + </tbody> |
| 42 | + </table> |
| 43 | + ) |
| 44 | + |
| 45 | + expect(screen.getByText('Cell content')).toBeInTheDocument() |
| 46 | + }) |
| 47 | + |
| 48 | + test('indicator', () => { |
| 49 | + renderWithTheme( |
| 50 | + <table> |
| 51 | + <tbody> |
| 52 | + <tr> |
| 53 | + <DataTableCell indicator="FauxIcon">Cell content</DataTableCell> |
| 54 | + </tr> |
| 55 | + </tbody> |
| 56 | + </table> |
| 57 | + ) |
| 58 | + |
| 59 | + expect(screen.getByText('FauxIcon')).toBeInTheDocument() |
| 60 | + }) |
| 61 | + |
| 62 | + test('description & indicator', () => { |
| 63 | + renderWithTheme( |
| 64 | + <table> |
| 65 | + <tbody> |
| 66 | + <tr> |
| 67 | + <DataTableCell indicator="FauxIcon" description="descriptive text"> |
| 68 | + Cell content |
| 69 | + </DataTableCell> |
| 70 | + </tr> |
| 71 | + </tbody> |
| 72 | + </table> |
| 73 | + ) |
| 74 | + |
| 75 | + expect(screen.getByText('descriptive text')).toBeInTheDocument() |
| 76 | + expect(screen.getByText('FauxIcon')).toBeInTheDocument() |
| 77 | + }) |
| 78 | + |
| 79 | + test('onKeyUp callback', () => { |
| 80 | + const onKeyUp = jest.fn() |
| 81 | + |
| 82 | + renderWithTheme( |
| 83 | + <table> |
| 84 | + <tbody> |
| 85 | + <tr> |
| 86 | + <DataTableCell onKeyUp={onKeyUp}>Cell content</DataTableCell> |
| 87 | + </tr> |
| 88 | + </tbody> |
| 89 | + </table> |
| 90 | + ) |
| 91 | + |
| 92 | + fireEvent.keyUp(screen.getByText('Cell content'), { |
| 93 | + charCode: 13, |
| 94 | + code: 13, |
| 95 | + key: 'Enter', |
| 96 | + }) |
| 97 | + |
| 98 | + expect(onKeyUp).toHaveBeenCalledTimes(1) |
| 99 | + }) |
| 100 | + |
| 101 | + test('keyup triggers :focus-visible blur removes', () => { |
| 102 | + renderWithTheme( |
| 103 | + <table> |
| 104 | + <tbody> |
| 105 | + <tr> |
| 106 | + <DataTableCell>Cell content</DataTableCell> |
| 107 | + </tr> |
| 108 | + </tbody> |
| 109 | + </table> |
| 110 | + ) |
| 111 | + |
| 112 | + const cell = screen.getByText('Cell content') |
| 113 | + const td = cell.closest('td') |
| 114 | + expect(td).toHaveStyleRule('outline', 'none', { |
| 115 | + modifier: ':focus', |
| 116 | + }) |
| 117 | + |
| 118 | + fireEvent.keyUp(cell, { |
| 119 | + charCode: 13, |
| 120 | + code: 13, |
| 121 | + key: 'Enter', |
| 122 | + }) |
| 123 | + |
| 124 | + expect(td).toHaveStyleRule('outline', '1px solid #6C43E0', { |
| 125 | + modifier: ':focus', |
| 126 | + }) |
| 127 | + |
| 128 | + fireEvent.blur(cell) |
| 129 | + |
| 130 | + expect(td).toHaveStyleRule('outline', 'none', { |
| 131 | + modifier: ':focus', |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + test('onClick callback + unset :focus-visible', () => { |
| 136 | + const onClick = jest.fn() |
| 137 | + |
| 138 | + renderWithTheme( |
| 139 | + <table> |
| 140 | + <tbody> |
| 141 | + <tr> |
| 142 | + <DataTableCell onClick={onClick}>Cell content</DataTableCell> |
| 143 | + </tr> |
| 144 | + </tbody> |
| 145 | + </table> |
| 146 | + ) |
| 147 | + |
| 148 | + const cell = screen.getByText('Cell content') |
| 149 | + const td = cell.closest('td') |
| 150 | + |
| 151 | + // Apply focus to cell |
| 152 | + fireEvent.keyUp(cell, { key: 'Enter' }) |
| 153 | + expect(td).toHaveStyleRule('outline', '1px solid #6C43E0', { |
| 154 | + modifier: ':focus', |
| 155 | + }) |
| 156 | + |
| 157 | + userEvent.click(cell) |
| 158 | + |
| 159 | + /** |
| 160 | + * TODO: Missing coverage |
| 161 | + * Frustratingly, this doesn't work in test but appears to work properly in real-world |
| 162 | + * |
| 163 | + * expect(td).toHaveStyleRule('outline', 'none', { |
| 164 | + * modifier: ':focus', |
| 165 | + * }) |
| 166 | + */ |
| 167 | + expect(onClick).toHaveBeenCalledTimes(1) |
| 168 | + }) |
| 169 | + |
| 170 | + test('tabIndex set properly on tabbable ', () => { |
| 171 | + renderWithTheme( |
| 172 | + <table> |
| 173 | + <tbody> |
| 174 | + <tr> |
| 175 | + <DataTableCell> |
| 176 | + <button>Click here</button> |
| 177 | + </DataTableCell> |
| 178 | + </tr> |
| 179 | + </tbody> |
| 180 | + </table> |
| 181 | + ) |
| 182 | + |
| 183 | + expect(screen.getByRole('button')).toHaveAttribute('tabIndex', '-1') |
| 184 | + }) |
| 185 | + |
| 186 | + test('onBlur callback', () => { |
| 187 | + const onBlur = jest.fn() |
| 188 | + |
| 189 | + renderWithTheme( |
| 190 | + <table> |
| 191 | + <tbody> |
| 192 | + <tr> |
| 193 | + <DataTableCell onBlur={onBlur}>Cell content</DataTableCell> |
| 194 | + </tr> |
| 195 | + </tbody> |
| 196 | + </table> |
| 197 | + ) |
| 198 | + |
| 199 | + const cell = screen.getByText('Cell content') |
| 200 | + cell.focus() |
| 201 | + |
| 202 | + userEvent.tab() |
| 203 | + expect(onBlur).toHaveBeenCalledTimes(1) |
| 204 | + }) |
| 205 | +}) |
0 commit comments