1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-22 09:13:19 +01:00

more unit tests

This commit is contained in:
Matthias Kretschmann 2024-02-06 01:29:33 +00:00
parent b7e03a1dbc
commit 408d9f96f3
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 106 additions and 40 deletions

View File

@ -1,6 +1,11 @@
import { render, screen } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import reposMock from '../../../tests/__fixtures__/repos.json'
import Page from '../page' import Page from '../page'
jest.mock('../../lib/getRepos', () => ({
getRepos: jest.fn().mockImplementation(() => reposMock)
}))
describe('app: /page', () => { describe('app: /page', () => {
it('renders correctly', async () => { it('renders correctly', async () => {
render(await Page()) render(await Page())

View File

@ -9,8 +9,24 @@ import { Flag } from './Flag'
import styles from './Location.module.css' import styles from './Location.module.css'
import { UseLocation } from './types' import { UseLocation } from './types'
export default function Location() { function Animation({ children }: { children: React.ReactNode }) {
const shouldReduceMotion = useReducedMotion() const shouldReduceMotion = useReducedMotion()
return (
<LazyMotion features={domAnimation}>
<m.section
aria-label="Location"
variants={fadeIn}
className={styles.location}
{...getAnimationProps(shouldReduceMotion)}
>
{children}
</m.section>
</LazyMotion>
)
}
export default function Location() {
const [isPending, startTransition] = useTransition() const [isPending, startTransition] = useTransition()
const [location, setLocation] = useState<UseLocation | null>(null) const [location, setLocation] = useState<UseLocation | null>(null)
@ -27,13 +43,7 @@ export default function Location() {
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
{!isPending && location?.now?.city ? ( {!isPending && location?.now?.city ? (
<LazyMotion features={domAnimation}> <Animation>
<m.section
aria-label="Location"
variants={fadeIn}
className={styles.location}
{...getAnimationProps(shouldReduceMotion)}
>
<Flag <Flag
country={{ country={{
code: location.now.country_code, code: location.now.country_code,
@ -59,8 +69,7 @@ export default function Location() {
</> </>
)} )}
</div> </div>
</m.section> </Animation>
</LazyMotion>
) : null} ) : null}
</div> </div>
) )

57
src/lib/getRepos.test.ts Normal file
View File

@ -0,0 +1,57 @@
import * as React from 'react'
import fetch, { FetchMock } from 'jest-fetch-mock'
import repoFilter from '@content/repos.json'
import { getRepos } from './getRepos'
jest.mock('react', () => ({
...jest.requireActual('react'),
cache: (fn) => fn
}))
describe('getRepos', () => {
beforeEach(() => {
fetch.resetMocks()
})
it('should fetch repos data', async () => {
const mockData = {
name: 'test',
full_name: 'test/test',
description: 'test repo',
html_url: 'https://github.com/test/test',
homepage: 'https://test.com',
stargazers_count: 100,
pushed_at: '2022-01-01T00:00:00Z'
}
;(fetch as FetchMock).mockResponse(JSON.stringify(mockData))
const data = await getRepos()
const count = repoFilter.length
expect(data).toEqual(Array.from({ length: count }, () => mockData))
expect(fetch).toHaveBeenCalledTimes(count)
})
it('should handle network errors', async () => {
let consoleErrorMock: jest.SpyInstance
consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {})
;(fetch as FetchMock).mockRejectOnce(new Error('Network error'))
const data = await getRepos()
expect(data).toBeUndefined()
expect(fetch).toHaveBeenCalledTimes(1)
consoleErrorMock.mockRestore()
})
it('should handle invalid repo data', async () => {
const mockData = { name: null }
;(fetch as FetchMock).mockResponseOnce(JSON.stringify(mockData))
const data = await getRepos()
expect(data).toBeUndefined()
expect(fetch).toHaveBeenCalledTimes(1)
})
})

View File

@ -3,7 +3,6 @@ import '@testing-library/jest-dom'
import fetchMock from 'jest-fetch-mock' import fetchMock from 'jest-fetch-mock'
import giphyMock from './__fixtures__/giphy.json' import giphyMock from './__fixtures__/giphy.json'
import { dataLocation } from './__fixtures__/location' import { dataLocation } from './__fixtures__/location'
import reposMock from './__fixtures__/repos.json'
import './__mocks__/matchMedia' import './__mocks__/matchMedia'
fetchMock.enableMocks() fetchMock.enableMocks()
@ -23,10 +22,6 @@ jest.mock('../src/lib/getRandomGif', () => ({
.mockImplementation(() => giphyMock.data.images.original.mp4) .mockImplementation(() => giphyMock.data.images.original.mp4)
})) }))
jest.mock('../src/lib/getRepos', () => ({
getRepos: jest.fn().mockImplementation(() => reposMock)
}))
const unmockedFetch = global.fetch const unmockedFetch = global.fetch
const unmockedEnv = process.env const unmockedEnv = process.env