Skip to content

Commit

Permalink
[TASK] Add test cases for clipboard.js
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler committed Jan 9, 2022
1 parent dad9b48 commit 770a9d4
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions cypress/integration/clipboard_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* This file is part of the Symfony project "eliashaeussler/typo3-badges".
*
* Copyright (C) 2022 Elias Häußler <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @author Elias Häußler <[email protected]>
* @license GPL-3.0-or-later
*/
describe('Clipboard Test', { browser: 'electron' }, () => {
beforeEach(() => {
cy.visit('/');

cy.get('details[open] > .badge-endpoint-example:not(.hidden) .group')
.as('container');

cy.get('@container')
.find('.clipboard-btn')
.as('button');
});

it('hides clipboard button by default', () => {
cy.get('@button')
.should('not.be.visible');
});

it('makes clipboard button visible on hovering parent', () => {
cy.get('@container')
.realHover();

cy.get('@button')
.should('be.visible');
});

it('should always make clipboard button visible on touch devices', () => {
cy.get('html')
.then(($el) => {
$el.addClass('touch');
$el.removeClass('no-touch');
});

cy.get('@button')
.should('be.visible');
});

it('copies text to the clipboard', () => {
let clipboardText;

cy.get('@container')
.realHover();

cy.get('@button')
.click()
.should('be.focused');

cy.window()
.its('navigator.clipboard')
.invoke('readText')
.then((text) => {
clipboardText = text;
});

cy.get('@container')
.should(($subject) => {
expect($subject.text().trim()).to.eq(clipboardText);
});
});

it('restores clipboard button 2 seconds after copy', () => {
cy.get('@container')
.realHover();

cy.clock();

cy.get('@button')
.click();

cy.tick(2000);

cy.get('@button')
.should('not.be.focused');
});
});

0 comments on commit 770a9d4

Please sign in to comment.