Files
simstudioai--sim/apps/sim/hooks/queries/unsubscribe.test.tsx
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

207 lines
6.0 KiB
TypeScript

/**
* @vitest-environment jsdom
*/
import { act, type ReactNode } from 'react'
import { sleep } from '@sim/utils/helpers'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const { mockRequestJson } = vi.hoisted(() => ({
mockRequestJson: vi.fn(),
}))
vi.mock('@/lib/api/client/request', () => ({
requestJson: mockRequestJson,
}))
import { requestJson } from '@/lib/api/client/request'
import { unsubscribeGetContract, unsubscribePostContract } from '@/lib/api/contracts/user'
import {
unsubscribeKeys,
useUnsubscribe,
useUnsubscribeMutation,
} from '@/hooks/queries/unsubscribe'
const EMAIL = 'person@example.com'
const TOKEN = 'tok-123'
const getResponse = {
success: true as const,
email: EMAIL,
token: TOKEN,
emailType: 'marketing',
isTransactional: false,
currentPreferences: {
unsubscribeAll: false,
unsubscribeMarketing: false,
unsubscribeUpdates: false,
unsubscribeNotifications: false,
},
}
/**
* Minimal dependency-free hook harness (the repo has no `@testing-library/react`).
* Mounts the hook in a real React 19 root under jsdom, wrapped in a real
* `QueryClientProvider`, so query/mutation lifecycles run exactly as in the app.
*/
function renderHookWithClient<T>(useHook: () => T): {
result: () => T
queryClient: QueryClient
unmount: () => void
} {
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
const container = document.createElement('div')
const root: Root = createRoot(container)
let latest: T
function Probe() {
latest = useHook()
return null
}
function Wrapper({ children }: { children: ReactNode }) {
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}
act(() => {
root.render(
<Wrapper>
<Probe />
</Wrapper>
)
})
return {
result: () => latest,
queryClient,
unmount: () => act(() => root.unmount()),
}
}
/** Flush pending microtasks and the macrotask queue (query observer scheduling) inside act(). */
async function flush() {
await act(async () => {
for (let i = 0; i < 5; i++) {
await Promise.resolve()
await sleep(0)
}
})
}
describe('useUnsubscribe', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
it('is disabled and does not fetch when email or token is missing', async () => {
const missingToken = renderHookWithClient(() => useUnsubscribe(EMAIL, undefined))
const missingEmail = renderHookWithClient(() => useUnsubscribe(undefined, TOKEN))
const missingBoth = renderHookWithClient(() => useUnsubscribe(undefined, undefined))
await flush()
expect(missingToken.result().fetchStatus).toBe('idle')
expect(missingEmail.result().fetchStatus).toBe('idle')
expect(missingBoth.result().fetchStatus).toBe('idle')
expect(mockRequestJson).not.toHaveBeenCalled()
missingToken.unmount()
missingEmail.unmount()
missingBoth.unmount()
})
it('fetches when both params are present and surfaces the contract data', async () => {
mockRequestJson.mockResolvedValueOnce(getResponse)
const { result, unmount } = renderHookWithClient(() => useUnsubscribe(EMAIL, TOKEN))
await flush()
expect(requestJson).toHaveBeenCalledTimes(1)
expect(requestJson).toHaveBeenCalledWith(
unsubscribeGetContract,
expect.objectContaining({ query: { email: EMAIL, token: TOKEN } })
)
expect(result().isSuccess).toBe(true)
expect(result().data).toEqual(getResponse)
expect(result().data?.isTransactional).toBe(false)
unmount()
})
})
describe('useUnsubscribeMutation', () => {
beforeEach(() => {
vi.clearAllMocks()
})
afterEach(() => {
vi.restoreAllMocks()
})
it('calls requestJson with the post contract and flips the cached preference flag on success', async () => {
mockRequestJson.mockResolvedValueOnce({
success: true as const,
message: 'Unsubscribed',
email: EMAIL,
type: 'marketing' as const,
emailType: 'marketing',
})
const { result, queryClient, unmount } = renderHookWithClient(() => useUnsubscribeMutation())
const detailKey = unsubscribeKeys.detail(EMAIL, TOKEN)
queryClient.setQueryData(detailKey, getResponse)
await act(async () => {
await result().mutateAsync({ email: EMAIL, token: TOKEN, type: 'marketing' })
})
await flush()
expect(result().isSuccess).toBe(true)
expect(requestJson).toHaveBeenCalledTimes(1)
expect(requestJson).toHaveBeenCalledWith(
unsubscribePostContract,
expect.objectContaining({ body: { email: EMAIL, token: TOKEN, type: 'marketing' } })
)
const reconciled = queryClient.getQueryData<typeof getResponse>(detailKey)
expect(reconciled?.currentPreferences.unsubscribeMarketing).toBe(true)
expect(reconciled?.currentPreferences.unsubscribeAll).toBe(false)
expect(reconciled?.currentPreferences.unsubscribeUpdates).toBe(false)
unmount()
})
it('flips unsubscribeAll when type is "all"', async () => {
mockRequestJson.mockResolvedValueOnce({
success: true as const,
message: 'Unsubscribed',
email: EMAIL,
type: 'all' as const,
emailType: 'marketing',
})
const { result, queryClient, unmount } = renderHookWithClient(() => useUnsubscribeMutation())
const detailKey = unsubscribeKeys.detail(EMAIL, TOKEN)
queryClient.setQueryData(detailKey, getResponse)
await act(async () => {
await result().mutateAsync({ email: EMAIL, token: TOKEN, type: 'all' })
})
await flush()
expect(result().isSuccess).toBe(true)
const reconciled = queryClient.getQueryData<typeof getResponse>(detailKey)
expect(reconciled?.currentPreferences.unsubscribeAll).toBe(true)
unmount()
})
})