From 098ef330364cc468511c84595558a029a2f72f88 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Tue, 10 Dec 2024 14:32:13 +0100 Subject: [PATCH 1/4] chore: write tests for disallowed sending messages --- .../__tests__/MessageInput.test.js | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/package/src/components/MessageInput/__tests__/MessageInput.test.js b/package/src/components/MessageInput/__tests__/MessageInput.test.js index 68b8c1681..3f5d40230 100644 --- a/package/src/components/MessageInput/__tests__/MessageInput.test.js +++ b/package/src/components/MessageInput/__tests__/MessageInput.test.js @@ -2,7 +2,7 @@ import React from 'react'; import { Alert } from 'react-native'; -import { cleanup, fireEvent, render, userEvent, waitFor } from '@testing-library/react-native'; +import { act, cleanup, fireEvent, render, userEvent, waitFor } from '@testing-library/react-native'; import * as AttachmentPickerUtils from '../../../contexts/attachmentPickerContext/AttachmentPickerContext'; import { OverlayProvider } from '../../../contexts/overlayContext/OverlayProvider'; @@ -188,4 +188,83 @@ describe('MessageInput', () => { expect(Alert.alert).toHaveBeenCalledWith('Hold to start recording.'); }); }); + + it('should render the SendMessageDisallowedIndicator if the send-message capability is not present', async () => { + await initializeChannel(generateChannelResponse()); + + const { queryByTestId } = render( + + + + + , + ); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); + }); + + act(() => { + chatClient.dispatchEvent({ + cid: channel.data.cid, + own_capabilities: channel.data.own_capabilities.filter( + (capability) => capability !== 'send-message', + ), + type: 'capabilities.changed', + }); + }); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeTruthy(); + }); + }); + + it('should not render the SendMessageDisallowedIndicator if the channel is frozen and the send-message capability is present', async () => { + await initializeChannel(generateChannelResponse({ channel: { frozen: true } })); + + const { queryByTestId } = render( + + + + + , + ); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); + }); + }); + + it('should render the SendMessageDisallowedIndicator only if the send-message capability is not present', async () => { + await initializeChannel(generateChannelResponse({ channel: { frozen: true } })); + + const { queryByTestId } = render( + + + + + , + ); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); + }); + + act(() => { + chatClient.dispatchEvent({ + channel: { + ...channel.data, + own_capabilities: channel.data.own_capabilities.filter( + (capability) => capability !== 'send-message', + ), + }, + cid: channel.data.cid, + type: 'channel.updated', + }); + }); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeTruthy(); + }); + }); }); From 8af38d7c7b06826a989938ef93e44f6dbe68dccd Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Tue, 10 Dec 2024 14:39:17 +0100 Subject: [PATCH 2/4] fix: make test name more concise --- .../src/components/MessageInput/__tests__/MessageInput.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/src/components/MessageInput/__tests__/MessageInput.test.js b/package/src/components/MessageInput/__tests__/MessageInput.test.js index 3f5d40230..78e91caed 100644 --- a/package/src/components/MessageInput/__tests__/MessageInput.test.js +++ b/package/src/components/MessageInput/__tests__/MessageInput.test.js @@ -235,7 +235,7 @@ describe('MessageInput', () => { }); }); - it('should render the SendMessageDisallowedIndicator only if the send-message capability is not present', async () => { + it('should render the SendMessageDisallowedIndicator in a frozen channel only if the send-message capability is not present', async () => { await initializeChannel(generateChannelResponse({ channel: { frozen: true } })); const { queryByTestId } = render( From 118302da67a5fd48c94a9d7016a18344a26b6387 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Tue, 10 Dec 2024 14:42:29 +0100 Subject: [PATCH 3/4] chore: remove redundant assertion --- .../components/MessageInput/__tests__/MessageInput.test.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/package/src/components/MessageInput/__tests__/MessageInput.test.js b/package/src/components/MessageInput/__tests__/MessageInput.test.js index 78e91caed..f61632143 100644 --- a/package/src/components/MessageInput/__tests__/MessageInput.test.js +++ b/package/src/components/MessageInput/__tests__/MessageInput.test.js @@ -246,10 +246,6 @@ describe('MessageInput', () => { , ); - await waitFor(() => { - expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); - }); - act(() => { chatClient.dispatchEvent({ channel: { From 6622c1f45d9e15ac9414b36a9e5d077066b27522 Mon Sep 17 00:00:00 2001 From: Ivan Sekovanikj Date: Tue, 10 Dec 2024 16:39:25 +0100 Subject: [PATCH 4/4] chore: add test for the editing state as well --- .../__tests__/MessageInput.test.js | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/package/src/components/MessageInput/__tests__/MessageInput.test.js b/package/src/components/MessageInput/__tests__/MessageInput.test.js index f61632143..c76e38fdf 100644 --- a/package/src/components/MessageInput/__tests__/MessageInput.test.js +++ b/package/src/components/MessageInput/__tests__/MessageInput.test.js @@ -1,9 +1,10 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { Alert } from 'react-native'; import { act, cleanup, fireEvent, render, userEvent, waitFor } from '@testing-library/react-native'; +import { useMessagesContext } from '../../../contexts'; import * as AttachmentPickerUtils from '../../../contexts/attachmentPickerContext/AttachmentPickerContext'; import { OverlayProvider } from '../../../contexts/overlayContext/OverlayProvider'; import { getOrCreateChannelApi } from '../../../mock-builders/api/getOrCreateChannel'; @@ -263,4 +264,42 @@ describe('MessageInput', () => { expect(queryByTestId('send-message-disallowed-indicator')).toBeTruthy(); }); }); + + const EditingStateMessageInput = () => { + const { setEditingState } = useMessagesContext(); + useEffect(() => { + setEditingState({ id: 'some-message-id' }); + }, []); + return ; + }; + + it('should not render the SendMessageDisallowedIndicator if we are editing a message, regardless of capabilities', async () => { + await initializeChannel(generateChannelResponse()); + + const { queryByTestId } = render( + + + + + , + ); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); + }); + + act(() => { + chatClient.dispatchEvent({ + cid: channel.data.cid, + own_capabilities: channel.data.own_capabilities.filter( + (capability) => capability !== 'send-message', + ), + type: 'capabilities.changed', + }); + }); + + await waitFor(() => { + expect(queryByTestId('send-message-disallowed-indicator')).toBeNull(); + }); + }); });