forked from liferay/liferay-portal
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LPS-137687 Unit testing for FileUrlCopyButton plus fixes
- Loading branch information
Beltran Rengifo
committed
Oct 18, 2021
1 parent
6919392
commit 4005ca9
Showing
4 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...apps/content-dashboard/content-dashboard-web/test/js/components/FileUrlCopyButton.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters