Skip to content

Commit

Permalink
feat: Remove Enzyme to IntentRedirect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Nov 15, 2024
1 parent b8da4f9 commit a173b9d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
22 changes: 0 additions & 22 deletions test/components/intents/__snapshots__/intentRedirect.spec.js.snap

This file was deleted.

84 changes: 65 additions & 19 deletions test/components/intents/intentRedirect.spec.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,79 @@
'use strict'

/* eslint-env jest */

import '@testing-library/jest-dom'
import { render } from '@testing-library/react'
import { IntentRedirect } from 'ducks/components/intents/IntentRedirect'
import { shallow } from 'enzyme'
import React from 'react'
import { MemoryRouter, Route, Routes, Navigate } from 'react-router-dom'
import { useLocation } from 'react-router-dom'

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn()
useLocation: jest.fn(),
Navigate: jest.fn()
}))

describe('IntentRedirect component', () => {
it('should be rendered correctly if url search provided', () => {
useLocation.mockReturnValue({ search: '?doctype=io.cozy.apps' })
const component = shallow(<IntentRedirect />).getElement()
expect(component).toMatchSnapshot()
describe('IntentRedirect', () => {
const renderWithRouter = () => {
return render(
<MemoryRouter>
<Routes>
<Route path="*" element={<IntentRedirect />} />
</Routes>
</MemoryRouter>
)
}
Navigate.mockImplementation(({ to }) => <a data-testid="Navigate" to={to} />)

it('should redirect to install page if step is install', () => {
useLocation.mockReturnValue({ search: '?slug=test-app&step=install' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute('to', '/discover/test-app/install')
})

it('should be rendered correctly if search for slug', () => {
useLocation.mockReturnValue({ search: '?slug=mock' })
const component = shallow(<IntentRedirect />).getElement()
expect(component).toMatchSnapshot()
it('should redirect to install page if step is update', () => {
useLocation.mockReturnValue({ search: '?slug=test-app&step=update' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute('to', '/discover/test-app/install')
})

it('should redirect to uninstall page if step is uninstall', () => {
useLocation.mockReturnValue({ search: '?slug=test-app&step=uninstall' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute(
'to',
'/discover/test-app/uninstall'
)
})

it('should handle search param without value', () => {
useLocation.mockReturnValue({ search: '?slug=mock&doctype' })
const component = shallow(<IntentRedirect />).getElement()
expect(component).toMatchSnapshot()
it('should redirect to permissions page if step is permissions', () => {
useLocation.mockReturnValue({ search: '?slug=test-app&step=permissions' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute(
'to',
'/discover/test-app/permissions'
)
})

it('should redirect to app page if step is not provided', () => {
useLocation.mockReturnValue({ search: '?slug=test-app' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute('to', '/discover/test-app')
})

it('should redirect to discover page if slug is not provided', () => {
useLocation.mockReturnValue({ search: '' })
const { getByTestId } = renderWithRouter()

const navigateElement = getByTestId('Navigate')
expect(navigateElement).toHaveAttribute('to', '/discover/')
})
})

0 comments on commit a173b9d

Please sign in to comment.