Skip to content

Commit

Permalink
test: create tests index page
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielduete committed Jul 21, 2024
1 parent 6860eea commit c8ea85b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
28 changes: 28 additions & 0 deletions pages/index.page.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { fireEvent, render, screen } from '@testing-library/react'

import Home from './index.page'

describe('<Home />', () => {
beforeAll(() => {
window.Element.prototype.animate = jest.fn()
})

it('should render the title and explore link', () => {
render(<Home />)

expect(screen.getAllByText('sandevistan')[0]).toBeInTheDocument()
expect(screen.getByText('explore')).toBeInTheDocument()
})

it('should navigate to the about page when explore link is clicked', () => {
render(<Home />)
window.open = jest.fn()

const exploreLink = screen.getByText('explore')

fireEvent.click(exploreLink)

expect(window.open).toHaveBeenCalledTimes(1)
expect(window.open).toHaveBeenCalledWith('/about', '_self', 'noreferrer')
})
})
2 changes: 1 addition & 1 deletion src/components/ErrorCase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ErrorCaseProps = {

const ErrorCase = ({ hasMargin, onClick }: ErrorCaseProps) => {
return (
<S.Container hasMargin={hasMargin} data-testid='layout__error'>
<S.Container hasMargin={hasMargin} data-testid='error-case__id'>
<ErrorIcon fontSize='large' />
<S.Text>Something went wrong. Please try again.</S.Text>
<S.RetryButton onClick={onClick}>Retry</S.RetryButton>
Expand Down
4 changes: 2 additions & 2 deletions src/components/SkeletonText/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const SkeletonText = () => {
const skeletons = [160, 120, 70, 50]

return (
<>
<div data-testid='skeleton__loading'>
{skeletons.map((height, index) => (
<Skeleton key={index} height={height} animation='wave' />
))}
</>
</div>
)
}

Expand Down
9 changes: 7 additions & 2 deletions src/layout/layout.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ describe('<Layout />', () => {
})

it('should render loading case correctly', async () => {
fetchMock.mockResponse(() => new Promise(() => {}))
fetchMock.mockResponse(
() =>
new Promise(() => {
// @NOTE: this is a promise that never resolves, to mock loading case
})
)

render(
<PagesStoregedProvider>
Expand All @@ -42,7 +47,7 @@ describe('<Layout />', () => {
)

await waitFor(() => {
expect(screen.getByTestId(`${id}__error`)).toBeInTheDocument()
expect(screen.getByTestId('error-case__id')).toBeInTheDocument()
})
})
})

0 comments on commit c8ea85b

Please sign in to comment.