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
73 changes: 69 additions & 4 deletions src/components/topbar/CurrentUserButton.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Check warning on line 8 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'billing/application' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/composables/billing/useBillingContext.ts Route the import through an allowed zone or restructure the dependency.
import type { BalanceInfo } from '@/composables/billing/types'

Check warning on line 9 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'billing/domain' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/composables/billing/types.ts Route the import through an allowed zone or restructure the dependency.
import CurrentUserPopoverWorkspace from '@/platform/workspace/components/CurrentUserPopoverWorkspace.vue'

Check warning on line 10 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'workspace/presentation' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/workspace/components/CurrentUserPopoverWorkspace.vue Route the import through an allowed zone or restructure the dependency.
import { useCurrentUser } from '@/composables/auth/useCurrentUser'
import enMessages from '@/locales/en/main.json' with { type: 'json' }
import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
Expand All @@ -20,6 +24,16 @@
}))

vi.mock(import('@/composables/auth/useCurrentUser'))
vi.mock(import('@/composables/billing/useBillingContext'))

Check warning on line 27 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'billing/application' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/composables/billing/__mocks__/useBillingContext.ts Route the import through an allowed zone or restructure the dependency.
vi.mock(import('@/platform/workspace/composables/useWorkspaceUI'))

Check warning on line 28 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'workspace/application' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/workspace/composables/useWorkspaceUI.ts Route the import through an allowed zone or restructure the dependency.

Check warning on line 28 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'workspace/application/outer-1' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/workspace/composables/__mocks__/useWorkspaceUI.ts Route the import through an allowed zone or restructure the dependency.
vi.mock(import('@/platform/workspace/composables/useBillingCapabilities'))

Check warning on line 29 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'billing/application' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/workspace/composables/useBillingCapabilities.ts Route the import through an allowed zone or restructure the dependency.

Check warning on line 29 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'billing/application' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/workspace/composables/__mocks__/useBillingCapabilities.ts Route the import through an allowed zone or restructure the dependency.
vi.mock(

Check warning on line 30 in src/components/topbar/CurrentUserButton.test.ts

View workflow job for this annotation

GitHub Actions / fallow / fallow

Boundary violation

Import from zone 'authentication/presentation' to zone 'cloud/application/outer-3' is not allowed. src/components/topbar/CurrentUserButton.test.ts -> src/platform/cloud/subscription/composables/__mocks__/useSubscriptionDialog.ts Route the import through an allowed zone or restructure the dependency.
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<unknown>(
Expand Down Expand Up @@ -85,7 +99,9 @@
mockIsCloud.value = false
})

function renderComponent() {
function renderComponent(
workspacePopover: Component = CurrentUserPopoverWorkspaceStub
) {
const user = userEvent.setup()
const i18n = createI18n({
legacy: false,
Expand All @@ -97,14 +113,63 @@
global: {
plugins: [i18n],
stubs: {
CurrentUserPopoverWorkspace: CurrentUserPopoverWorkspaceStub
CurrentUserPopoverWorkspace: workspacePopover,
WorkspaceSwitcherPopover: true,
SubscribeButton: true,
DeploymentSwitcher: true
}
}
})

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<BalanceInfo>({ 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()

Expand Down
10 changes: 0 additions & 10 deletions src/components/topbar/CurrentUserButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@
showWorkspacePopover ? 'w-fit max-w-96 min-w-80' : 'w-80'
)
"
@show="onPopoverShow"
>
<CurrentUserPopoverWorkspace
v-if="showWorkspacePopover"
ref="workspacePopoverContent"
:account-actions-only="initState !== 'ready'"
@close="closePopover"
/>
Expand Down Expand Up @@ -111,15 +109,7 @@ const workspaceName = computed(() => {
})

const popover = ref<InstanceType<typeof Popover> | null>(null)
const workspacePopoverContent = ref<{
refreshBalance: () => void
} | null>(null)

const closePopover = () => {
popover.value?.hide()
}

const onPopoverShow = () => {
workspacePopoverContent.value?.refreshBalance()
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -472,9 +472,7 @@ const closeWorkspaceSwitcherFromKeyboard = () => {
isWorkspaceNameTooltipOpen.value = false
}

const refreshBalance = () => {
onMounted(() => {
if (!accountActionsOnly) void fetchBalance()
}

defineExpose({ refreshBalance })
})
</script>
Loading