|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { act } from 'react' |
| 4 | +import { |
| 5 | + apiClientRequestMock, |
| 6 | + apiClientRequestMockFns, |
| 7 | +} from '@sim/testing/mocks/api-client-request.mock' |
| 8 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 9 | +import { createRoot, type Root } from 'react-dom/client' |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 11 | + |
| 12 | +vi.mock('@/lib/api/client/request', () => apiClientRequestMock) |
| 13 | + |
| 14 | +import { useStartSlackSearchOAuth } from '@/hooks/queries/slack-search' |
| 15 | + |
| 16 | +const mockRequestJson = apiClientRequestMockFns.mockRequestJson |
| 17 | + |
| 18 | +describe('useStartSlackSearchOAuth', () => { |
| 19 | + let root: Root |
| 20 | + let client: QueryClient |
| 21 | + let result: ReturnType<typeof useStartSlackSearchOAuth> |
| 22 | + |
| 23 | + function Probe() { |
| 24 | + result = useStartSlackSearchOAuth() |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 30 | + mockRequestJson.mockReset() |
| 31 | + client = new QueryClient() |
| 32 | + root = createRoot(document.createElement('div')) |
| 33 | + await act(async () => |
| 34 | + root.render( |
| 35 | + <QueryClientProvider client={client}> |
| 36 | + <Probe /> |
| 37 | + </QueryClientProvider> |
| 38 | + ) |
| 39 | + ) |
| 40 | + }) |
| 41 | + |
| 42 | + afterEach(async () => { |
| 43 | + await act(async () => root.unmount()) |
| 44 | + client.clear() |
| 45 | + }) |
| 46 | + |
| 47 | + it('cancels an in-flight browser OAuth start when its signal aborts', async () => { |
| 48 | + mockRequestJson.mockImplementation( |
| 49 | + (_contract: unknown, input: { signal?: AbortSignal }) => |
| 50 | + new Promise((_resolve, reject) => |
| 51 | + input.signal?.addEventListener('abort', () => reject(input.signal?.reason)) |
| 52 | + ) |
| 53 | + ) |
| 54 | + const controller = new AbortController() |
| 55 | + let outcome: 'pending' | 'rejected' = 'pending' |
| 56 | + await act(async () => { |
| 57 | + void result |
| 58 | + .mutateAsync({ |
| 59 | + organizationId: 'org-1', |
| 60 | + name: 'Sim Search', |
| 61 | + description: 'Search Slack', |
| 62 | + mode: 'shared', |
| 63 | + signal: controller.signal, |
| 64 | + }) |
| 65 | + .catch(() => { |
| 66 | + outcome = 'rejected' |
| 67 | + }) |
| 68 | + }) |
| 69 | + await act(async () => controller.abort()) |
| 70 | + expect(outcome).toBe('rejected') |
| 71 | + }) |
| 72 | +}) |
0 commit comments