Skip to content

Commit 9634d2e

Browse files
committed
test(org): use the central mocks in the portal and token tests
1 parent 7091718 commit 9634d2e

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

‎apps/sim/hooks/queries/subscription-portal.test.tsx‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/** @vitest-environment jsdom */
2+
import {
3+
apiClientRequestMock,
4+
apiClientRequestMockFns,
5+
} from '@sim/testing/mocks/api-client-request.mock'
26
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
37
import { renderDataHook, unmountDataHooks } from '@/platform/lib/render-hook.test-support'
48

5-
const mocks = vi.hoisted(() => ({ requestJson: vi.fn() }))
6-
7-
vi.mock('@/lib/api/client/request', () => ({ requestJson: mocks.requestJson }))
9+
vi.mock('@/lib/api/client/request', () => apiClientRequestMock)
810

911
import { useOpenBillingPortal } from '@/hooks/queries/subscription'
1012

13+
const mockRequestJson = apiClientRequestMockFns.mockRequestJson
1114
const BODY = { context: 'user' } as const
1215
const PORTAL_URL = 'https://billing.example.test/session'
1316

1417
function deferredPortal() {
1518
let resolve: (value: { url: string }) => void = () => {}
1619
let reject: (error: Error) => void = () => {}
17-
mocks.requestJson.mockReturnValueOnce(
20+
mockRequestJson.mockReturnValueOnce(
1821
new Promise((res, rej) => {
1922
resolve = res
2023
reject = rej
@@ -24,7 +27,7 @@ function deferredPortal() {
2427
}
2528

2629
async function settle() {
27-
await vi.waitFor(() => expect(mocks.requestJson).toHaveBeenCalled())
30+
await vi.waitFor(() => expect(mockRequestJson).toHaveBeenCalled())
2831
}
2932

3033
/**
@@ -36,7 +39,7 @@ describe('useOpenBillingPortal after the page that asked has gone', () => {
3639
const openTab = vi.fn()
3740

3841
beforeEach(() => {
39-
mocks.requestJson.mockReset()
42+
mockRequestJson.mockReset()
4043
portalTab.location.href = ''
4144
portalTab.close.mockReset()
4245
openTab.mockReset().mockReturnValue(portalTab)
@@ -45,7 +48,6 @@ describe('useOpenBillingPortal after the page that asked has gone', () => {
4548

4649
afterEach(async () => {
4750
await unmountDataHooks()
48-
vi.unstubAllGlobals()
4951
})
5052

5153
it('still sends the opened tab to the portal', async () => {

‎apps/sim/lib/organizations/home/account-token.test.ts‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1+
import {
2+
credentialsManagedOauthMock,
3+
credentialsManagedOauthMockFns,
4+
} from '@sim/testing/mocks/credentials-managed-oauth.mock'
15
import { beforeEach, describe, expect, it, vi } from 'vitest'
26

3-
const mocks = vi.hoisted(() => ({
4-
resolve: vi.fn(),
5-
reject: vi.fn(),
6-
recordAccess: vi.fn(),
7-
}))
7+
const mocks = vi.hoisted(() => ({ recordAccess: vi.fn() }))
88

9-
vi.mock('@/lib/credentials/managed-oauth', () => ({
10-
resolveManagedOAuthToken: mocks.resolve,
11-
rejectManagedOAuthToken: mocks.reject,
12-
}))
9+
vi.mock('@/lib/credentials/managed-oauth', () => credentialsManagedOauthMock)
1310
vi.mock('@/lib/oauth/token-resolution', () => ({ recordCredentialAccess: mocks.recordAccess }))
1411

1512
import type { OrganizationMembershipContext } from '@/lib/core/application/organization-authorization'
1613
import { createAccountTokenRead } from '@/lib/organizations/home/account-token'
1714
import { HomeProviderReadError } from '@/lib/organizations/home/provider-reads'
1815

16+
const mockResolve = credentialsManagedOauthMockFns.mockResolveManagedOAuthToken
17+
const mockReject = credentialsManagedOauthMockFns.mockRejectManagedOAuthToken
1918
const input = {
2019
context: { organizationId: 'org', userId: 'viewer' } as OrganizationMembershipContext,
2120
credentialId: 'credential',
@@ -26,19 +25,19 @@ const refused = () => new HomeProviderReadError('token_rejected', 401)
2625
describe('one request reading an account several times', () => {
2726
beforeEach(() => {
2827
let issued = 0
29-
mocks.resolve.mockReset().mockImplementation(async () => {
28+
mockResolve.mockReset().mockImplementation(async () => {
3029
issued += 1
3130
return { accessToken: `token-${issued}` }
3231
})
33-
mocks.reject.mockReset().mockResolvedValue(false)
32+
mockReject.mockReset().mockResolvedValue(false)
3433
mocks.recordAccess.mockReset()
3534
})
3635

3736
it('resolves and records the token once across reads', async () => {
3837
const withToken = createAccountTokenRead(input)
3938
const used = [await withToken(async (token) => token), await withToken(async (token) => token)]
4039
expect(used).toEqual(['token-1', 'token-1'])
41-
expect(mocks.resolve).toHaveBeenCalledOnce()
40+
expect(mockResolve).toHaveBeenCalledOnce()
4241
expect(mocks.recordAccess).toHaveBeenCalledOnce()
4342
})
4443

@@ -53,11 +52,11 @@ describe('one request reading an account several times', () => {
5352
})
5453
expect(seen).toEqual(['token-1', 'token-2'])
5554
expect(retried).toBe('token-2')
56-
expect(mocks.reject).toHaveBeenCalledWith(
55+
expect(mockReject).toHaveBeenCalledWith(
5756
expect.objectContaining({ rejectedAccessToken: 'token-1' })
5857
)
5958
await expect(withToken(async (token) => token)).resolves.toBe('token-2')
60-
expect(mocks.resolve).toHaveBeenCalledTimes(2)
59+
expect(mockResolve).toHaveBeenCalledTimes(2)
6160
})
6261

6362
it('asks to reconnect when the fresh token is refused too', async () => {
@@ -77,7 +76,7 @@ describe('one request reading an account several times', () => {
7776
throw failure
7877
})
7978
).rejects.toBe(failure)
80-
expect(mocks.reject).not.toHaveBeenCalled()
79+
expect(mockReject).not.toHaveBeenCalled()
8180
await expect(withToken(async (token) => token)).resolves.toBe('token-1')
8281
})
8382

0 commit comments

Comments
 (0)