|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { |
| 3 | + apiClientRequestMock, |
| 4 | + apiClientRequestMockFns, |
| 5 | +} from '@sim/testing/mocks/api-client-request.mock' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { renderDataHook, unmountDataHooks } from '@/platform/lib/render-hook.test-support' |
| 8 | + |
| 9 | +vi.mock('@/lib/api/client/request', () => apiClientRequestMock) |
| 10 | + |
| 11 | +import { useOpenBillingPortal } from '@/hooks/queries/subscription' |
| 12 | + |
| 13 | +const mockRequestJson = apiClientRequestMockFns.mockRequestJson |
| 14 | +const BODY = { context: 'user' } as const |
| 15 | +const PORTAL_URL = 'https://billing.example.test/session' |
| 16 | + |
| 17 | +function deferredPortal() { |
| 18 | + let resolve: (value: { url: string }) => void = () => {} |
| 19 | + let reject: (error: Error) => void = () => {} |
| 20 | + mockRequestJson.mockReturnValueOnce( |
| 21 | + new Promise((res, rej) => { |
| 22 | + resolve = res |
| 23 | + reject = rej |
| 24 | + }) |
| 25 | + ) |
| 26 | + return { resolve, reject } |
| 27 | +} |
| 28 | + |
| 29 | +async function settle() { |
| 30 | + await vi.waitFor(() => expect(mockRequestJson).toHaveBeenCalled()) |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Failure mode: the tab opened before the request belongs to the viewer's click, so it has to reach |
| 35 | + * the portal or close whatever the page does in the meantime, including leave. |
| 36 | + */ |
| 37 | +describe('useOpenBillingPortal after the page that asked has gone', () => { |
| 38 | + const portalTab = { location: { href: '' }, close: vi.fn() } |
| 39 | + const openTab = vi.fn() |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + mockRequestJson.mockReset() |
| 43 | + portalTab.location.href = '' |
| 44 | + portalTab.close.mockReset() |
| 45 | + openTab.mockReset().mockReturnValue(portalTab) |
| 46 | + vi.stubGlobal('open', openTab) |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await unmountDataHooks() |
| 51 | + }) |
| 52 | + |
| 53 | + it('still sends the opened tab to the portal', async () => { |
| 54 | + const portal = deferredPortal() |
| 55 | + const hook = await renderDataHook(() => useOpenBillingPortal()) |
| 56 | + hook.result.current.open(BODY, vi.fn()) |
| 57 | + await settle() |
| 58 | + await hook.unmount() |
| 59 | + |
| 60 | + portal.resolve({ url: PORTAL_URL }) |
| 61 | + |
| 62 | + await vi.waitFor(() => expect(portalTab.location.href).toBe(PORTAL_URL)) |
| 63 | + }) |
| 64 | + |
| 65 | + it('still closes the opened tab and reports a failed request', async () => { |
| 66 | + const portal = deferredPortal() |
| 67 | + const onError = vi.fn() |
| 68 | + const hook = await renderDataHook(() => useOpenBillingPortal()) |
| 69 | + hook.result.current.open(BODY, onError) |
| 70 | + await settle() |
| 71 | + await hook.unmount() |
| 72 | + |
| 73 | + portal.reject(new Error('no portal')) |
| 74 | + |
| 75 | + await vi.waitFor(() => expect(portalTab.close).toHaveBeenCalledOnce()) |
| 76 | + expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'no portal' })) |
| 77 | + }) |
| 78 | + |
| 79 | + it('leaves the current page alone when a blocked tab outlives the page that asked', async () => { |
| 80 | + const location = { href: 'https://app.example.test/elsewhere' } |
| 81 | + vi.stubGlobal('location', location) |
| 82 | + openTab.mockReturnValue(null) |
| 83 | + const portal = deferredPortal() |
| 84 | + const hook = await renderDataHook(() => useOpenBillingPortal()) |
| 85 | + hook.result.current.open(BODY, vi.fn()) |
| 86 | + await settle() |
| 87 | + await hook.unmount() |
| 88 | + |
| 89 | + portal.resolve({ url: PORTAL_URL }) |
| 90 | + await vi.waitFor(() => expect(hook.queryClient.isMutating()).toBe(0)) |
| 91 | + |
| 92 | + expect(location.href).toBe('https://app.example.test/elsewhere') |
| 93 | + }) |
| 94 | + |
| 95 | + it('takes the current tab when one is blocked and the page is still here', async () => { |
| 96 | + const location = { href: 'https://app.example.test/billing' } |
| 97 | + vi.stubGlobal('location', location) |
| 98 | + openTab.mockReturnValue(null) |
| 99 | + const portal = deferredPortal() |
| 100 | + const hook = await renderDataHook(() => useOpenBillingPortal()) |
| 101 | + hook.result.current.open(BODY, vi.fn()) |
| 102 | + await settle() |
| 103 | + |
| 104 | + portal.resolve({ url: PORTAL_URL }) |
| 105 | + |
| 106 | + await vi.waitFor(() => expect(location.href).toBe(PORTAL_URL)) |
| 107 | + }) |
| 108 | +}) |
0 commit comments