Skip to content

Commit

Permalink
test: Migrate last enzyme tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Nov 7, 2024
1 parent 2f17975 commit b0cc85c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/components/Apps/ButtonCozyHome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ButtonCozyHome = ({ homeHref, isInvertedTheme }) => {
webviewIntent.call('backToHome')
}}
className="coz-nav-apps-btns-home coz-nav-apps-btns-home--is-flagship"
data-testid="buttonCozyHome"
>
<IconCozyHome
className="coz-nav-apps-btns-home-svg"
Expand Down
58 changes: 23 additions & 35 deletions src/components/Apps/ButtonCozyHome.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { shallow } from 'enzyme'
import { render, fireEvent } from '@testing-library/react'
import { ButtonCozyHome } from './ButtonCozyHome'
import { isFlagshipApp } from 'cozy-device-helper'
import { useWebviewIntent } from 'cozy-intent'
Expand All @@ -9,51 +9,39 @@ jest.mock('cozy-intent', () => ({
useWebviewIntent: jest.fn(() => ({ call: jest.fn() }))
}))

const homeHref = 'foo'
const expectedCall = 'backToHome'

describe('ButtonCozyHome', () => {
it('should render a span with no props', () => {
isFlagshipApp.mockImplementation(() => false)
const render = shallow(<ButtonCozyHome />)
const element = render.getElement()
it('renders a link with backToHome intent for flagship apps', () => {
isFlagshipApp.mockReturnValue(true)

expect(element.type).toBe('span')
})
const mockCall = jest.fn()
useWebviewIntent.mockImplementation(() => ({ call: mockCall }))

it('should render an anchor with correct href when homeHref', () => {
isFlagshipApp.mockImplementation(() => false)
const render = shallow(<ButtonCozyHome homeHref={homeHref} />)
const element = render.getElement()
// We need to use a testid because
// - the a tag has no text to query it (just an icon)
// - an "a" tag without href is not seen as a link by queryByRole
const { getByTestId } = render(<ButtonCozyHome />)
const linkElement = getByTestId('buttonCozyHome')

expect(element.type).toBe('a')
expect(element.props.href).toBe(homeHref)
})
fireEvent.click(linkElement)

it('should render an anchor when isFlagshipApp', () => {
isFlagshipApp.mockImplementation(() => true)
const render = shallow(<ButtonCozyHome />)
const element = render.getElement()

expect(element.type).toBe('a')
expect(mockCall).toHaveBeenCalledWith('backToHome')
})

it('should give priority to anchor if both isFlagshipApp and homeHref are present', () => {
isFlagshipApp.mockImplementation(() => true)
const render = shallow(<ButtonCozyHome homeHref={homeHref} />)
const element = render.getElement()
it('renders a link with homeHref for non-flagship apps with homeHref', () => {
isFlagshipApp.mockReturnValue(false)

const { queryByRole } = render(<ButtonCozyHome homeHref="/home" />)
const linkElement = queryByRole('link')

expect(element.type).toBe('a')
expect(linkElement).toHaveAttribute('href', '/home')
})

it('should call the correct context method on click', () => {
isFlagshipApp.mockImplementation(() => true)
const mockCall = jest.fn()
useWebviewIntent.mockImplementation(() => ({ call: mockCall }))
const render = shallow(<ButtonCozyHome homeHref={homeHref} />)
it('renders a span without href for non-flagship apps without homeHref', () => {
isFlagshipApp.mockReturnValue(false)

render.simulate('click')
const { queryByRole } = render(<ButtonCozyHome />)
const linkElement = queryByRole('link')

expect(mockCall).toBeCalledWith(expectedCall)
expect(linkElement).toBeNull()
})
})

0 comments on commit b0cc85c

Please sign in to comment.