Skip to content

Commit

Permalink
Merge pull request #54 from gabrielduete/feat/resolve-todo-tests
Browse files Browse the repository at this point in the history
feat; resolve todo tests
  • Loading branch information
gabrielduete authored Jul 22, 2024
2 parents 892a9e7 + c8ea85b commit 5f19771
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 17 deletions.
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const createJestConfig = nextJest({
})

const customJestConfig = {
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
setupFilesAfterEnv: [
'@testing-library/jest-dom/extend-expect',
'<rootDir>/jest.setup.js',
],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
moduleNameMapper: {
Expand Down
3 changes: 3 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fetchMock from 'jest-fetch-mock'

fetchMock.enableMocks()
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@mui/material": "^5.14.11",
"@types/react-sound": "^1.2.4",
"axios": "^1.5.1",
"jest-fetch-mock": "^3.0.3",
"next": "12.2.5",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
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}>
<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
2 changes: 1 addition & 1 deletion src/layout/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as S from './styles'

const Footer = () => {
return (
<S.Footer>
<S.Footer data-testid='layout__footer'>
{items.map(({ name, href }, idx) => {
return (
<S.Link href={href} target='_blank' key={idx}>
Expand Down
2 changes: 1 addition & 1 deletion src/layout/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Header = ({ pages }: NavBarProps) => {
const router = useRouter()

return (
<S.Header>
<S.Header data-testid='layout__header'>
<S.WrapperLinks>
{items.map(({ name, href }) => {
const isPath = router?.pathname === href
Expand Down
16 changes: 13 additions & 3 deletions src/layout/components/NavBar/Desktop/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
describe('<MobileNavBar />', () => {
it.todo('render texts correctly')
import { render, screen } from '@testing-library/react'
import { pagesMock } from '~/src/mocks/pagesMock'

it.todo('toggles the NavBar when BackIcon is clicked')
import DesktopNavBar from '.'

describe('<DesktopNavBar />', () => {
beforeEach(() => {
render(<DesktopNavBar pages={pagesMock} />)
})

it('render texts correctly', () => {
expect(screen.getByText('Git e Versionamento')).toBeInTheDocument()
expect(screen.getByText('My list git commands')).toBeInTheDocument()
})
})
5 changes: 4 additions & 1 deletion src/layout/components/NavBar/Desktop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ const DesktopNavBar = ({ pages }: NavBarProps) => {
))}
</S.NavBar>
<S.RollbackContainer>
<S.BackIcon onClick={closeNavBar} />
<S.BackIcon
onClick={closeNavBar}
data-testid='navbar__button-close'
/>
</S.RollbackContainer>
</S.Wrapper>
<S.WrapperColapsed showNavBar={!isOpen}>
Expand Down
14 changes: 12 additions & 2 deletions src/layout/components/NavBar/Mobile/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { render, screen } from '@testing-library/react'
import { pagesMock } from '~/src/mocks/pagesMock'

import MobileNavBar from '.'

describe('<MobileNavBar />', () => {
it.todo('render texts correctly')
beforeEach(() => {
render(<MobileNavBar pages={pagesMock} />)
})

it.todo('toggles the NavBar when BackIcon is clicked')
it('render texts correctly', () => {
expect(screen.getByText('Git e Versionamento')).toBeInTheDocument()
expect(screen.getByText('My list git commands')).toBeInTheDocument()
})
})
5 changes: 3 additions & 2 deletions src/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const Layout = ({ children }: LayoutProps) => {

const LayoutBaseMemorized = memo(LayoutBase)

if (isLoading) return <S.Loading color='inherit' />
if (isLoading)
return <S.Loading color='inherit' data-testid='layout__loader' />

if (hasError)
return (
Expand All @@ -46,7 +47,7 @@ const Layout = ({ children }: LayoutProps) => {
const MemorizedContent = memo(() => (
<LayoutBaseMemorized>
<S.WrapperContent>
<DesktopNavbar pages={pages} />
<DesktopNavbar pages={pages} data-testid='layout__navbar' />
<S.Content>
<Breadcrumb />
{children}
Expand Down
44 changes: 41 additions & 3 deletions src/layout/layout.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
import { render, screen } from '@testing-library/react'
import { render, screen, waitFor } from '@testing-library/react'
import fetchMock from 'jest-fetch-mock'

import Layout from '.'
import { PagesStoregedProvider } from '../contexts/ContextPages'

describe('<Layout />', () => {
it('should render children Layout correctly', () => {
const id = 'layout'

it('should render children and components correctly', () => {
render(<Layout>Layout</Layout>)

const children = screen.getByText('Layout')

expect(children).toBeInTheDocument()
expect(screen.getByTestId(`${id}__header`)).toBeInTheDocument()
expect(screen.queryByTestId(`${id}__navbar`)).not.toBeInTheDocument()
expect(screen.getByTestId(`${id}__footer`)).toBeInTheDocument()
})

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

render(
<PagesStoregedProvider>
<Layout>Layout</Layout>
</PagesStoregedProvider>
)

await waitFor(() => {
expect(screen.getByTestId(`${id}__loader`)).toBeInTheDocument()
})
})

it.todo('should render Header, Footer and NavBar components correctly')
it('should render error case correctly', async () => {
fetchMock.mockReject(new Error('error bip bop'))

render(
<PagesStoregedProvider>
<Layout>Layout</Layout>
</PagesStoregedProvider>
)

await waitFor(() => {
expect(screen.getByTestId('error-case__id')).toBeInTheDocument()
})
})
})
52 changes: 52 additions & 0 deletions src/mocks/pagesMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const pagesMock = [
{
object: 'block',
id: '1',
parent: {
type: 'page_id',
page_id: '1',
},
created_time: '2023-11-14T13:54:00.000Z',
last_edited_time: '2024-07-17T01:18:00.000Z',
created_by: {
object: 'user',
id: '1234',
},
last_edited_by: {
object: 'user',
id: '1234',
},
has_children: true,
archived: false,
in_trash: false,
type: 'child_page',
child_page: {
title: 'Git e Versionamento',
},
},
{
object: 'block',
id: '2',
parent: {
type: 'page_id',
page_id: '2',
},
created_time: '2023-11-15T04:37:00.000Z',
last_edited_time: '2024-07-17T01:18:00.000Z',
created_by: {
object: 'user',
id: '1234',
},
last_edited_by: {
object: 'user',
id: '1234',
},
has_children: true,
archived: false,
in_trash: false,
type: 'child_page',
child_page: {
title: 'My list git commands',
},
},
]
45 changes: 45 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4201,6 +4201,13 @@ cross-env@^7.0.3:
dependencies:
cross-spawn "^7.0.1"

cross-fetch@^3.0.4:
version "3.1.8"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.8.tgz#0327eba65fd68a7d119f8fb2bf9334a1a7956f82"
integrity sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==
dependencies:
node-fetch "^2.6.12"

cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
Expand Down Expand Up @@ -6565,6 +6572,14 @@ jest-environment-node@^29.0.1:
jest-mock "^29.0.1"
jest-util "^29.0.1"

jest-fetch-mock@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz#31749c456ae27b8919d69824f1c2bd85fe0a1f3b"
integrity sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==
dependencies:
cross-fetch "^3.0.4"
promise-polyfill "^8.1.3"

jest-get-type@^27.5.1:
version "27.5.1"
resolved "https://registry.npmjs.org/jest-get-type/-/jest-get-type-27.5.1.tgz"
Expand Down Expand Up @@ -7730,6 +7745,13 @@ no-case@^3.0.4:
lower-case "^2.0.2"
tslib "^2.0.3"

node-fetch@^2.6.12:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-forge@^1:
version "1.3.1"
resolved "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz"
Expand Down Expand Up @@ -8769,6 +8791,11 @@ process-nextick-args@~2.0.0:
resolved "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

promise-polyfill@^8.1.3:
version "8.3.0"
resolved "https://registry.yarnpkg.com/promise-polyfill/-/promise-polyfill-8.3.0.tgz#9284810268138d103807b11f4e23d5e945a4db63"
integrity sha512-H5oELycFml5yto/atYqmjyigJoAo3+OXwolYiH7OfQuYlAqhxNvTfiNMbV9hsC6Yp83yE5r2KTVmtrG6R9i6Pg==

promise@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/promise/-/promise-8.1.0.tgz"
Expand Down Expand Up @@ -10160,6 +10187,11 @@ tr46@^3.0.0:
dependencies:
punycode "^2.1.1"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

tryer@^1.0.1:
version "1.0.1"
resolved "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz"
Expand Down Expand Up @@ -10435,6 +10467,11 @@ wbuf@^1.1.0, wbuf@^1.7.3:
dependencies:
minimalistic-assert "^1.0.0"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==

webidl-conversions@^4.0.2:
version "4.0.2"
resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-4.0.2.tgz"
Expand Down Expand Up @@ -10611,6 +10648,14 @@ whatwg-url@^11.0.0:
tr46 "^3.0.0"
webidl-conversions "^7.0.0"

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

whatwg-url@^7.0.0:
version "7.1.0"
resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-7.1.0.tgz"
Expand Down

0 comments on commit 5f19771

Please sign in to comment.