Skip to content

Commit 0a06cca

Browse files
test: add purchaseCredits tests using canAccessSubscriptionFeatures
- Update mock from isActiveSubscription to canAccessSubscriptionFeatures - Add tests for purchaseCredits early return and successful flow Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1b7e873 commit 0a06cca

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

src/composables/auth/useAuthActions.test.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import type { ComfyWorkflow } from '@/platform/workflow/management/stores/workfl
77
type ModifiedWorkflow = Pick<ComfyWorkflow, 'path' | 'isModified'>
88

99
const mockAuthStore = vi.hoisted(() => ({
10-
logout: vi.fn().mockResolvedValue(undefined)
10+
logout: vi.fn().mockResolvedValue(undefined),
11+
initiateCreditPurchase: vi
12+
.fn()
13+
.mockResolvedValue({ checkout_url: 'https://checkout.example.com' })
1114
}))
1215

1316
const mockToastStore = vi.hoisted(() => ({
@@ -35,8 +38,12 @@ vi.mock('@/platform/distribution/types', () => ({
3538
isCloud: false
3639
}))
3740

41+
const mockStartTopupTracking = vi.hoisted(() => vi.fn())
42+
3843
vi.mock('@/platform/telemetry', () => ({
39-
useTelemetry: vi.fn(() => undefined)
44+
useTelemetry: vi.fn(() => ({
45+
startTopupTracking: mockStartTopupTracking
46+
}))
4047
}))
4148

4249
vi.mock('@/platform/updates/common/toastStore', () => ({
@@ -59,9 +66,13 @@ vi.mock('@/stores/authStore', () => ({
5966
useAuthStore: vi.fn(() => mockAuthStore)
6067
}))
6168

69+
const mockCanAccessSubscriptionFeatures = vi.hoisted(() => ({
70+
value: false
71+
}))
72+
6273
vi.mock('@/composables/billing/useBillingContext', () => ({
6374
useBillingContext: vi.fn(() => ({
64-
isActiveSubscription: { value: false },
75+
canAccessSubscriptionFeatures: mockCanAccessSubscriptionFeatures,
6576
isFreeTier: { value: true },
6677
type: { value: 'free' }
6778
}))
@@ -193,3 +204,40 @@ describe('useAuthActions.logout', () => {
193204
)
194205
})
195206
})
207+
208+
describe('useAuthActions.purchaseCredits', () => {
209+
beforeEach(() => {
210+
setActivePinia(createPinia())
211+
vi.clearAllMocks()
212+
mockCanAccessSubscriptionFeatures.value = false
213+
})
214+
215+
it('returns early when canAccessSubscriptionFeatures is false', async () => {
216+
mockCanAccessSubscriptionFeatures.value = false
217+
218+
const { purchaseCredits } = useAuthActions()
219+
await purchaseCredits(10)
220+
221+
expect(mockAuthStore.initiateCreditPurchase).not.toHaveBeenCalled()
222+
})
223+
224+
it('initiates credit purchase when canAccessSubscriptionFeatures is true', async () => {
225+
mockCanAccessSubscriptionFeatures.value = true
226+
const mockOpen = vi.spyOn(window, 'open').mockImplementation(() => null)
227+
228+
const { purchaseCredits } = useAuthActions()
229+
await purchaseCredits(10)
230+
231+
expect(mockAuthStore.initiateCreditPurchase).toHaveBeenCalledWith({
232+
amount_micros: 10_000_000,
233+
currency: 'usd'
234+
})
235+
expect(mockStartTopupTracking).toHaveBeenCalled()
236+
expect(mockOpen).toHaveBeenCalledWith(
237+
'https://checkout.example.com',
238+
'_blank'
239+
)
240+
241+
mockOpen.mockRestore()
242+
})
243+
})

0 commit comments

Comments
 (0)