/** @jest-environment jsdom */ import { renderHook } from '@testing-library/react'; import { type ReactNode } from 'react'; import { useJourneyTestRuntime, baseJourneyDef, lastJourney, } from '../../__test-utils__/test-runtime'; import { completeJourney, defineJourney } from '../../core'; import { JourneyScope } from '../../integrations/react/scope'; import { useJourneyStep } from '../../integrations/react/use-journey-step'; const emitMock = jest.fn(); describe('[journey] useJourneyStep', () => { useJourneyTestRuntime(emitMock); test('runs a step on the JourneyScope journey', async () => { const wrapper = ({ children }: { children: ReactNode }) => ( {children} ); const { result } = renderHook(() => useJourneyStep(), { wrapper }); await result.current('load', () => 'ok'); completeJourney('hook-journey'); expect(lastJourney(emitMock).steps[0].name).toBe('load'); expect(lastJourney(emitMock).outcome).toBe('good'); }); test('auto-starts when given a handle', async () => { const handle = defineJourney({ ...baseJourneyDef, name: 'hook-handle' }); const { result } = renderHook(() => useJourneyStep(handle)); await result.current('load', () => 'ok'); handle.complete(); expect(lastJourney(emitMock).steps[0].name).toBe('load'); expect(lastJourney(emitMock).outcome).toBe('good'); }); });