{!isPending && location?.now?.city ? (
-
-
-
- {location.now.city} Now
-
- {location?.next?.city && (
- <>
- {isDifferentCountry && (
-
- )}
- {location.next.city}{' '}
-
- {relativeTime.from(new Date(location.next.date_start))}
-
- >
- )}
-
-
-
+
+
+ {location.now.city} Now
+
+ {location?.next?.city && (
+ <>
+ {isDifferentCountry && (
+
+ )}
+ {location.next.city}{' '}
+
+ {relativeTime.from(new Date(location.next.date_start))}
+
+ >
+ )}
+
+
) : null}
)
diff --git a/src/lib/getRepos.test.ts b/src/lib/getRepos.test.ts
new file mode 100644
index 0000000..b1d8ebc
--- /dev/null
+++ b/src/lib/getRepos.test.ts
@@ -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)
+ })
+})
diff --git a/tests/jest.setup.ts b/tests/jest.setup.ts
index b542ce5..20a989d 100644
--- a/tests/jest.setup.ts
+++ b/tests/jest.setup.ts
@@ -3,7 +3,6 @@ import '@testing-library/jest-dom'
import fetchMock from 'jest-fetch-mock'
import giphyMock from './__fixtures__/giphy.json'
import { dataLocation } from './__fixtures__/location'
-import reposMock from './__fixtures__/repos.json'
import './__mocks__/matchMedia'
fetchMock.enableMocks()
@@ -23,10 +22,6 @@ jest.mock('../src/lib/getRandomGif', () => ({
.mockImplementation(() => giphyMock.data.images.original.mp4)
}))
-jest.mock('../src/lib/getRepos', () => ({
- getRepos: jest.fn().mockImplementation(() => reposMock)
-}))
-
const unmockedFetch = global.fetch
const unmockedEnv = process.env