tooltip.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { useState } from 'react'
  2. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  3. describe('<OLTooltip />', function () {
  4. it('calls the bound handler and blur then hides text on click', function () {
  5. const clickHandler = cy.stub().as('clickHandler')
  6. const blurHandler = cy.stub().as('blurHandler')
  7. const description = 'foo'
  8. const btnText = 'Click me!'
  9. cy.mount(
  10. <div
  11. style={{
  12. display: 'flex',
  13. alignItems: 'center',
  14. justifyContent: 'center',
  15. height: '100vh',
  16. }}
  17. >
  18. <OLTooltip id="abc" description={description}>
  19. <button onClick={clickHandler} onBlur={blurHandler}>
  20. {btnText}
  21. </button>
  22. </OLTooltip>
  23. </div>
  24. )
  25. cy.findByRole('button', { name: btnText }).as('button')
  26. cy.get('@button').trigger('mouseover')
  27. cy.findByText(description)
  28. cy.get('@button').click()
  29. cy.get('@clickHandler').should('have.been.calledOnce')
  30. cy.get('@blurHandler').should('have.been.calledOnce')
  31. cy.findByText(description).should('not.exist')
  32. })
  33. it('hides the tooltip when Escape key is pressed', function () {
  34. const description = 'Press Escape to close'
  35. const btnText = 'Hover me!'
  36. cy.mount(
  37. <div
  38. style={{
  39. display: 'flex',
  40. alignItems: 'center',
  41. justifyContent: 'center',
  42. height: '100vh',
  43. }}
  44. >
  45. <OLTooltip id="esc" description={description}>
  46. <button>{btnText}</button>
  47. </OLTooltip>
  48. </div>
  49. )
  50. cy.findByRole('button', { name: btnText }).as('button')
  51. cy.get('@button').trigger('mouseover')
  52. cy.findByText(description)
  53. cy.get('@button').trigger('mouseout')
  54. cy.get('@button').focus()
  55. cy.findByText(description)
  56. cy.get('body').type('{esc}')
  57. cy.findByText(description).should('not.exist')
  58. })
  59. it('does not re-show the tooltip when the hidden prop transitions from true to false', function () {
  60. const description = 'tooltip content'
  61. function Wrapper() {
  62. const [hidden, setHidden] = useState(false)
  63. return (
  64. <OLTooltip id="abc" description={description} hidden={hidden}>
  65. <button onClick={() => setHidden(h => !h)}>button</button>
  66. </OLTooltip>
  67. )
  68. }
  69. cy.mount(
  70. <div
  71. style={{
  72. display: 'flex',
  73. alignItems: 'center',
  74. justifyContent: 'center',
  75. height: '100vh',
  76. }}
  77. >
  78. <Wrapper />
  79. </div>
  80. )
  81. cy.findByRole('button', { name: 'button' }).as('tooltip-btn')
  82. cy.get('@tooltip-btn').trigger('mouseover')
  83. cy.findByText(description)
  84. cy.get('@tooltip-btn').click()
  85. cy.findByText(description).should('not.exist')
  86. cy.get('@tooltip-btn').trigger('mouseout')
  87. cy.get('@tooltip-btn').click()
  88. cy.findByText(description).should('not.exist')
  89. cy.get('@tooltip-btn').trigger('mouseover')
  90. cy.findByText(description)
  91. })
  92. })