/** @jest-environment jsdom */ import { render, screen } from '@testing-library/react'; import { JourneyNameContext, useJourneyName, useOptionalJourneyName, } from '../../integrations/react/context'; function OptionalProbe() { const name = useOptionalJourneyName(); return {name ?? 'null'}; } function RequiredProbe() { const name = useJourneyName(); return {name}; } describe('[journey] context hooks', () => { describe('useOptionalJourneyName', () => { describe('without a provider', () => { const subject = () => render(); test('returns null', () => { subject(); expect(screen.getByTestId('optional').textContent).toBe('null'); }); }); describe('with a provider', () => { const subject = () => render( ); test('reads the provided name', () => { subject(); expect(screen.getByTestId('optional').textContent).toBe('scoped'); }); }); }); describe('useJourneyName', () => { describe('with a provider', () => { const subject = () => render( ); test('returns the provided name', () => { subject(); expect(screen.getByTestId('required').textContent).toBe('scoped'); }); }); describe('without a provider', () => { let spy: jest.SpyInstance; beforeEach(() => { spy = jest.spyOn(console, 'error').mockImplementation(() => {}); }); afterEach(() => { spy.mockRestore(); }); const subject = () => render(); test('throws', () => { expect(() => subject()).toThrow( 'useJourneyName must be used within a ' ); }); }); }); });