diff --git a/src/components/topbar/CurrentUserButton.test.ts b/src/components/topbar/CurrentUserButton.test.ts index a62d9d7cc55..54bd3d4a7b1 100644 --- a/src/components/topbar/CurrentUserButton.test.ts +++ b/src/components/topbar/CurrentUserButton.test.ts @@ -1,9 +1,13 @@ import userEvent from '@testing-library/user-event' -import { render, screen } from '@testing-library/vue' +import { render, screen, waitFor } from '@testing-library/vue' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { computed, defineComponent, h } from 'vue' +import type { Component } from 'vue' +import { computed, defineComponent, h, ref } from 'vue' import { createI18n } from 'vue-i18n' +import { useBillingContext } from '@/composables/billing/useBillingContext' +import type { BalanceInfo } from '@/composables/billing/types' +import CurrentUserPopoverWorkspace from '@/platform/workspace/components/CurrentUserPopoverWorkspace.vue' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import enMessages from '@/locales/en/main.json' with { type: 'json' } import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' @@ -20,6 +24,16 @@ vi.mock(import('@/platform/distribution/types'), () => ({ })) vi.mock(import('@/composables/auth/useCurrentUser')) +vi.mock(import('@/composables/billing/useBillingContext')) +vi.mock(import('@/platform/workspace/composables/useWorkspaceUI')) +vi.mock(import('@/platform/workspace/composables/useBillingCapabilities')) +vi.mock( + import('@/platform/cloud/subscription/composables/useSubscriptionDialog') +) +vi.mock(import('@/platform/settings/composables/useSettingsDialog')) +vi.mock(import('@/composables/billing/useBillingDialogs')) +vi.mock(import('@/platform/workspace/composables/useWorkspaceDialogs')) +vi.mock(import('@/platform/telemetry')) // Mock the WorkspaceProfilePic component vi.mock( @@ -85,7 +99,9 @@ describe('CurrentUserButton', () => { mockIsCloud.value = false }) - function renderComponent() { + function renderComponent( + workspacePopover: Component = CurrentUserPopoverWorkspaceStub + ) { const user = userEvent.setup() const i18n = createI18n({ legacy: false, @@ -97,7 +113,10 @@ describe('CurrentUserButton', () => { global: { plugins: [i18n], stubs: { - CurrentUserPopoverWorkspace: CurrentUserPopoverWorkspaceStub + CurrentUserPopoverWorkspace: workspacePopover, + WorkspaceSwitcherPopover: true, + SubscribeButton: true, + DeploymentSwitcher: true } } }) @@ -105,6 +124,52 @@ describe('CurrentUserButton', () => { return { user, ...result } } + it.for(['personal', 'team'] as const)( + 'refreshes the %s balance on first opening and reopening the profile menu', + async (workspaceType) => { + mockIsCloud.value = true + const balance = ref({ amountMicros: 100, currency: 'USD' }) + const billing = useBillingContext() + billing.balance = computed(() => balance.value) + billing.billingStatus = computed(() => 'paid') + billing.canAccessSubscriptionFeatures = computed(() => true) + vi.mocked(useBillingContext).mockReturnValue(billing) + vi.mocked(billing.fetchBalance) + .mockImplementationOnce(async () => { + balance.value = { amountMicros: 200, currency: 'USD' } + }) + .mockImplementationOnce(async () => { + balance.value = { amountMicros: 75, currency: 'USD' } + }) + useTeamWorkspaceStore().initState = 'ready' + Object.assign(useTeamWorkspaceStore(), { + workspaceName: `${workspaceType} workspace`, + isInPersonalWorkspace: workspaceType === 'personal' + }) + const { user } = renderComponent(CurrentUserPopoverWorkspace) + + expect(billing.fetchBalance).not.toHaveBeenCalled() + await user.click(screen.getByRole('button', { name: 'Current user' })) + + expect(await screen.findByText('422')).toBeInTheDocument() + expect(billing.fetchBalance).toHaveBeenCalledOnce() + + await user.click(screen.getByRole('button', { name: 'Current user' })) + + await waitFor(() => + expect( + screen.queryByTestId('credits-info-button') + ).not.toBeInTheDocument() + ) + expect(billing.fetchBalance).toHaveBeenCalledOnce() + + await user.click(screen.getByRole('button', { name: 'Current user' })) + + expect(await screen.findByText('158')).toBeInTheDocument() + expect(billing.fetchBalance).toHaveBeenCalledTimes(2) + } + ) + it('toggles popover on button click', async () => { const { user } = renderComponent() diff --git a/src/components/topbar/CurrentUserButton.vue b/src/components/topbar/CurrentUserButton.vue index db9c4aa6d96..19f1a95746d 100644 --- a/src/components/topbar/CurrentUserButton.vue +++ b/src/components/topbar/CurrentUserButton.vue @@ -43,11 +43,9 @@ showWorkspacePopover ? 'w-fit max-w-96 min-w-80' : 'w-80' ) " - @show="onPopoverShow" > @@ -111,15 +109,7 @@ const workspaceName = computed(() => { }) const popover = ref | null>(null) -const workspacePopoverContent = ref<{ - refreshBalance: () => void -} | null>(null) - const closePopover = () => { popover.value?.hide() } - -const onPopoverShow = () => { - workspacePopoverContent.value?.refreshBalance() -} diff --git a/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts b/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts index 7353b43bcef..6e2968357a6 100644 --- a/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts +++ b/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts @@ -273,6 +273,7 @@ describe('CurrentUserPopoverWorkspace', () => { expect( screen.queryByTestId('workspace-settings-menu-item') ).not.toBeInTheDocument() + expect(useBillingContext().fetchBalance).not.toHaveBeenCalled() }) describe('with a Desktop host session', () => { diff --git a/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue b/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue index 5d9cff0175b..ade9bf7d80e 100644 --- a/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue +++ b/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue @@ -257,7 +257,7 @@ import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames' import { onClickOutside } from '@vueuse/core' import { storeToRefs } from 'pinia' -import { computed, ref, useTemplateRef } from 'vue' +import { computed, onMounted, ref, useTemplateRef } from 'vue' import { useI18n } from 'vue-i18n' import { formatCreditsFromCents } from '@/base/credits/comfyCredits' @@ -472,9 +472,7 @@ const closeWorkspaceSwitcherFromKeyboard = () => { isWorkspaceNameTooltipOpen.value = false } -const refreshBalance = () => { +onMounted(() => { if (!accountActionsOnly) void fetchBalance() -} - -defineExpose({ refreshBalance }) +})