import { Emitter } from '@rocket.chat/emitter'; import type { CallRole, CallState } from '@rocket.chat/media-signaling'; import { renderHook, act } from '@testing-library/react'; import type { ReactNode } from 'react'; import { usePeekMediaSessionState } from './usePeekMediaSessionState'; import MockedInstanceProvider from '../providers/MockedInstanceProvider'; type MockInstance = { getState: () => { state: CallState; localParticipant: { role: CallRole } } | null; on: (event: 'sessionStateChange', onStoreChange: () => void) => () => void; }; const createWrapper = (instance: MockInstance | undefined) => { const wrapper = ({ children }: { children?: ReactNode }) => ( {children} ); return wrapper; }; describe('usePeekMediaSessionState', () => { it('returns "unavailable" when instance is undefined', () => { const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(undefined), }); expect(result.current).toBe('unavailable'); }); it('returns "available" when instance has no main call', () => { const instance: MockInstance = { getState: () => null, on: () => () => undefined, }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe('available'); }); it('returns "available" when main call state does not map to a widget state (e.g. hangup)', () => { const instance: MockInstance = { getState: () => ({ state: 'hangup', localParticipant: { role: 'caller' } }), on: () => () => undefined, }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe('available'); }); describe('when main call maps to a widget state', () => { it.each([ ['active', 'caller', 'ongoing'] as const, ['active', 'callee', 'ongoing'] as const, ['renegotiating', 'callee', 'ongoing'] as const, ['renegotiating', 'caller', 'ongoing'] as const, ])('returns "ongoing" for state "%s" and role "%s"', (callState, role, expected) => { const instance: MockInstance = { getState: () => ({ state: callState, localParticipant: { role } }), on: () => () => undefined, }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe(expected); }); it.each(['ringing', 'none', 'accepted'] as const)('returns "ringing" for callee when state is "%s"', (state) => { const instance: MockInstance = { getState: () => ({ state, localParticipant: { role: 'callee' } }), on: () => () => undefined, }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe('ringing'); }); it.each(['ringing', 'none', 'accepted'] as const)('returns "calling" for caller when state is "%s"', (state) => { const instance: MockInstance = { getState: () => ({ state, localParticipant: { role: 'caller' } }), on: () => () => undefined, }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe('calling'); }); }); describe('sessionStateChange subscription', () => { it('updates state when sessionStateChange is emitted', () => { const emitter = new Emitter<{ sessionStateChange: void }>(); let instanceState: { state: CallState; localParticipant: { role: CallRole } } | null = { state: 'active', localParticipant: { role: 'caller', }, }; const instance: MockInstance = { getState: () => instanceState, on: (event, onStoreChange) => emitter.on(event, onStoreChange), }; const { result } = renderHook(() => usePeekMediaSessionState(), { wrapper: createWrapper(instance), }); expect(result.current).toBe('ongoing'); act(() => { instanceState = null; emitter.emit('sessionStateChange'); }); expect(result.current).toBe('available'); act(() => { instanceState = { state: 'ringing', localParticipant: { role: 'callee' } }; emitter.emit('sessionStateChange'); }); expect(result.current).toBe('ringing'); }); }); });