-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests on fetcher & axios errors handling
- Loading branch information
Showing
3 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters