Skip to content

Commit

Permalink
test: add tests on fetcher & axios errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
QRuhier committed Dec 12, 2024
1 parent b676514 commit 4ac54e8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
67 changes: 67 additions & 0 deletions src/core/tools/axiosError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, it, expect, vi } from 'vitest'
import { handleAxiosError } from './axiosError'
import { AxiosError } from 'axios'

vi.mock('i18n', () => ({
getTranslation: () => ({ t: (keyMessage: string) => keyMessage }),
}))

describe('handleAxiosError', () => {
it('should return a custom message for no response', () => {
const error: AxiosError = {
message: '',
name: '',
config: {},
isAxiosError: true,
toJSON: () => ({}),
response: undefined,
} as AxiosError

const result = handleAxiosError(error)
expect(result.message).toBe(
"Une erreur s'est produite lors du traitement de la requête. Veuillez réessayer plus tard."
)
})

it('should return the correct message for status 400', () => {
const error: AxiosError = {
response: { status: 400 } as any,
message: '',
name: '',
config: {},
isAxiosError: true,
toJSON: () => ({}),
} as AxiosError

const result = handleAxiosError(error)
expect(result.message).toBe('400')
})

it('should return the correct message for status 401', () => {
const error: AxiosError = {
response: { status: 401 } as any,
message: '',
name: '',
config: {},
isAxiosError: true,
toJSON: () => ({}),
} as AxiosError

const result = handleAxiosError(error)
expect(result.message).toBe('401')
})

it('should return the correct message for unknown status', () => {
const error: AxiosError = {
response: { status: 999 } as any,
message: '',
name: '',
config: {},
isAxiosError: true,
toJSON: () => ({}),
} as AxiosError

const result = handleAxiosError(error)
expect(result.message).toBe('longUnknownError')
})
})
35 changes: 35 additions & 0 deletions src/core/tools/fetchUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { vi, describe, beforeEach, it, expect } from 'vitest'
import axios from 'axios'
import { fetchUrl } from './fetchUrl'

vi.mock('axios')

describe('fetchUrl', () => {
beforeEach(() => {
vi.clearAllMocks()
})

it('should fetch data from the provided URL and return the response data', async () => {
const mockUrl = 'https://example.com/api/data'
const mockResponseData = { key: 'value' }

vi.mocked(axios.get).mockResolvedValueOnce({ data: mockResponseData })

const result = await fetchUrl<{ key: string }>({ url: mockUrl })

expect(axios.get).toHaveBeenCalledWith(decodeURIComponent(mockUrl))

expect(result).toEqual(mockResponseData)
})

it('should throw an error if the request fails', async () => {
const mockUrl = 'https://example.com/api/error'
const mockError = new Error('Request failed')

vi.mocked(axios.get).mockRejectedValueOnce(mockError)

await expect(fetchUrl({ url: mockUrl })).rejects.toThrow(mockError)

expect(axios.get).toHaveBeenCalledWith(decodeURIComponent(mockUrl))
})
})
5 changes: 0 additions & 5 deletions src/core/tools/fetchUrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import axios from 'axios'

/**
*
* @param params: { url : string}
* @returns {Promise<T>} fetched data of type T.
*/
export async function fetchUrl<T>(params: { url: string }): Promise<T> {
const { url } = params
return axios.get<T>(decodeURIComponent(url)).then(({ data }) => data)
Expand Down

0 comments on commit 4ac54e8

Please sign in to comment.