|
6 | 6 | generateUser, |
7 | 7 | initClientWithChannels, |
8 | 8 | } from '../../../mock-builders'; |
9 | | -import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 9 | +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
10 | 10 | import { ChatProvider, MessageProvider, useChannelActionContext } from '../../../context'; |
11 | 11 | import { Channel } from '../../Channel'; |
12 | 12 | import { MessageActionsBox } from '../../MessageActions'; |
@@ -78,7 +78,9 @@ const setup = async ({ channelData } = {}) => { |
78 | 78 | const sendFileSpy = jest.spyOn(customChannel, 'sendFile').mockResolvedValueOnce({ |
79 | 79 | file: fileUploadUrl, |
80 | 80 | }); |
81 | | - const getDraftSpy = jest.spyOn(customChannel, 'getDraft').mockResolvedValue({}); |
| 81 | + const getDraftSpy = jest |
| 82 | + .spyOn(customChannel, 'getDraft') |
| 83 | + .mockResolvedValue({ draft: { message: { id: 'x' } } }); |
82 | 84 | customChannel.initialized = true; |
83 | 85 | customClient.activeChannels[customChannel.cid] = customChannel; |
84 | 86 | return { customChannel, customClient, getDraftSpy, sendFileSpy, sendImageSpy }; |
@@ -157,13 +159,68 @@ const renderComponent = async ({ |
157 | 159 | }; |
158 | 160 |
|
159 | 161 | describe('MessageInput in Thread', () => { |
| 162 | + describe('draft', () => { |
| 163 | + it('is queried when drafts are enabled', async () => { |
| 164 | + const { customChannel, customClient, getDraftSpy } = await setup(); |
| 165 | + await act(() => { |
| 166 | + customClient.setMessageComposerSetupFunction(({ composer }) => { |
| 167 | + composer.updateConfig({ drafts: { enabled: true } }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + await renderComponent({ |
| 171 | + customChannel, |
| 172 | + customClient, |
| 173 | + }); |
| 174 | + expect(getDraftSpy).toHaveBeenCalledTimes(1); |
| 175 | + }); |
| 176 | + it('prevents querying if composition is not empty', async () => { |
| 177 | + const { customChannel, customClient, getDraftSpy } = await setup(); |
| 178 | + await act(() => { |
| 179 | + customClient.setMessageComposerSetupFunction(({ composer }) => { |
| 180 | + composer.updateConfig({ drafts: { enabled: true } }); |
| 181 | + composer.textComposer.setText('abc'); |
| 182 | + }); |
| 183 | + }); |
| 184 | + await renderComponent({ |
| 185 | + customChannel, |
| 186 | + customClient, |
| 187 | + }); |
| 188 | + expect(getDraftSpy).not.toHaveBeenCalled(); |
| 189 | + }); |
| 190 | + it('prevents querying if not rendered inside a thread', async () => { |
| 191 | + const { customChannel, customClient, getDraftSpy } = await setup(); |
| 192 | + await act(() => { |
| 193 | + customClient.setMessageComposerSetupFunction(({ composer }) => { |
| 194 | + composer.updateConfig({ drafts: { enabled: true } }); |
| 195 | + composer.compositionContext = customChannel; |
| 196 | + }); |
| 197 | + }); |
| 198 | + await renderComponent({ |
| 199 | + customChannel, |
| 200 | + customClient, |
| 201 | + }); |
| 202 | + expect(getDraftSpy).not.toHaveBeenCalled(); |
| 203 | + }); |
| 204 | + it('prevents querying if drafts are disabled (default)', async () => { |
| 205 | + const { customChannel, customClient, getDraftSpy } = await setup(); |
| 206 | + await renderComponent({ |
| 207 | + customChannel, |
| 208 | + customClient, |
| 209 | + }); |
| 210 | + expect(getDraftSpy).not.toHaveBeenCalled(); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
160 | 214 | it('renders in the thread context for direct messaging channel', async () => { |
161 | 215 | const { customChannel, customClient } = await setup(); |
162 | 216 | await renderComponent({ |
163 | 217 | customChannel, |
164 | 218 | customClient, |
165 | 219 | }); |
166 | | - expect(screen.getByLabelText('Also send as a direct message')).toBeInTheDocument(); |
| 220 | + |
| 221 | + await waitFor(() => { |
| 222 | + expect(screen.getByLabelText('Also send as a direct message')).toBeInTheDocument(); |
| 223 | + }); |
167 | 224 | }); |
168 | 225 | it('renders in the thread context for non-direct messaging channel', async () => { |
169 | 226 | const mainListMessage = generateMessage({ cid, user }); |
|
0 commit comments