| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {act, fireEvent, render, screen, within} from '@testing-library/react'; |
| b69ab31 | | | 11 | import userEvent from '@testing-library/user-event'; |
| b69ab31 | | | 12 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 13 | import {ViewportOverlayRoot} from 'isl-components/ViewportOverlay'; |
| b69ab31 | | | 14 | import App from '../App'; |
| b69ab31 | | | 15 | import { |
| b69ab31 | | | 16 | COMMIT, |
| b69ab31 | | | 17 | closeCommitInfoSidebar, |
| b69ab31 | | | 18 | expectMessageSentToServer, |
| b69ab31 | | | 19 | resetTestMessages, |
| b69ab31 | | | 20 | simulateCommits, |
| b69ab31 | | | 21 | } from '../testUtils'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | describe('tooltips in ISL', () => { |
| b69ab31 | | | 26 | let unmount: () => void; |
| b69ab31 | | | 27 | beforeEach(() => { |
| b69ab31 | | | 28 | resetTestMessages(); |
| b69ab31 | | | 29 | unmount = render(<App />).unmount; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | act(() => { |
| b69ab31 | | | 32 | closeCommitInfoSidebar(); |
| b69ab31 | | | 33 | expectMessageSentToServer({ |
| b69ab31 | | | 34 | type: 'subscribe', |
| b69ab31 | | | 35 | kind: 'smartlogCommits', |
| b69ab31 | | | 36 | subscriptionID: expect.anything(), |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | simulateCommits({ |
| b69ab31 | | | 39 | value: [ |
| b69ab31 | | | 40 | COMMIT('1', 'some public base', '0', {phase: 'public'}), |
| b69ab31 | | | 41 | COMMIT('a', 'My Commit', '1'), |
| b69ab31 | | | 42 | COMMIT('b', 'Another Commit', 'a', {isDot: true}), |
| b69ab31 | | | 43 | ], |
| b69ab31 | | | 44 | }); |
| b69ab31 | | | 45 | }); |
| b69ab31 | | | 46 | }); |
| b69ab31 | | | 47 | afterEach(() => { |
| b69ab31 | | | 48 | unmount(); |
| b69ab31 | | | 49 | }); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | describe('click to show', () => { |
| b69ab31 | | | 52 | const clickSettingsGearToMakeTooltip = () => { |
| b69ab31 | | | 53 | const settingsButtonTooltipCreator = |
| b69ab31 | | | 54 | screen.getByTestId('settings-gear-button').parentElement!; |
| b69ab31 | | | 55 | expect(settingsButtonTooltipCreator).toBeInTheDocument(); |
| b69ab31 | | | 56 | act(() => { |
| b69ab31 | | | 57 | fireEvent.click(settingsButtonTooltipCreator); |
| b69ab31 | | | 58 | }); |
| b69ab31 | | | 59 | }; |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | it('shows settings dropdown when clicked', () => { |
| b69ab31 | | | 62 | clickSettingsGearToMakeTooltip(); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const settingsDropdown = within(screen.getByTestId('viewport-overlay-root')).getByTestId( |
| b69ab31 | | | 65 | 'settings-dropdown', |
| b69ab31 | | | 66 | ); |
| b69ab31 | | | 67 | expect(settingsDropdown).toBeInTheDocument(); |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | it('clicking inside tooltip does not dismiss it', () => { |
| b69ab31 | | | 71 | clickSettingsGearToMakeTooltip(); |
| b69ab31 | | | 72 | |
| b69ab31 | | | 73 | const settingsDropdown = within(screen.getByTestId('viewport-overlay-root')).getByTestId( |
| b69ab31 | | | 74 | 'settings-dropdown', |
| b69ab31 | | | 75 | ); |
| b69ab31 | | | 76 | const themeDropdown = within(settingsDropdown).getByText('Theme'); |
| b69ab31 | | | 77 | expect(themeDropdown).toBeInTheDocument(); |
| b69ab31 | | | 78 | act(() => { |
| b69ab31 | | | 79 | fireEvent.click(themeDropdown!); |
| b69ab31 | | | 80 | }); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | const settingsDropdown2 = within(screen.getByTestId('viewport-overlay-root')).getByTestId( |
| b69ab31 | | | 83 | 'settings-dropdown', |
| b69ab31 | | | 84 | ); |
| b69ab31 | | | 85 | expect(settingsDropdown2).toBeInTheDocument(); |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | it('clicking outside tooltip dismisses it', () => { |
| b69ab31 | | | 89 | const settingsButton = screen.getByTestId('settings-gear-button'); |
| b69ab31 | | | 90 | act(() => { |
| b69ab31 | | | 91 | fireEvent.click(settingsButton); |
| b69ab31 | | | 92 | }); |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | const settingsDropdown = within(screen.getByTestId('viewport-overlay-root')).queryByTestId( |
| b69ab31 | | | 95 | 'settings-dropdown', |
| b69ab31 | | | 96 | ); |
| b69ab31 | | | 97 | expect(settingsDropdown).toBeInTheDocument(); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | act(() => { |
| b69ab31 | | | 100 | fireEvent.click(screen.getByTestId('commit-a')!); |
| b69ab31 | | | 101 | }); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | const settingsDropdown2 = within(screen.getByTestId('viewport-overlay-root')).queryByTestId( |
| b69ab31 | | | 104 | 'settings-dropdown', |
| b69ab31 | | | 105 | ); |
| b69ab31 | | | 106 | expect(settingsDropdown2).not.toBeInTheDocument(); |
| b69ab31 | | | 107 | }); |
| b69ab31 | | | 108 | }); |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | describe('hover to show', () => { |
| b69ab31 | | | 111 | const REFRESH_BUTTON_HOVER_TEXT = 'Re-fetch latest commits and uncommitted changes.'; |
| b69ab31 | | | 112 | it('hovering refresh button shows tooltip', () => { |
| b69ab31 | | | 113 | const refreshButton = screen.getByTestId('refresh-button').parentElement as HTMLElement; |
| b69ab31 | | | 114 | userEvent.hover(refreshButton); |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | const refreshButtonTooltip = within(screen.getByTestId('viewport-overlay-root')).getByText( |
| b69ab31 | | | 117 | REFRESH_BUTTON_HOVER_TEXT, |
| b69ab31 | | | 118 | ); |
| b69ab31 | | | 119 | expect(refreshButtonTooltip).toBeInTheDocument(); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | userEvent.unhover(refreshButton); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | expect( |
| b69ab31 | | | 124 | within(screen.getByTestId('viewport-overlay-root')).queryByText(REFRESH_BUTTON_HOVER_TEXT), |
| b69ab31 | | | 125 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 126 | }); |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | it('escape key dismisses tooltip', () => { |
| b69ab31 | | | 129 | const refreshButton = screen.getByTestId('refresh-button').parentElement as HTMLElement; |
| b69ab31 | | | 130 | userEvent.hover(refreshButton); |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | const refreshButtonTooltip = within(screen.getByTestId('viewport-overlay-root')).getByText( |
| b69ab31 | | | 133 | REFRESH_BUTTON_HOVER_TEXT, |
| b69ab31 | | | 134 | ); |
| b69ab31 | | | 135 | expect(refreshButtonTooltip).toBeInTheDocument(); |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | userEvent.keyboard('{Escape}'); |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | expect( |
| b69ab31 | | | 140 | within(screen.getByTestId('viewport-overlay-root')).queryByText(REFRESH_BUTTON_HOVER_TEXT), |
| b69ab31 | | | 141 | ).not.toBeInTheDocument(); |
| b69ab31 | | | 142 | }); |
| b69ab31 | | | 143 | }); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | describe('tooltip', () => { |
| b69ab31 | | | 147 | function renderCustom(node: ReactNode) { |
| b69ab31 | | | 148 | render( |
| b69ab31 | | | 149 | <div className="isl-root"> |
| b69ab31 | | | 150 | <ViewportOverlayRoot /> |
| b69ab31 | | | 151 | {node} |
| b69ab31 | | | 152 | </div>, |
| b69ab31 | | | 153 | ); |
| b69ab31 | | | 154 | } |
| b69ab31 | | | 155 | |
| b69ab31 | | | 156 | describe('onDismiss', () => { |
| b69ab31 | | | 157 | it('calls onDismiss when hover leaves', () => { |
| b69ab31 | | | 158 | const onDismiss = jest.fn(); |
| b69ab31 | | | 159 | renderCustom( |
| b69ab31 | | | 160 | <Tooltip trigger="hover" title="hi" onDismiss={onDismiss}> |
| b69ab31 | | | 161 | hover me |
| b69ab31 | | | 162 | </Tooltip>, |
| b69ab31 | | | 163 | ); |
| b69ab31 | | | 164 | const tooltip = screen.getByText('hover me'); |
| b69ab31 | | | 165 | userEvent.hover(tooltip); |
| b69ab31 | | | 166 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 167 | userEvent.unhover(tooltip); |
| b69ab31 | | | 168 | expect(onDismiss).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 169 | }); |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | it('calls onDismiss when pressing escape', () => { |
| b69ab31 | | | 172 | const onDismiss = jest.fn(); |
| b69ab31 | | | 173 | renderCustom( |
| b69ab31 | | | 174 | <Tooltip trigger="hover" title="hi" onDismiss={onDismiss}> |
| b69ab31 | | | 175 | hover me |
| b69ab31 | | | 176 | </Tooltip>, |
| b69ab31 | | | 177 | ); |
| b69ab31 | | | 178 | const tooltip = screen.getByText('hover me'); |
| b69ab31 | | | 179 | userEvent.hover(tooltip); |
| b69ab31 | | | 180 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 181 | userEvent.keyboard('{Escape}'); |
| b69ab31 | | | 182 | expect(onDismiss).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 183 | }); |
| b69ab31 | | | 184 | |
| b69ab31 | | | 185 | it('calls onDismiss when clicking outside', () => { |
| b69ab31 | | | 186 | const onDismiss = jest.fn(); |
| b69ab31 | | | 187 | renderCustom( |
| b69ab31 | | | 188 | <div> |
| b69ab31 | | | 189 | <div>something else</div> |
| b69ab31 | | | 190 | <Tooltip trigger="click" component={() => <div>hi</div>} onDismiss={onDismiss}> |
| b69ab31 | | | 191 | click me |
| b69ab31 | | | 192 | </Tooltip> |
| b69ab31 | | | 193 | </div>, |
| b69ab31 | | | 194 | ); |
| b69ab31 | | | 195 | const tooltip = screen.getByText('click me'); |
| b69ab31 | | | 196 | fireEvent.click(tooltip); |
| b69ab31 | | | 197 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 198 | const other = screen.getByText('something else'); |
| b69ab31 | | | 199 | fireEvent.click(other); |
| b69ab31 | | | 200 | expect(onDismiss).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 201 | }); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | it('title fields on click tooltips does not trigger onDismiss', () => { |
| b69ab31 | | | 204 | const onDismiss = jest.fn(); |
| b69ab31 | | | 205 | renderCustom( |
| b69ab31 | | | 206 | <div> |
| b69ab31 | | | 207 | <div>something else</div> |
| b69ab31 | | | 208 | <Tooltip |
| b69ab31 | | | 209 | trigger="click" |
| b69ab31 | | | 210 | component={() => <div>hi</div>} |
| b69ab31 | | | 211 | title="hovered" |
| b69ab31 | | | 212 | onDismiss={onDismiss}> |
| b69ab31 | | | 213 | click me |
| b69ab31 | | | 214 | </Tooltip> |
| b69ab31 | | | 215 | </div>, |
| b69ab31 | | | 216 | ); |
| b69ab31 | | | 217 | const tooltip = screen.getByText('click me'); |
| b69ab31 | | | 218 | userEvent.hover(tooltip); |
| b69ab31 | | | 219 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 220 | userEvent.unhover(tooltip); |
| b69ab31 | | | 221 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 222 | }); |
| b69ab31 | | | 223 | |
| b69ab31 | | | 224 | it('dismiss prop in tooltip components calls onDismiss', () => { |
| b69ab31 | | | 225 | const onDismiss = jest.fn(); |
| b69ab31 | | | 226 | renderCustom( |
| b69ab31 | | | 227 | <Tooltip |
| b69ab31 | | | 228 | trigger="click" |
| b69ab31 | | | 229 | component={dismiss => ( |
| b69ab31 | | | 230 | <> |
| b69ab31 | | | 231 | <div>hi</div> |
| b69ab31 | | | 232 | <button onClick={dismiss}>my button</button> |
| b69ab31 | | | 233 | </> |
| b69ab31 | | | 234 | )} |
| b69ab31 | | | 235 | title="hovered" |
| b69ab31 | | | 236 | onDismiss={onDismiss}> |
| b69ab31 | | | 237 | click me |
| b69ab31 | | | 238 | </Tooltip>, |
| b69ab31 | | | 239 | ); |
| b69ab31 | | | 240 | const tooltip = screen.getByText('click me'); |
| b69ab31 | | | 241 | fireEvent.click(tooltip); |
| b69ab31 | | | 242 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | // clicking inside tooltip is fine |
| b69ab31 | | | 245 | const innerText = screen.getByText('hi'); |
| b69ab31 | | | 246 | fireEvent.click(innerText); |
| b69ab31 | | | 247 | expect(onDismiss).not.toHaveBeenCalled(); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | // action that causes dismiss prop causes onDismiss |
| b69ab31 | | | 250 | const innerDismiss = screen.getByText('my button'); |
| b69ab31 | | | 251 | fireEvent.click(innerDismiss); |
| b69ab31 | | | 252 | expect(onDismiss).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 253 | }); |
| b69ab31 | | | 254 | }); |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | describe('groups', () => { |
| b69ab31 | | | 257 | it('dismisses other tooltips in the same group', () => { |
| b69ab31 | | | 258 | const content = (value: string) => { |
| b69ab31 | | | 259 | return () => <div>{value}</div>; |
| b69ab31 | | | 260 | }; |
| b69ab31 | | | 261 | renderCustom( |
| b69ab31 | | | 262 | <div> |
| b69ab31 | | | 263 | <Tooltip trigger="click" group="test" component={content('Tooltip A')}> |
| b69ab31 | | | 264 | Button A |
| b69ab31 | | | 265 | </Tooltip> |
| b69ab31 | | | 266 | <Tooltip trigger="click" group="test" component={content('Tooltip B')}> |
| b69ab31 | | | 267 | Button B |
| b69ab31 | | | 268 | </Tooltip> |
| b69ab31 | | | 269 | </div>, |
| b69ab31 | | | 270 | ); |
| b69ab31 | | | 271 | const a = screen.getByText('Button A'); |
| b69ab31 | | | 272 | const b = screen.getByText('Button B'); |
| b69ab31 | | | 273 | fireEvent.click(a); |
| b69ab31 | | | 274 | expect(screen.getByText('Tooltip A')).toBeInTheDocument(); |
| b69ab31 | | | 275 | expect(screen.queryByText('Tooltip B')).not.toBeInTheDocument(); |
| b69ab31 | | | 276 | fireEvent.click(b); |
| b69ab31 | | | 277 | expect(screen.queryByText('Tooltip A')).not.toBeInTheDocument(); |
| b69ab31 | | | 278 | expect(screen.getByText('Tooltip B')).toBeInTheDocument(); |
| b69ab31 | | | 279 | }); |
| b69ab31 | | | 280 | |
| b69ab31 | | | 281 | it('does not dismiss from other groups', () => { |
| b69ab31 | | | 282 | const content = (value: string) => { |
| b69ab31 | | | 283 | return () => <div>{value}</div>; |
| b69ab31 | | | 284 | }; |
| b69ab31 | | | 285 | renderCustom( |
| b69ab31 | | | 286 | <div> |
| b69ab31 | | | 287 | <Tooltip trigger="click" group="test1" component={content('Tooltip A')}> |
| b69ab31 | | | 288 | Button A |
| b69ab31 | | | 289 | </Tooltip> |
| b69ab31 | | | 290 | <Tooltip trigger="click" group="test2" component={content('Tooltip B')}> |
| b69ab31 | | | 291 | Button B |
| b69ab31 | | | 292 | </Tooltip> |
| b69ab31 | | | 293 | </div>, |
| b69ab31 | | | 294 | ); |
| b69ab31 | | | 295 | const a = screen.getByText('Button A'); |
| b69ab31 | | | 296 | const b = screen.getByText('Button B'); |
| b69ab31 | | | 297 | fireEvent.click(a); |
| b69ab31 | | | 298 | expect(screen.getByText('Tooltip A')).toBeInTheDocument(); |
| b69ab31 | | | 299 | expect(screen.queryByText('Tooltip B')).not.toBeInTheDocument(); |
| b69ab31 | | | 300 | fireEvent.click(b); |
| b69ab31 | | | 301 | expect(screen.getByText('Tooltip A')).toBeInTheDocument(); |
| b69ab31 | | | 302 | expect(screen.getByText('Tooltip B')).toBeInTheDocument(); |
| b69ab31 | | | 303 | }); |
| b69ab31 | | | 304 | }); |
| b69ab31 | | | 305 | }); |