Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .fallowrc.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"entry": [
"src/main.ts",
"src/platform/telemetry/__mocks__/index.ts",
"src/composables/billing/__mocks__/useBillingContext.ts",
"index.html",
"scripts/**/*.{js,ts}",
"apps/website/src/scripts/**/*.ts",
Expand Down
38 changes: 11 additions & 27 deletions src/components/dialog/content/setting/CreditsPanel.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import userEvent from '@testing-library/user-event'
import { render, screen } from '@testing-library/vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defineComponent, h, nextTick } from 'vue'
import { describe, expect, it, vi } from 'vitest'
import { computed, defineComponent, h, nextTick, ref } from 'vue'
import { createI18n } from 'vue-i18n'

import type { BalanceInfo } from '@/composables/billing/types'
import { mockBillingContext } from '@/utils/__tests__/mockBillingContext'

import CreditsPanel from './CreditsPanel.vue'

const billingMocks = vi.hoisted(() => ({
balance: { value: null as BalanceInfo | null },
manageSubscription: vi.fn()
}))
vi.mock<unknown>(
import('@/composables/billing/useBillingContext'),
async () => {
const { ref } = await import('vue')
const balance = ref<BalanceInfo | null>(null)
Object.defineProperty(billingMocks, 'balance', { get: () => balance })
return {
useBillingContext: () => ({
balance,
manageSubscription: billingMocks.manageSubscription
})
}
}
)
vi.mock(import('@/composables/billing/useBillingContext'))

const refreshActivity = vi.hoisted(() => vi.fn())
vi.mock<unknown>(import('./UsageLogsTable.vue'), async () => {
Expand Down Expand Up @@ -84,33 +68,33 @@ function makeBalance(amountMicros: number): BalanceInfo {
}

describe('CreditsPanel', () => {
beforeEach(() => {
billingMocks.balance.value = null
})

function renderComponent() {
return render(CreditsPanel, {
global: { plugins: [i18n], stubs: { Divider: true } }
})
}

it('opens the billing portal for the active billing rail', async () => {
const billing = mockBillingContext()
const user = userEvent.setup()
renderComponent()

await user.click(screen.getByRole('button', { name: /Invoice History/ }))

expect(billingMocks.manageSubscription).toHaveBeenCalledOnce()
expect(billing.manageSubscription).toHaveBeenCalledOnce()
})

it('refreshes activity on a balance change but not on first hydration', async () => {
const billing = mockBillingContext()
const balance = ref<BalanceInfo | null>(null)
billing.balance = computed(() => balance.value)
renderComponent()

billingMocks.balance.value = makeBalance(5000)
balance.value = makeBalance(5000)
await nextTick()
expect(refreshActivity).not.toHaveBeenCalled()

billingMocks.balance.value = makeBalance(9000)
balance.value = makeBalance(9000)
await nextTick()
expect(refreshActivity).toHaveBeenCalledOnce()
})
Expand Down
31 changes: 17 additions & 14 deletions src/components/topbar/CurrentUserPopoverLegacy.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { getActivePinia } from 'pinia'
import { render, screen } from '@testing-library/vue'
import userEvent from '@testing-library/user-event'
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { h, ref } from 'vue'
import { h, ref, computed } from 'vue'
import { createI18n } from 'vue-i18n'

import { formatCreditsFromCents } from '@/base/credits/comfyCredits'
Expand All @@ -25,6 +26,19 @@ vi.mock(import('@/platform/settings/composables/useSettingsDialog'), () => ({

const originalWindowOpen = window.open
beforeEach(() => {
const billing = useBillingContext()
Object.assign(billing, {
canAccessSubscriptionFeatures: computed(
() => mockCanAccessSubscriptionFeatures.value
),
tier: computed(() => mockTier.value),
subscription: computed(() => mockSubscription.value),
balance: computed(() => mockBalance.value),
isLoading: mockIsLoading,
isTeamPlan: computed(() => mockIsTeamPlan.value)
})
vi.mocked(useBillingContext).mockReturnValue(billing)

window.open = vi.fn()
})

Expand Down Expand Up @@ -65,7 +79,6 @@ function makeSubscription(
}
}

const mockFetchBalance = vi.fn().mockResolvedValue(undefined)
const mockCanAccessSubscriptionFeatures = ref(true)
const mockTier = ref<SubscriptionInfo['tier']>('CREATOR')
const mockSubscription = ref<SubscriptionInfo | null>(makeSubscription())
Expand All @@ -75,17 +88,7 @@ const mockIsTeamPlan = ref(false)
const mockCanTopUp = ref(true)
const mockCanSubscribeSelfServe = ref(false)

vi.mock<unknown>(import('@/composables/billing/useBillingContext'), () => ({
useBillingContext: vi.fn(() => ({
canAccessSubscriptionFeatures: mockCanAccessSubscriptionFeatures,
tier: mockTier,
subscription: mockSubscription,
balance: mockBalance,
isLoading: mockIsLoading,
isTeamPlan: mockIsTeamPlan,
fetchBalance: mockFetchBalance
}))
}))
vi.mock(import('@/composables/billing/useBillingContext'))

vi.mock<unknown>(
import('@/platform/workspace/composables/useBillingCapabilities'),
Expand Down Expand Up @@ -174,7 +177,7 @@ describe('CurrentUserPopoverLegacy', () => {
it('fetches the balance through the billing facade on mount', () => {
renderComponent()

expect(mockFetchBalance).toHaveBeenCalled()
expect(useBillingContext().fetchBalance).toHaveBeenCalled()
})

describe('subscription tier badge', () => {
Expand Down
53 changes: 15 additions & 38 deletions src/components/topbar/TopbarSubscribeButton.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { useSubscribeToRunPromptPresence } from '@/platform/cloud/subscription/composables/useSubscribeCtaPresence'
import { getActivePinia } from 'pinia'
import { nextTick } from 'vue'
import { describe, expect, it, vi } from 'vitest'
import { computed, nextTick, ref } from 'vue'
import { createI18n } from 'vue-i18n'

import { render, screen } from '@testing-library/vue'
Expand Down Expand Up @@ -28,33 +30,9 @@ vi.mock<unknown>(
})
)

const mockState = vi.hoisted(() => ({
holder: null as null | { isFreeTier: boolean; promptMounted: boolean }
}))

vi.mock<unknown>(
import('@/composables/billing/useBillingContext'),
async () => {
const { computed, reactive } = await import('vue')
mockState.holder ??= reactive({ isFreeTier: true, promptMounted: false })
return {
useBillingContext: vi.fn(() => ({
isFreeTier: computed(() => mockState.holder!.isFreeTier)
}))
}
}
)

vi.mock<unknown>(
import('@/platform/cloud/subscription/composables/useSubscribeCtaPresence'),
async () => {
const { computed, reactive } = await import('vue')
mockState.holder ??= reactive({ isFreeTier: true, promptMounted: false })
return {
useSubscribeToRunPromptPresence: () =>
computed(() => mockState.holder!.promptMounted)
}
}
vi.mock(import('@/composables/billing/useBillingContext'))
vi.mock(
import('@/platform/cloud/subscription/composables/useSubscribeCtaPresence')
)

vi.mock(import('firebase/app'), () => ({
Expand All @@ -70,7 +48,11 @@ vi.mock<unknown>(import('firebase/auth'), () => ({
signOut: vi.fn()
}))

function renderComponent() {
function renderComponent(promptMounted = computed(() => false)) {
const billing = useBillingContext()
billing.isFreeTier = computed(() => true)
vi.mocked(useBillingContext).mockReturnValue(billing)
vi.mocked(useSubscribeToRunPromptPresence).mockReturnValue(promptMounted)
const i18n = createI18n({
legacy: false,
locale: 'en',
Expand All @@ -85,11 +67,6 @@ function renderComponent() {
}

describe('TopbarSubscribeButton', () => {
beforeEach(() => {
mockState.holder!.isFreeTier = true
mockState.holder!.promptMounted = false
})

it('renders on cloud when isFreeTier is true', () => {
mockIsCloud.value = true
renderComponent()
Expand All @@ -98,13 +75,13 @@ describe('TopbarSubscribeButton', () => {

it('yields while a Run-slot subscribe prompt is mounted, and returns when it unmounts', async () => {
mockIsCloud.value = true
mockState.holder!.promptMounted = true
renderComponent()
const promptMounted = ref(true)
renderComponent(computed(() => promptMounted.value))
expect(
screen.queryByTestId('topbar-subscribe-button')
).not.toBeInTheDocument()

mockState.holder!.promptMounted = false
promptMounted.value = false
await nextTick()
expect(screen.getByTestId('topbar-subscribe-button')).toBeInTheDocument()
})
Expand Down
50 changes: 50 additions & 0 deletions src/composables/billing/__mocks__/useBillingContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { vi } from 'vitest'
import { computed, ref } from 'vue'

import type { BillingContext } from '../types'
import type { useBillingContext as realUseBillingContext } from '../useBillingContext'

function createBillingContextMock(): BillingContext {
return {
type: computed(() => 'legacy'),
isInitialized: ref(false),
isLoading: ref(false),
error: ref(null),
subscription: computed(() => null),
balance: computed(() => null),
plans: computed(() => []),
currentPlanSlug: computed(() => null),
teamCreditStops: computed(() => null),
currentTeamCreditStop: computed(() => null),
maxSeats: computed(() => null),
occupiedSeats: computed(() => null),
canAccessSubscriptionFeatures: computed(() => false),
isFreeTier: computed(() => false),
billingStatus: computed(() => null),
subscriptionStatus: computed(() => null),
tier: computed(() => null),
renewalDate: computed(() => null),
isLegacyTeamPlan: computed(() => false),
isTeamPlan: computed(() => false),
canRunWorkflows: computed(() => false),
showsSubscribeToRunPrompt: computed(() => false),
getMaxSeats: vi.fn(() => 1),
initialize: vi.fn(async () => {}),
fetchStatus: vi.fn(async () => {}),
fetchBalance: vi.fn(async () => {}),
subscribe: vi.fn(async () => {}),
previewSubscribe: vi.fn(async () => null),
manageSubscription: vi.fn(async () => {}),
cancelSubscription: vi.fn(async () => {}),
resubscribe: vi.fn(async () => {}),
topup: vi.fn(async () => {}),
fetchPlans: vi.fn(async () => {}),
requireActiveSubscription: vi.fn(async () => {}),
showSubscriptionDialog: vi.fn(),
reconcileSubscriptionSuccess: vi.fn(async () => {})
}
}

export const useBillingContext = vi.fn<typeof realUseBillingContext>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also update the billing mock guidance? docs/testing/unit-testing.md:323 still describes vi.mock(...) plus mockReturnValue(...) as an anti-pattern, but these migrated suites now use that pattern through this manual mock. Would it make sense to align the docs here, or track that as a follow-up?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createBillingContextMock
)
Loading
Loading