diff --git a/web/app/stores/billing.test.ts b/web/app/stores/billing.test.ts new file mode 100644 index 000000000..f17e82984 --- /dev/null +++ b/web/app/stores/billing.test.ts @@ -0,0 +1,43 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { createPinia, setActivePinia } from 'pinia' +import { ref } from 'vue' +import { useBillingStore } from './billing' + +function setupStore( + apiFetch: (path: string, options: { method: string }) => Promise, +) { + Object.assign(globalThis, { + ref, + useApi: () => ({ apiFetch }), + useNotificationsStore: () => ({ addNotification: () => {} }), + }) + setActivePinia(createPinia()) + return useBillingStore() +} + +test('subscription cancellation succeeds with an empty 204 response', async () => { + const calls: { path: string; method: string }[] = [] + const store = setupStore(async (path, options) => { + calls.push({ path, method: options.method }) + }) + + await assert.doesNotReject(() => store.cancelSubscription()) + assert.deepEqual(calls, [ + { path: '/v1/users/subscription', method: 'DELETE' }, + ]) +}) + +test('subscription cancellation propagates API failures', async () => { + const error = Object.assign(new Error('Cancellation failed'), { + statusCode: 500, + }) + const store = setupStore(async () => { + throw error + }) + + await assert.rejects( + () => store.cancelSubscription(), + (caught) => caught === error, + ) +}) diff --git a/web/app/stores/billing.ts b/web/app/stores/billing.ts index 6d5db82e5..5d75f1061 100644 --- a/web/app/stores/billing.ts +++ b/web/app/stores/billing.ts @@ -43,14 +43,10 @@ export const useBillingStore = defineStore('billing', () => { return response.data } - async function cancelSubscription(): Promise { - const response = await apiFetch<{ message: string }>( - '/v1/users/subscription', - { - method: 'DELETE', - }, - ) - return response.message + async function cancelSubscription(): Promise { + await apiFetch('/v1/users/subscription', { + method: 'DELETE', + }) } async function indexSubscriptionPayments(): Promise {