Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: write tests for disallowed sending messages #2839

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 116 additions & 2 deletions package/src/components/MessageInput/__tests__/MessageInput.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useEffect } 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 { useMessagesContext } from '../../../contexts';
import * as AttachmentPickerUtils from '../../../contexts/attachmentPickerContext/AttachmentPickerContext';
import { OverlayProvider } from '../../../contexts/overlayContext/OverlayProvider';
import { getOrCreateChannelApi } from '../../../mock-builders/api/getOrCreateChannel';
Expand Down Expand Up @@ -188,4 +189,117 @@ 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(
<Chat client={chatClient}>
<Channel audioRecordingEnabled channel={channel}>
<MessageInput />
</Channel>
</Chat>,
);

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(
<Chat client={chatClient}>
<Channel audioRecordingEnabled channel={channel}>
<MessageInput />
</Channel>
</Chat>,
);

await waitFor(() => {
expect(queryByTestId('send-message-disallowed-indicator')).toBeNull();
});
});

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(
<Chat client={chatClient}>
<Channel audioRecordingEnabled channel={channel}>
<MessageInput />
</Channel>
</Chat>,
);

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();
});
});

const EditingStateMessageInput = () => {
const { setEditingState } = useMessagesContext();
useEffect(() => {
setEditingState({ id: 'some-message-id' });
}, []);
return <MessageInput />;
};

it('should not render the SendMessageDisallowedIndicator if we are editing a message, regardless of capabilities', async () => {
await initializeChannel(generateChannelResponse());

const { queryByTestId } = render(
<Chat client={chatClient}>
<Channel audioRecordingEnabled channel={channel}>
<EditingStateMessageInput />
</Channel>
</Chat>,
);

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();
});
});
});
Loading