test(axios): refactor axios functional tests for improved clarity
Refactored the axios functional tests by removing unnecessary asynchronous imports and simplifying the setup in the `beforeEach` hook. This change enhances the readability and maintainability of the test code, ensuring it aligns with the project's coding standards.
This commit is contained in:
parent
c0428bb65d
commit
3f072976fe
@ -1,6 +1,8 @@
|
|||||||
import axios, { AxiosError, type InternalAxiosRequestConfig } from 'axios'
|
import axios, { AxiosError, type InternalAxiosRequestConfig } from 'axios'
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
|
import axiosInstance, { getOrRefreshAccessToken, resetAxiosAuthModuleState } from '@/config/axios'
|
||||||
|
|
||||||
const mocks = vi.hoisted(() => ({
|
const mocks = vi.hoisted(() => ({
|
||||||
expireRefreshSession: vi.fn().mockResolvedValue(undefined),
|
expireRefreshSession: vi.fn().mockResolvedValue(undefined),
|
||||||
}))
|
}))
|
||||||
@ -23,18 +25,15 @@ const writeStoredToken = (accessToken: string): void => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('admin access-token refresh races', () => {
|
describe('admin access-token refresh races', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
vi.restoreAllMocks()
|
vi.restoreAllMocks()
|
||||||
window.localStorage.clear()
|
window.localStorage.clear()
|
||||||
const { resetAxiosAuthModuleState } = await import('@/config/axios')
|
|
||||||
|
|
||||||
resetAxiosAuthModuleState()
|
resetAxiosAuthModuleState()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('replays a late 401 with the newer stored token without rotating refresh again', async () => {
|
it('replays a late 401 with the newer stored token without rotating refresh again', async () => {
|
||||||
writeStoredToken('access-a')
|
writeStoredToken('access-a')
|
||||||
const { default: axiosInstance } = await import('@/config/axios')
|
|
||||||
const refreshRequest = vi.spyOn(axios, 'post')
|
const refreshRequest = vi.spyOn(axios, 'post')
|
||||||
const authorizationHeaders: string[] = []
|
const authorizationHeaders: string[] = []
|
||||||
let requestCount = 0
|
let requestCount = 0
|
||||||
@ -85,7 +84,6 @@ describe('admin access-token refresh races', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { getOrRefreshAccessToken } = await import('@/config/axios')
|
|
||||||
const refreshRequest = vi.spyOn(axios, 'post')
|
const refreshRequest = vi.spyOn(axios, 'post')
|
||||||
|
|
||||||
await expect(getOrRefreshAccessToken()).resolves.toBe('access-b')
|
await expect(getOrRefreshAccessToken()).resolves.toBe('access-b')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user