Skip to content

Commit

Permalink
test(ui-tooltip): migrate old Tooltip tests
Browse files Browse the repository at this point in the history
Closes: INSTUI-4076
  • Loading branch information
git-nandor authored and balzss committed Jun 6, 2024
1 parent 23efdfc commit b4b5bb3
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 266 deletions.
134 changes: 134 additions & 0 deletions cypress/component/Tooltip.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from 'react'
import 'cypress-real-events'

import { Tooltip } from '../../packages/ui'
import '../support/component'

describe('<Tooltip/>', () => {
it('should render the tip offscreen', async () => {
cy.mount(
<div>
<button>For dismiss</button>
<Tooltip renderTip="Hello">
<a data-testid="trigger" href="example.html">
Hover or focus me
</a>
</Tooltip>
</div>
)

cy.contains('Hello').should('not.be.visible')
cy.get('[data-testid="trigger"]').then(($trigger) => {
const offscreenPopoverId = $trigger.attr('data-position-target')
const offscreenPopover = `[data-position-content="${offscreenPopoverId}"]`

cy.get(offscreenPopover).should('have.text', 'Hello')
cy.get(offscreenPopover).should('have.css', 'display', 'none')
cy.get(offscreenPopover).should('have.css', 'left', '-159984px')
})

cy.get('[data-testid="trigger"]').realHover().wait(100)

cy.contains('Hello').should('be.visible')
cy.get('[data-testid="trigger"]').then(($trigger) => {
const popoverId = $trigger.attr('data-position-target')
const popover = `span[data-position-content="${popoverId}"]`

cy.get(popover).should('have.text', 'Hello')
cy.get(popover).should('have.css', 'display', 'block')
cy.get(popover).should('have.css', 'left', '0px')
})
})

it('should show tip by default when defaultIsShowingContent is true', async () => {
cy.mount(
<Tooltip renderTip="Hello" defaultIsShowingContent>
<a data-testid="trigger" href="example.html">
Hover or focus me
</a>
</Tooltip>
)

cy.contains('Hello').should('be.visible')

cy.get('[data-testid="trigger"]').then(($trigger) => {
const popoverId = $trigger.attr('data-position-target')
const popover = `span[data-position-content="${popoverId}"]`

cy.get(popover).should('have.text', 'Hello')
cy.get(popover).should('have.css', 'display', 'block')
cy.get(popover).should('have.css', 'left', '0px')
})
})

it('should show tip when isShowingContent is true', async () => {
cy.mount(
<Tooltip renderTip={<h2>Hello</h2>} isShowingContent>
<a data-testid="trigger" href="example.html">
Hover or focus me
</a>
</Tooltip>
)

cy.contains('Hello').should('be.visible')
cy.get('[data-testid="trigger"]').then(($trigger) => {
const popoverId = $trigger.attr('data-position-target')
const popover = `span[data-position-content="${popoverId}"]`

cy.get(popover).should('have.text', 'Hello')
cy.get(popover).should('have.css', 'display', 'block')
cy.get(popover).should('have.css', 'left', '0px')
})
})

it('should call onShowContent and on onHideContent', async () => {
const onShowContent = cy.spy()
const onHideContent = cy.spy()

cy.mount(
<div>
<button>For dismiss</button>
<Tooltip
renderTip={<h2>Hello</h2>}
onShowContent={onShowContent}
onHideContent={onHideContent}
>
<a data-testid="trigger" href="example.html">
Hover or focus me
</a>
</Tooltip>
</div>
)

cy.get('[data-testid="trigger"]').realHover()
cy.contains('Hello').should('be.visible')
cy.wrap(onShowContent).should('have.been.calledOnce')

cy.contains('button', 'For dismiss').realHover()
cy.contains('Hello').should('not.be.visible')
cy.wrap(onHideContent).should('have.been.calledOnce')
})
})
59 changes: 58 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion packages/ui-tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@
"prop-types": "^15.8.1"
},
"devDependencies": {
"@instructure/ui-axe-check": "9.0.1",
"@instructure/ui-babel-preset": "9.0.1",
"@instructure/ui-color-utils": "9.0.1",
"@instructure/ui-scripts": "9.0.1",
"@instructure/ui-test-locator": "9.0.1",
"@instructure/ui-test-queries": "9.0.1",
"@instructure/ui-test-utils": "9.0.1",
"@instructure/ui-themes": "9.0.1"
"@instructure/ui-themes": "9.0.1",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2"
},
"peerDependencies": {
"react": ">=16.8 <=18"
Expand Down
Loading

0 comments on commit b4b5bb3

Please sign in to comment.