From 53fce572aad1f434323b812c02aa8fd634d7f158 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Thu, 13 Oct 2022 18:53:20 +0200 Subject: [PATCH] Add FormattingButtons test --- .../FormattingButtons-test.tsx | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/components/views/rooms/wysiwyg_composer/FormattingButtons-test.tsx diff --git a/test/components/views/rooms/wysiwyg_composer/FormattingButtons-test.tsx b/test/components/views/rooms/wysiwyg_composer/FormattingButtons-test.tsx new file mode 100644 index 0000000000..da3cfa887c --- /dev/null +++ b/test/components/views/rooms/wysiwyg_composer/FormattingButtons-test.tsx @@ -0,0 +1,77 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React from 'react'; +import { render, screen } from "@testing-library/react"; +import userEvent from '@testing-library/user-event'; + +import { FormattingButtons } from "../../../../../src/components/views/rooms/wysiwyg_composer/FormattingButtons"; + +describe('FormattingButtons', () => { + const wysiwyg = { + bold: jest.fn(), + italic: jest.fn(), + underline: jest.fn(), + strikeThrough: jest.fn(), + } as any; + + const formattingStates = { + bold: 'reversed', + italic: 'reversed', + underline: 'enabled', + strikeThrough: 'enabled', + } as any; + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('Should have the correspond CSS classes', () => { + // When + render(); + + // Then + expect(screen.getByLabelText('Bold')).toHaveClass('mx_FormattingButtons_active'); + expect(screen.getByLabelText('Italic')).toHaveClass('mx_FormattingButtons_active'); + expect(screen.getByLabelText('Underline')).not.toHaveClass('mx_FormattingButtons_active'); + expect(screen.getByLabelText('Strikethrough')).not.toHaveClass('mx_FormattingButtons_active'); + }); + + it('Should call wysiwyg function on button click', () => { + // When + render(); + screen.getByLabelText('Bold').click(); + screen.getByLabelText('Italic').click(); + screen.getByLabelText('Underline').click(); + screen.getByLabelText('Strikethrough').click(); + + // Then + expect(wysiwyg.bold).toHaveBeenCalledTimes(1); + expect(wysiwyg.italic).toHaveBeenCalledTimes(1); + expect(wysiwyg.underline).toHaveBeenCalledTimes(1); + expect(wysiwyg.strikeThrough).toHaveBeenCalledTimes(1); + }); + + it('Should display the tooltip on mouse over', async () => { + // When + const user = userEvent.setup(); + render(); + await user.hover(screen.getByLabelText('Bold')); + + // Then + expect(await screen.findByText('Bold')).toBeTruthy(); + }); +});