Skip to content

Commit

Permalink
LPS-137687 Unit testing for FileUrlCopyButton plus fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Beltran Rengifo committed Oct 18, 2021
1 parent 6919392 commit 4005ca9
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
@@ -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(
<FileUrlCopyButton url={demoFileUrl} />
);

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(<FileUrlCopyButton url={demoFileUrl} />);
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);
});
});
Expand Up @@ -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')
Expand Down Expand Up @@ -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();

Expand Down
Expand Up @@ -44,10 +44,10 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="sidebar-body"
>
<div
class="mb-2 sidebar-section"
class="c-mb-2 sidebar-section"
>
<p
class="mb-1 text-secondary"
class="c-mb-1 text-secondary"
>
Basic Web Content
</p>
Expand Down Expand Up @@ -112,7 +112,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
</span>
</span>
<span
class="ml-2 text-secondary"
class="c-ml-2 text-secondary"
>
Kate Williams
</span>
Expand All @@ -126,7 +126,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
categories
</h5>
Expand Down Expand Up @@ -209,7 +209,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
tags
</h5>
Expand Down Expand Up @@ -243,7 +243,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
display-date
</h5>
Expand All @@ -257,7 +257,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
creation-date
</h5>
Expand All @@ -271,7 +271,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
modified-date
</h5>
Expand All @@ -285,7 +285,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
expiration-date
</h5>
Expand All @@ -299,7 +299,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
review-date
</h5>
Expand All @@ -313,7 +313,7 @@ exports[`SidebarPanelInfoView renders 1`] = `
class="c-mb-4 sidebar-dl sidebar-section"
>
<h5
class="font-weight-semi-bold mb-1"
class="c-mb-1 font-weight-semi-bold"
>
id
</h5>
Expand Down
Expand Up @@ -142,6 +142,7 @@ export const mockedFileDocumentProps = {
extension: 'sh',
fileName: 'script.sh',
previewImageURL: '',
previewURL: 'mocked/download/url/demo.jpg&download=true',
size: '9 KB',
viewURL: 'mocked/view/url/in/portal',
},
Expand Down

0 comments on commit 4005ca9

Please sign in to comment.