From 406be7c51888cf8067eb9ceb30dfd340d7eef51b Mon Sep 17 00:00:00 2001 From: "chihao.ou" Date: Thu, 17 Sep 2026 14:59:43 +0800 Subject: [PATCH] fix(subscriptions): reset loading when clearing state --- .../__tests__/subscriptions.clear.spec.ts | 56 +++++++++++++++++++ frontend/src/stores/subscriptions.ts | 1 + 2 files changed, 57 insertions(+) create mode 100644 frontend/src/stores/__tests__/subscriptions.clear.spec.ts diff --git a/frontend/src/stores/__tests__/subscriptions.clear.spec.ts b/frontend/src/stores/__tests__/subscriptions.clear.spec.ts new file mode 100644 index 0000000000..5951d39490 --- /dev/null +++ b/frontend/src/stores/__tests__/subscriptions.clear.spec.ts @@ -0,0 +1,56 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createPinia, setActivePinia } from 'pinia' +import { useSubscriptionStore } from '../subscriptions' +import type { UserSubscription } from '@/types' + +const getActive = vi.hoisted(() => vi.fn()) +vi.mock('@/api/subscriptions', () => ({ default: { getActiveSubscriptions: getActive } })) + +function deferred() { + let resolve!: (value: UserSubscription[]) => void + let reject!: (error: Error) => void + const promise = new Promise((yes, no) => { resolve = yes; reject = no }) + return { promise, resolve, reject } +} + +beforeEach(() => { + setActivePinia(createPinia()) + getActive.mockReset() + vi.spyOn(console, 'error').mockImplementation(() => {}) +}) +afterEach(() => vi.restoreAllMocks()) + +describe('clearing in-flight subscriptions', () => { + it.each(['resolve', 'reject'] as const)('resets loading immediately and after the old request %s', async (outcome) => { + const pending = deferred() + getActive.mockReturnValueOnce(pending.promise) + const store = useSubscriptionStore() + const request = store.fetchActiveSubscriptions().catch(() => []) + expect(store.loading).toBe(true) + store.clear() + expect(store.loading).toBe(false) + if (outcome === 'resolve') pending.resolve([{ id: 1 } as UserSubscription]) + else pending.reject(new Error('offline')) + await request + expect(store.loading).toBe(false) + expect(store.activeSubscriptions).toEqual([]) + }) + + it('keeps a new request loading when an older cleared request finishes', async () => { + const older = deferred() + const newer = deferred() + getActive.mockReturnValueOnce(older.promise).mockReturnValueOnce(newer.promise) + const store = useSubscriptionStore() + const first = store.fetchActiveSubscriptions() + store.clear() + const second = store.fetchActiveSubscriptions() + older.resolve([{ id: 1 } as UserSubscription]) + await first + expect(store.loading).toBe(true) + expect(store.activeSubscriptions).toEqual([]) + newer.resolve([{ id: 2 } as UserSubscription]) + await second + expect(store.loading).toBe(false) + expect(store.activeSubscriptions.map(item => item.id)).toEqual([2]) + }) +}) diff --git a/frontend/src/stores/subscriptions.ts b/frontend/src/stores/subscriptions.ts index 5896591454..8a478323db 100644 --- a/frontend/src/stores/subscriptions.ts +++ b/frontend/src/stores/subscriptions.ts @@ -111,6 +111,7 @@ export const useSubscriptionStore = defineStore('subscriptions', () => { function clear() { requestGeneration++ activePromise = null + loading.value = false activeSubscriptions.value = [] loaded.value = false lastFetchedAt.value = null