Repository navigation
FRONT-3856 fix(billing): re-read the local Billing tab when the user returns to it #23391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dante01yoon
merged 2 commits into
main
from
1.61-local-billing/front-3856-refresh-on-focus
Nov 15, 2026
+204
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/platform/billing/composables/useRefreshBillingOnReturn.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { describe, expect, it, onTestFinished, vi } from 'vitest' | ||
| import { effectScope } from 'vue' | ||
|
|
||
| import { onBillingRefresh } from '@/platform/billing/billingRefresh' | ||
| import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' | ||
| import { useBillingContext } from '@/platform/billing/composables/useBillingContext' | ||
| import { useRefreshBillingOnReturn } from '@/platform/billing/composables/useRefreshBillingOnReturn' | ||
|
|
||
| const distribution = vi.hoisted(() => ({ isCloud: false })) | ||
| vi.mock(import('@/platform/distribution/types'), () => ({ | ||
| get isCloud() { | ||
| return distribution.isCloud | ||
| } | ||
| })) | ||
| vi.mock(import('@/platform/billing/composables/useBillingContext')) | ||
| vi.mock(import('@/platform/billing/composables/useBillingCapabilities')) | ||
|
|
||
| const leave = () => window.dispatchEvent(new Event('blur')) | ||
| const comeBack = () => window.dispatchEvent(new Event('focus')) | ||
|
|
||
| function heldRead() { | ||
| const settles: Array<() => void> = [] | ||
| const read = vi.fn( | ||
| () => | ||
| new Promise<void>((resolve) => { | ||
| settles.push(resolve) | ||
| }) | ||
| ) | ||
| const settle = async (index: number) => { | ||
| settles[index]() | ||
| await vi.advanceTimersByTimeAsync(0) | ||
| } | ||
| return { read, settle } | ||
| } | ||
|
|
||
| function openBillingTab({ isCloud = false, isInitialized = true } = {}) { | ||
| distribution.isCloud = isCloud | ||
| onTestFinished(() => { | ||
| distribution.isCloud = false | ||
| }) | ||
| const account = heldRead() | ||
| onTestFinished(onBillingRefresh(account.read)) | ||
| const capabilities = heldRead() | ||
| const billing = useBillingContext() | ||
| billing.isInitialized.value = isInitialized | ||
| vi.mocked(useBillingContext).mockReturnValue(billing) | ||
| vi.mocked(useBillingCapabilities().refresh).mockImplementation( | ||
| capabilities.read | ||
| ) | ||
| const scope = effectScope() | ||
| scope.run(useRefreshBillingOnReturn) | ||
| onTestFinished(() => scope.stop()) | ||
| return { | ||
| readAccount: account.read, | ||
| readCapabilities: capabilities.read, | ||
| settleAccount: account.settle, | ||
| settleCapabilities: capabilities.settle, | ||
| close: () => scope.stop() | ||
| } | ||
| } | ||
|
|
||
| describe('useRefreshBillingOnReturn', () => { | ||
| it('re-reads plan, balance and capabilities once when the user returns to a local tab', () => { | ||
| const { readAccount, readCapabilities } = openBillingTab() | ||
|
|
||
| leave() | ||
| comeBack() | ||
| document.dispatchEvent(new Event('visibilitychange')) | ||
|
|
||
| expect(readAccount).toHaveBeenCalledExactlyOnceWith('account') | ||
| expect(readCapabilities).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| it('re-reads on every later return', async () => { | ||
| const { readAccount, settleAccount, settleCapabilities } = openBillingTab() | ||
|
|
||
| leave() | ||
| comeBack() | ||
| await settleAccount(0) | ||
| await settleCapabilities(0) | ||
| leave() | ||
| comeBack() | ||
|
|
||
| expect(readAccount).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('reads nothing on Cloud', () => { | ||
| const { readAccount, readCapabilities } = openBillingTab({ isCloud: true }) | ||
|
|
||
| leave() | ||
| comeBack() | ||
|
|
||
| expect(readAccount).not.toHaveBeenCalled() | ||
| expect(readCapabilities).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('holds the read of a return that lands in flight until account and capabilities both settle', async () => { | ||
| const { readAccount, readCapabilities, settleAccount, settleCapabilities } = | ||
| openBillingTab() | ||
|
|
||
| leave() | ||
| comeBack() | ||
| leave() | ||
| comeBack() | ||
| await settleAccount(0) | ||
|
|
||
| expect(readAccount).toHaveBeenCalledOnce() | ||
| expect(readCapabilities).toHaveBeenCalledOnce() | ||
|
|
||
| await settleCapabilities(0) | ||
|
|
||
| expect(readAccount).toHaveBeenCalledTimes(2) | ||
| expect(readCapabilities).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('leaves the first load alone while billing has not initialized', () => { | ||
| const { readAccount, readCapabilities } = openBillingTab({ | ||
| isInitialized: false | ||
| }) | ||
|
|
||
| leave() | ||
| comeBack() | ||
|
|
||
| expect(readAccount).not.toHaveBeenCalled() | ||
| expect(readCapabilities).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('stops reading once the tab is closed', () => { | ||
| const { readAccount, close } = openBillingTab() | ||
|
|
||
| close() | ||
| leave() | ||
| comeBack() | ||
|
|
||
| expect(readAccount).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
23 changes: 23 additions & 0 deletions
23
src/platform/billing/composables/useRefreshBillingOnReturn.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { onScopeDispose } from 'vue' | ||
|
|
||
| import { refreshBilling } from '@/platform/billing/billingRefresh' | ||
| import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' | ||
| import { useBillingContext } from '@/platform/billing/composables/useBillingContext' | ||
| import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn' | ||
| import { isCloud } from '@/platform/distribution/types' | ||
|
|
||
| /** On local, re-reads plan, balance and capabilities on each return to this tab or window. */ | ||
| export function useRefreshBillingOnReturn(): void { | ||
| if (isCloud) return | ||
| const billing = useBillingContext() | ||
| const capabilities = useBillingCapabilities() | ||
| onScopeDispose( | ||
| registerRefreshOnReturn(async () => { | ||
| if (!billing.isInitialized.value) return | ||
| await Promise.allSettled([ | ||
| refreshBilling('account'), | ||
|
dante01yoon marked this conversation as resolved.
Resolved
|
||
| capabilities.refresh() | ||
| ]) | ||
| }) | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.