|
22 | 22 | * SOFTWARE. |
23 | 23 | */ |
24 | 24 | import React from 'react' |
| 25 | +import { useState } from 'react' |
25 | 26 | import 'cypress-real-events' |
26 | 27 |
|
27 | | -import { Tooltip } from '../../packages/ui' |
| 28 | +import { Modal, Tooltip, Button } from '@instructure/ui' |
| 29 | +import { IconInfoLine } from '@instructure/ui-icons' |
28 | 30 | import '../support/component' |
29 | 31 |
|
30 | 32 | describe('<Tooltip/>', () => { |
@@ -131,4 +133,231 @@ describe('<Tooltip/>', () => { |
131 | 133 | cy.contains('Hello').should('not.be.visible') |
132 | 134 | cy.wrap(onHideContent).should('have.been.calledOnce') |
133 | 135 | }) |
| 136 | + |
| 137 | + it('should remain visible when Tooltip is hovered over', async () => { |
| 138 | + cy.mount( |
| 139 | + <Tooltip renderTip={<h2>Hello</h2>}> |
| 140 | + <a data-testid="trigger" href="example.html"> |
| 141 | + Hover me! |
| 142 | + </a> |
| 143 | + </Tooltip> |
| 144 | + ) |
| 145 | + |
| 146 | + cy.get('[data-testid="trigger"]').then(($trigger) => { |
| 147 | + const tooltipId = $trigger.attr('data-position-target') |
| 148 | + const tooltip = `span[data-position-content="${tooltipId}"]` |
| 149 | + |
| 150 | + cy.get(tooltip).should('not.be.visible') |
| 151 | + |
| 152 | + cy.contains('Hover me!').realHover() |
| 153 | + |
| 154 | + cy.get(tooltip).should('be.visible') |
| 155 | + |
| 156 | + cy.contains('Hello').realHover().wait(100) |
| 157 | + |
| 158 | + cy.get(tooltip).should('be.visible') |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + it('should close when Esc key is pressed', async () => { |
| 163 | + cy.mount( |
| 164 | + <Tooltip renderTip={<h2>Hello</h2>}> |
| 165 | + <a data-testid="trigger" href="example.html"> |
| 166 | + Hover me! |
| 167 | + </a> |
| 168 | + </Tooltip> |
| 169 | + ) |
| 170 | + |
| 171 | + cy.get('[data-testid="trigger"]').then(($trigger) => { |
| 172 | + const tooltipId = $trigger.attr('data-position-target') |
| 173 | + const tooltip = `span[data-position-content="${tooltipId}"]` |
| 174 | + |
| 175 | + cy.get(tooltip).should('not.be.visible') |
| 176 | + |
| 177 | + cy.contains('Hover me!').realHover() |
| 178 | + |
| 179 | + cy.get(tooltip).should('be.visible').realPress('Escape') |
| 180 | + |
| 181 | + cy.get(tooltip).should('not.be.visible') |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + it('should close when Esc key is pressed and Tooltip is hovered over', async () => { |
| 186 | + cy.mount( |
| 187 | + <Tooltip renderTip={<h2>Hello</h2>}> |
| 188 | + <a data-testid="trigger" href="example.html"> |
| 189 | + Hover me! |
| 190 | + </a> |
| 191 | + </Tooltip> |
| 192 | + ) |
| 193 | + |
| 194 | + cy.get('[data-testid="trigger"]').then(($trigger) => { |
| 195 | + const tooltipId = $trigger.attr('data-position-target') |
| 196 | + const tooltip = `span[data-position-content="${tooltipId}"]` |
| 197 | + |
| 198 | + cy.get(tooltip).should('not.be.visible') |
| 199 | + |
| 200 | + cy.contains('Hover me!') |
| 201 | + .realHover() |
| 202 | + .then(() => { |
| 203 | + cy.get(tooltip).should('be.visible') |
| 204 | + }) |
| 205 | + |
| 206 | + cy.contains('Hello') |
| 207 | + .realHover() |
| 208 | + .then(() => { |
| 209 | + cy.realPress('Escape').then(() => { |
| 210 | + cy.get(tooltip).should('not.be.visible') |
| 211 | + }) |
| 212 | + }) |
| 213 | + }) |
| 214 | + }) |
| 215 | + |
| 216 | + it('should close when Esc key is pressed, but should not close the parent modal', () => { |
| 217 | + const TestModal = () => { |
| 218 | + const [open, setOpen] = useState(false) |
| 219 | + |
| 220 | + return ( |
| 221 | + <div> |
| 222 | + <Button onClick={() => setOpen((state) => !state)}> |
| 223 | + Open the Modal |
| 224 | + </Button> |
| 225 | + <Modal |
| 226 | + data-testid="modal" |
| 227 | + open={open} |
| 228 | + onDismiss={() => setOpen(false)} |
| 229 | + label="modal" |
| 230 | + > |
| 231 | + Hello, Word! |
| 232 | + <Tooltip renderTip="Hello. I'm a tool tip"> |
| 233 | + <IconInfoLine data-testid="trigger" /> |
| 234 | + </Tooltip> |
| 235 | + </Modal> |
| 236 | + </div> |
| 237 | + ) |
| 238 | + } |
| 239 | + cy.mount(<TestModal />) |
| 240 | + |
| 241 | + cy.contains('Open the Modal').click() |
| 242 | + |
| 243 | + cy.get('[data-testid="trigger"]').then(($trigger) => { |
| 244 | + const tooltipId = $trigger.attr('data-position-target') |
| 245 | + const tooltip = `span[data-position-content="${tooltipId}"]` |
| 246 | + |
| 247 | + cy.get(tooltip).should('not.be.visible') |
| 248 | + |
| 249 | + cy.get('[data-testid="trigger"]') |
| 250 | + .realHover() |
| 251 | + .then(() => { |
| 252 | + cy.get(tooltip).should('be.visible') |
| 253 | + }) |
| 254 | + |
| 255 | + cy.get(tooltip) |
| 256 | + .realPress('Escape') |
| 257 | + .then(() => { |
| 258 | + cy.get(tooltip).should('not.be.visible') |
| 259 | + cy.wait(300) |
| 260 | + cy.get('[role="dialog"]').should('be.visible') |
| 261 | + }) |
| 262 | + |
| 263 | + cy.get('[role="dialog"]') |
| 264 | + .realPress('Escape') |
| 265 | + .then(() => { |
| 266 | + cy.get('[role="dialog"]').should('not.exist') |
| 267 | + }) |
| 268 | + }) |
| 269 | + }) |
| 270 | + |
| 271 | + it('should allow closing modal with Esc when the modal trigger button has a Tooltip', async () => { |
| 272 | + const TestModal = () => { |
| 273 | + const [open, setOpen] = useState(false) |
| 274 | + |
| 275 | + return ( |
| 276 | + <div> |
| 277 | + <Tooltip renderTip="Hello. I'm a tool tip"> |
| 278 | + <Button |
| 279 | + onClick={() => { |
| 280 | + setOpen((state) => !state) |
| 281 | + }} |
| 282 | + > |
| 283 | + Open the Modal |
| 284 | + </Button> |
| 285 | + </Tooltip> |
| 286 | + <Modal |
| 287 | + open={open} |
| 288 | + onDismiss={() => { |
| 289 | + setOpen(false) |
| 290 | + }} |
| 291 | + label="modal" |
| 292 | + > |
| 293 | + Hello, World! |
| 294 | + </Modal> |
| 295 | + </div> |
| 296 | + ) |
| 297 | + } |
| 298 | + cy.mount(<TestModal />) |
| 299 | + |
| 300 | + cy.contains('Open the Modal').click() |
| 301 | + |
| 302 | + cy.get('[role="dialog"]').should('be.visible') |
| 303 | + |
| 304 | + cy.get('[role="dialog"]') |
| 305 | + .realPress('Escape') |
| 306 | + .then(() => { |
| 307 | + cy.get('[role="dialog"]').should('not.exist') |
| 308 | + }) |
| 309 | + }) |
| 310 | + |
| 311 | + it('should not trap focus when Modal closing button has a Tooltip', async () => { |
| 312 | + const TestModal = () => { |
| 313 | + const [open, setOpen] = useState(false) |
| 314 | + |
| 315 | + return ( |
| 316 | + <div> |
| 317 | + <Button |
| 318 | + onClick={() => { |
| 319 | + setOpen((state) => !state) |
| 320 | + }} |
| 321 | + > |
| 322 | + Open the Modal |
| 323 | + </Button> |
| 324 | + <Button>Hello</Button> |
| 325 | + <Modal |
| 326 | + label="modal" |
| 327 | + open={open} |
| 328 | + onDismiss={() => { |
| 329 | + setOpen((state) => !state) |
| 330 | + }} |
| 331 | + > |
| 332 | + <Tooltip renderTip="Hello. I'm a tool tip"> |
| 333 | + <Button |
| 334 | + onClick={() => { |
| 335 | + setOpen((state) => !state) |
| 336 | + }} |
| 337 | + > |
| 338 | + Close the Modal |
| 339 | + </Button> |
| 340 | + </Tooltip> |
| 341 | + </Modal> |
| 342 | + </div> |
| 343 | + ) |
| 344 | + } |
| 345 | + cy.mount(<TestModal />) |
| 346 | + |
| 347 | + cy.contains('Open the Modal').click() |
| 348 | + |
| 349 | + cy.get('[role="dialog"]').should('be.visible') |
| 350 | + |
| 351 | + cy.contains('Close the Modal') |
| 352 | + .realPress('Space') |
| 353 | + .then(() => { |
| 354 | + cy.get('[role="dialog"]').should('not.exist') |
| 355 | + }) |
| 356 | + |
| 357 | + cy.contains('Open the Modal').should('be.focused') |
| 358 | + |
| 359 | + cy.contains('Open the Modal').realPress('Tab') |
| 360 | + |
| 361 | + cy.contains('Hello').should('be.focused') |
| 362 | + }) |
134 | 363 | }) |
0 commit comments