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

[RTE] Unit tests for RichTextSendBox #4551

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import React from 'react';
import { RichTextSendBox } from './RichTextSendBox';
/* @conditional-compile-remove(rich-text-editor) */
import { renderWithLocalization, createTestLocale } from '../utils/testUtils';
import { render, waitFor, fireEvent } from '@testing-library/react';
import { screen } from '@testing-library/react';
import { render, waitFor, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { registerIcons } from '@fluentui/react';

Expand All @@ -28,6 +27,58 @@ const icons: {
chevrondown: <></>
};

describe('RichTextSendBox should only call onSendMessage when there is content and it is not disabled', () => {
beforeAll(() => {
registerIcons({
icons: icons
});
});
test('onSendMessage should not be called when message is empty', async () => {
let called = false;
render(
<RichTextSendBox
onSendMessage={async (): Promise<void> => {
called = true;
return Promise.resolve();
}}
/>
);
// Find and click the send button
const sendButton = await screen.findByRole('button', {
name: 'Send message'
});
fireEvent.click(sendButton);
// Check that onSendMessage was not called
expect(called).toBeFalsy();
});
test('onSendMessage should not be called when disabled', async () => {
let called = false;
render(
<RichTextSendBox
disabled={true}
onSendMessage={async (): Promise<void> => {
called = true;
return Promise.resolve();
}}
/>
);
// Find the input field
const editorDiv = screen.queryByTestId('rooster-rich-text-editor');
// fix for an issue when contentEditable is not set to RoosterJS for tests
editorDiv?.setAttribute('contentEditable', 'true');
if (editorDiv === null) {
fail('Editor div not found');
}
// Find and click the send button
const sendButton = await screen.findByRole('button', {
name: 'Send message'
});
fireEvent.click(sendButton);
// Check that onSendMessage was not called
expect(called).toBeFalsy();
});
});

describe('RichTextSendBox should return text correctly', () => {
beforeAll(() => {
registerIcons({
Expand Down