From f1aca518f768bef90daf864a6ed4a59a8442f57b Mon Sep 17 00:00:00 2001 From: martincupela Date: Mon, 28 Oct 2024 17:12:56 +0100 Subject: [PATCH] test: add AttachmentSelector tests --- .../__tests__/AttachmentSelector.test.js | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 src/components/MessageInput/__tests__/AttachmentSelector.test.js diff --git a/src/components/MessageInput/__tests__/AttachmentSelector.test.js b/src/components/MessageInput/__tests__/AttachmentSelector.test.js new file mode 100644 index 000000000..bb39dfc9b --- /dev/null +++ b/src/components/MessageInput/__tests__/AttachmentSelector.test.js @@ -0,0 +1,164 @@ +import React from 'react'; +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; +import '@testing-library/jest-dom'; +import { MessageInput } from '../MessageInput'; +import { Chat } from '../../Chat'; +import { + ChannelActionProvider, + ChannelStateProvider, + ComponentProvider, + TranslationProvider, +} from '../../../context'; +import { initClientWithChannels } from '../../../mock-builders'; +import { CHANNEL_CONTAINER_ID } from '../../Channel/constants'; +import { AttachmentSelector } from '../AttachmentSelector'; + +const ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID = 'attachment-selector-actions-menu'; +const POLL_CREATION_DIALOG_TEST_ID = 'poll-creation-dialog'; + +const UPLOAD_FILE_BUTTON_CLASS = 'str-chat__attachment-selector-actions-menu__upload-file-button'; +const CREATE_POLL_BUTTON_CLASS = 'str-chat__attachment-selector-actions-menu__create-poll-button'; + +const translationContext = { + t: (v) => v, +}; + +const defaultChannelStateContext = { + channelCapabilities: { 'send-poll': true, 'upload-file': true }, +}; + +const defaultMessageInputProps = { + isThreadInput: false, +}; + +const invokeMenu = async () => { + await act(async () => { + await fireEvent.click(screen.getByTestId('invoke-attachment-selector-button')); + }); +}; + +const renderComponent = async ({ + channelStateContext, + componentContext, + messageInputProps, +} = {}) => { + const { + channels: [channel], + client, + } = await initClientWithChannels(); + let result; + await act(() => { + result = render( + + + + + +
+ +
+
+
+
+
+
, + ); + }); + return result; +}; + +describe('AttachmentSelector', () => { + it('renders with upload file button and poll button if send-poll and upload-file permissions are granted', async () => { + await renderComponent(); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + expect(menu).toBeInTheDocument(); + expect(menu).toHaveTextContent('File'); + expect(menu).toHaveTextContent('Poll'); + }); + + it('renders with single upload file button if send-poll permission is not granted', async () => { + await renderComponent({ + channelStateContext: { channelCapabilities: { 'upload-file': true } }, + }); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + expect(menu).toBeInTheDocument(); + expect(menu).toHaveTextContent('File'); + expect(menu).not.toHaveTextContent('Poll'); + }); + + it('renders with single upload file button if rendered in a thread', async () => { + await renderComponent({ + messageInputProps: { isThreadInput: true }, + }); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + expect(menu).toBeInTheDocument(); + expect(menu).toHaveTextContent('File'); + expect(menu).not.toHaveTextContent('Poll'); + }); + + it('renders with single poll button if upload-file permission is not granted', async () => { + await renderComponent({ + channelStateContext: { channelCapabilities: { 'send-poll': true } }, + }); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + expect(menu).toBeInTheDocument(); + expect(menu).not.toHaveTextContent('File'); + expect(menu).toHaveTextContent('Poll'); + }); + + it('does not render the invoke button if send-poll and upload-file permission is not granted', async () => { + await renderComponent({ + channelStateContext: { channelCapabilities: {} }, + }); + expect(screen.queryByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID)).not.toBeInTheDocument(); + }); + + it('opens poll creation dialog if Poll option is selected and closes the attachment selector menu', async () => { + await renderComponent(); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + + const createPollButton = menu.querySelector(`.${CREATE_POLL_BUTTON_CLASS}`); + expect(createPollButton).toBeInTheDocument(); + fireEvent.click(createPollButton); + await waitFor(() => { + expect(menu).not.toBeInTheDocument(); + expect(screen.queryByTestId(POLL_CREATION_DIALOG_TEST_ID)).toBeInTheDocument(); + }); + }); + + it('is closed if File menu button is clicked', async () => { + await renderComponent(); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + const uploadFileMenuBtn = menu.querySelector(`.${UPLOAD_FILE_BUTTON_CLASS}`); + expect(uploadFileMenuBtn).toBeInTheDocument(); + fireEvent.click(uploadFileMenuBtn); + await waitFor(() => { + expect(menu).not.toBeInTheDocument(); + }); + }); + + it('renders custom menu actions if provided', async () => { + const customText = 'Custom text'; + const CustomItem = () =>
{customText}
; + const CustomAttachmentSelector = () => ( + + ); + await renderComponent({ componentContext: { AttachmentSelector: CustomAttachmentSelector } }); + await invokeMenu(); + const menu = screen.getByTestId(ATTACHMENT_SELECTOR__ACTIONS_MENU_TEST_ID); + expect(menu).toBeInTheDocument(); + expect(menu).toHaveTextContent(customText); + expect(menu).not.toHaveTextContent('File'); + expect(menu).not.toHaveTextContent('Poll'); + }); +});