|
| 1 | +// MessageComposer's send/update hooks import `useChannel` from this root barrel, and a deep |
| 2 | +// component import first would re-enter it and leave `useChannel` undefined under Vitest. |
| 3 | +import '../../..'; |
| 4 | +import React from 'react'; |
| 5 | +import { act, render, screen } from '@testing-library/react'; |
| 6 | +import { fromPartial } from '@total-typescript/shoehorn'; |
| 7 | +import { AIStates } from 'stream-chat'; |
| 8 | +import type { AIState, Channel as ChannelType, CooldownTimerState } from 'stream-chat'; |
| 9 | + |
| 10 | +import { MessageComposer } from '../MessageComposer'; |
| 11 | +import { Channel } from '../../Channel'; |
| 12 | +import { Chat } from '../../Chat'; |
| 13 | +import { WithComponents } from '../../../context'; |
| 14 | +import type { ComponentContextValue } from '../../../context'; |
| 15 | +import { initClientWithChannels } from '../../../mock-builders'; |
| 16 | +import { MediaRecorderMock } from '../../../mock-builders/browser'; |
| 17 | + |
| 18 | +const STOP_BUTTON_TEST_ID = 'stop-ai-generation-button'; |
| 19 | +const RECORDING_BUTTON_TEST_ID = 'start-recording-audio-button'; |
| 20 | +const COOLDOWN_TIMER_TEST_ID = 'cooldown-timer'; |
| 21 | +const COOLDOWN_SECONDS = 30; |
| 22 | + |
| 23 | +window.MediaRecorder = MediaRecorderMock as unknown as typeof MediaRecorder; |
| 24 | + |
| 25 | +// Renders an empty composer with audio recording enabled, on a channel that allows uploads (which |
| 26 | +// a recording needs) and has a cooldown configured. `overrides` go through `WithComponents`. |
| 27 | +const renderComposer = async ({ |
| 28 | + hideSendButton, |
| 29 | + overrides = {}, |
| 30 | +}: { |
| 31 | + hideSendButton?: boolean; |
| 32 | + overrides?: Partial<ComponentContextValue>; |
| 33 | +} = {}) => { |
| 34 | + const { |
| 35 | + channels: [channel], |
| 36 | + client, |
| 37 | + } = await initClientWithChannels({ |
| 38 | + channelsData: [ |
| 39 | + { channel: { cooldown: COOLDOWN_SECONDS, own_capabilities: ['upload-file'] } }, |
| 40 | + ], |
| 41 | + }); |
| 42 | + await act(() => { |
| 43 | + render( |
| 44 | + <WithComponents overrides={overrides}> |
| 45 | + <Chat client={client}> |
| 46 | + <Channel channel={channel}> |
| 47 | + <MessageComposer audioRecordingEnabled hideSendButton={hideSendButton} /> |
| 48 | + </Channel> |
| 49 | + </Chat> |
| 50 | + </WithComponents>, |
| 51 | + ); |
| 52 | + }); |
| 53 | + return channel; |
| 54 | +}; |
| 55 | + |
| 56 | +const setAIState = (channel: ChannelType, aiState: AIState) => |
| 57 | + act(() => { |
| 58 | + channel.state.partialNext({ aiState }); |
| 59 | + }); |
| 60 | + |
| 61 | +const startCooldown = (channel: ChannelType) => |
| 62 | + act(() => { |
| 63 | + channel.cooldownTimer.state.next( |
| 64 | + fromPartial<CooldownTimerState>({ cooldownRemaining: COOLDOWN_SECONDS }), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | +describe('MessageComposerActions', () => { |
| 69 | + beforeEach(() => { |
| 70 | + (navigator as any).mediaDevices = { getUserMedia: vi.fn().mockResolvedValue({}) }; |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + (navigator as any).mediaDevices = undefined; |
| 75 | + }); |
| 76 | + |
| 77 | + it('shows the recording button for an empty input while the AI is idle', async () => { |
| 78 | + await renderComposer(); |
| 79 | + |
| 80 | + expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 81 | + expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it.each([AIStates.Thinking, AIStates.Generating])( |
| 85 | + 'shows the stop button instead of the recording button for an empty input while the AI state is %s', |
| 86 | + async (aiState) => { |
| 87 | + const channel = await renderComposer(); |
| 88 | + |
| 89 | + await setAIState(channel, aiState); |
| 90 | + |
| 91 | + expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 92 | + expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + it('shows the cooldown timer during a cooldown while the AI is idle', async () => { |
| 97 | + const channel = await renderComposer(); |
| 98 | + |
| 99 | + await startCooldown(channel); |
| 100 | + |
| 101 | + expect(screen.getByTestId(COOLDOWN_TIMER_TEST_ID)).toBeInTheDocument(); |
| 102 | + expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('shows the stop button instead of the cooldown timer while the AI is generating', async () => { |
| 106 | + const channel = await renderComposer(); |
| 107 | + |
| 108 | + await startCooldown(channel); |
| 109 | + await setAIState(channel, AIStates.Generating); |
| 110 | + |
| 111 | + expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 112 | + expect(screen.queryByTestId(COOLDOWN_TIMER_TEST_ID)).not.toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('shows the stop button while the AI is generating even when the send button is hidden', async () => { |
| 116 | + const channel = await renderComposer({ hideSendButton: true }); |
| 117 | + |
| 118 | + expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 119 | + |
| 120 | + await setAIState(channel, AIStates.Generating); |
| 121 | + |
| 122 | + expect(screen.getByTestId(STOP_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('keeps the recording button while the AI is generating when the stop button is removed', async () => { |
| 126 | + const channel = await renderComposer({ overrides: { StopAIGenerationButton: null } }); |
| 127 | + |
| 128 | + await setAIState(channel, AIStates.Generating); |
| 129 | + |
| 130 | + expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 131 | + expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('shows the send button once the input has text', async () => { |
| 135 | + const channel = await renderComposer(); |
| 136 | + |
| 137 | + await act(() => { |
| 138 | + channel.messageComposer.textComposer.setText('Hello'); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(screen.getByTestId('send-button')).toBeInTheDocument(); |
| 142 | + expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('shows a custom send button once the input has text', async () => { |
| 146 | + const CustomSendButton = () => ( |
| 147 | + <button data-testid='custom-send-button' type='button' /> |
| 148 | + ); |
| 149 | + const channel = await renderComposer({ overrides: { SendButton: CustomSendButton } }); |
| 150 | + |
| 151 | + await act(() => { |
| 152 | + channel.messageComposer.textComposer.setText('Hello'); |
| 153 | + }); |
| 154 | + |
| 155 | + expect(screen.getByTestId('custom-send-button')).toBeInTheDocument(); |
| 156 | + expect(screen.queryByTestId(RECORDING_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 157 | + }); |
| 158 | + |
| 159 | + it('goes back to the recording button once the AI stops generating', async () => { |
| 160 | + const channel = await renderComposer(); |
| 161 | + |
| 162 | + await setAIState(channel, AIStates.Generating); |
| 163 | + await setAIState(channel, AIStates.Idle); |
| 164 | + |
| 165 | + expect(screen.getByTestId(RECORDING_BUTTON_TEST_ID)).toBeInTheDocument(); |
| 166 | + expect(screen.queryByTestId(STOP_BUTTON_TEST_ID)).not.toBeInTheDocument(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments