test: extract mock response helpers

Extract mockSuccessResponse and mockErrorResponse helpers to reduce duplication in test code
This commit is contained in:
bymyself
2026-01-26 12:13:10 -08:00
parent 2c6f3028f5
commit 4cb555ab18

View File

@@ -16,6 +16,21 @@ vi.stubGlobal('fetch', vi.fn())
describe('refreshRemoteConfig', () => {
const mockConfig = { feature1: true, feature2: 'value' }
function mockSuccessResponse(config = mockConfig) {
return {
ok: true,
json: async () => config
} as Response
}
function mockErrorResponse(status: number, statusText: string) {
return {
ok: false,
status,
statusText
} as Response
}
beforeEach(() => {
vi.clearAllMocks()
remoteConfig.value = {}
@@ -24,10 +39,7 @@ describe('refreshRemoteConfig', () => {
describe('with auth (default)', () => {
it('uses api.fetchApi when useAuth is true', async () => {
vi.mocked(api.fetchApi).mockResolvedValue({
ok: true,
json: async () => mockConfig
} as Response)
vi.mocked(api.fetchApi).mockResolvedValue(mockSuccessResponse())
await refreshRemoteConfig({ useAuth: true })
@@ -40,10 +52,7 @@ describe('refreshRemoteConfig', () => {
})
it('uses api.fetchApi by default', async () => {
vi.mocked(api.fetchApi).mockResolvedValue({
ok: true,
json: async () => mockConfig
} as Response)
vi.mocked(api.fetchApi).mockResolvedValue(mockSuccessResponse())
await refreshRemoteConfig()
@@ -54,10 +63,7 @@ describe('refreshRemoteConfig', () => {
describe('without auth', () => {
it('uses raw fetch when useAuth is false', async () => {
vi.mocked(global.fetch).mockResolvedValue({
ok: true,
json: async () => mockConfig
} as Response)
vi.mocked(global.fetch).mockResolvedValue(mockSuccessResponse())
await refreshRemoteConfig({ useAuth: false })
@@ -72,11 +78,9 @@ describe('refreshRemoteConfig', () => {
describe('error handling', () => {
it('clears config on 401 response', async () => {
vi.mocked(api.fetchApi).mockResolvedValue({
ok: false,
status: 401,
statusText: 'Unauthorized'
} as Response)
vi.mocked(api.fetchApi).mockResolvedValue(
mockErrorResponse(401, 'Unauthorized')
)
await refreshRemoteConfig()
@@ -85,11 +89,9 @@ describe('refreshRemoteConfig', () => {
})
it('clears config on 403 response', async () => {
vi.mocked(api.fetchApi).mockResolvedValue({
ok: false,
status: 403,
statusText: 'Forbidden'
} as Response)
vi.mocked(api.fetchApi).mockResolvedValue(
mockErrorResponse(403, 'Forbidden')
)
await refreshRemoteConfig()