diff --git a/modules/apps/content-dashboard/content-dashboard-web/test/js/components/FileUrlCopyButton.test.js b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/FileUrlCopyButton.test.js new file mode 100644 index 00000000000000..3f54e33e1cbe11 --- /dev/null +++ b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/FileUrlCopyButton.test.js @@ -0,0 +1,81 @@ +/* eslint-disable no-unused-vars */ +/* eslint-disable no-import-assign */ +/** + * Copyright (c) 2000-present Liferay, Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + */ + +import {cleanup, fireEvent, render} from '@testing-library/react'; +import React from 'react'; + +import '@testing-library/jest-dom/extend-expect'; +import {act} from 'react-dom/test-utils'; + +import FileUrlCopyButton from '../../../src/main/resources/META-INF/resources/js/components/FileUrlCopyButton'; + +const demoFileUrl = 'http://localhost:8080/documents/my-demo-url.txt'; + +describe('FileUrlCopyButton', () => { + beforeEach(() => { + jest.useFakeTimers(); + + global.navigator.writeText = jest.fn(); + Liferay.component = jest.fn(); + }); + + afterEach(() => { + jest.runOnlyPendingTimers(); + jest.useRealTimers(); + + jest.restoreAllMocks(); + cleanup(); + }); + + it('renders an input and a button with the proper value and the initial UI', () => { + const {getByDisplayValue, getByRole} = render( + + ); + + expect(getByDisplayValue(demoFileUrl)).toBeInTheDocument(); + + const button = getByRole('button'); + expect(button).toBeInTheDocument(); + + const icon = button.getElementsByTagName('svg')[0]; + expect(icon.classList).toContain('lexicon-icon-copy'); + }); + + it('renders the proper icon after clicking the button', async () => { + const {getByRole} = render(); + const button = getByRole('button'); + + fireEvent( + button, + new MouseEvent('click', { + bubbles: true, + cancelable: true, + }) + ); + + let icon = button.getElementsByTagName('svg')[0]; + expect(icon.classList).toContain('lexicon-icon-check-small'); + + act(() => { + jest.runAllTimers(); + }); + + icon = button.getElementsByTagName('svg')[0]; + expect(icon.classList).toContain('lexicon-icon-copy'); + + expect(Liferay.component).toHaveBeenCalledTimes(1); + }); +}); diff --git a/modules/apps/content-dashboard/content-dashboard-web/test/js/components/SidebarPanelInfoView.test.js b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/SidebarPanelInfoView.test.js index 2f5a2071a3a974..92da9e68c0ea8f 100644 --- a/modules/apps/content-dashboard/content-dashboard-web/test/js/components/SidebarPanelInfoView.test.js +++ b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/SidebarPanelInfoView.test.js @@ -132,7 +132,6 @@ describe('SidebarPanelInfoView', () => { expect(getByText('Mocked description')).toBeInTheDocument(); expect(getByText('download')).toBeInTheDocument(); expect(getByText('size')).toBeInTheDocument(); - expect(getByText('url')).toBeInTheDocument(); expect( queryByText('languages-translated-into') @@ -175,6 +174,10 @@ describe('SidebarPanelInfoView', () => { expect(previewFigureTag).toBe(null); + expect( + container.getElementsByClassName('lexicon-icon-copy').length + ).toBe(1); + expect(getByText('Basic Document')).toBeInTheDocument(); expect(getByText('download')).toBeInTheDocument(); diff --git a/modules/apps/content-dashboard/content-dashboard-web/test/js/components/__snapshots__/SidebarPanelInfoView.test.js.snap b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/__snapshots__/SidebarPanelInfoView.test.js.snap index 22b95056e3ca97..dadbf666844221 100644 --- a/modules/apps/content-dashboard/content-dashboard-web/test/js/components/__snapshots__/SidebarPanelInfoView.test.js.snap +++ b/modules/apps/content-dashboard/content-dashboard-web/test/js/components/__snapshots__/SidebarPanelInfoView.test.js.snap @@ -44,10 +44,10 @@ exports[`SidebarPanelInfoView renders 1`] = ` class="sidebar-body" >