Files
notifications-admin/tests/javascripts/copyToClipboard.test.js

259 lines
6.6 KiB
JavaScript
Raw Permalink Normal View History

2019-08-12 10:24:03 +01:00
const helpers = require('./support/helpers');
afterAll(() => {
require('./support/teardown.js');
// clear up methods in the global space
navigator.clipboard = undefined;
2019-08-12 10:24:03 +01:00
});
describe('copy to clipboard', () => {
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
let apiKey = '00000000-0000-0000-0000-000000000000';
2019-08-12 10:24:03 +01:00
let component;
let clipboardWriteTextMock;
2020-08-25 10:26:36 +01:00
const setUpDOM = function (options) {
// set up DOM
document.body.innerHTML =`
<div data-module="copy-to-clipboard" data-value="${apiKey}" data-thing="${options.thing}" data-name="${options.name}"></div>`;
2020-08-25 10:26:36 +01:00
};
beforeEach(() => {
2020-08-25 10:26:36 +01:00
// Reset the clipboard mock for each test
if (clipboardWriteTextMock) {
clipboardWriteTextMock.mockClear();
}
2019-08-12 10:24:03 +01:00
// mock sticky JS
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.stickAtBottomWhenScrolling = {
2019-08-12 10:24:03 +01:00
recalculate: jest.fn(() => {})
}
});
test("If Clipboard API isn't available, nothing should happen", () => {
2019-08-12 10:24:03 +01:00
// Remove clipboard API
navigator.clipboard = undefined;
2019-08-12 10:24:03 +01:00
require('../../app/assets/javascripts/copyToClipboard.js');
2019-08-12 10:24:03 +01:00
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
2019-08-12 10:24:03 +01:00
component = document.querySelector('[data-module=copy-to-clipboard]');
2019-08-12 10:24:03 +01:00
// start the module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
expect(component.querySelector('button')).toBeNull();
2019-08-12 10:24:03 +01:00
});
describe("If Clipboard API is available", () => {
2019-08-12 10:24:03 +01:00
beforeAll(() => {
// Mock modern Clipboard API BEFORE loading the module
clipboardWriteTextMock = jest.fn(() => Promise.resolve());
navigator.clipboard = {
writeText: clipboardWriteTextMock
};
2019-08-12 10:24:03 +01:00
// force module require to not come from cache
jest.resetModules();
require('../../app/assets/javascripts/copyToClipboard.js');
2019-08-12 10:24:03 +01:00
});
2020-08-25 10:26:36 +01:00
describe("On page load", () => {
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
describe("For all variations of the initial HTML", () => {
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
beforeEach(() => {
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
component = document.querySelector('[data-module=copy-to-clipboard]');
2020-08-25 10:26:36 +01:00
// start the module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
});
2019-08-12 10:24:03 +01:00
test("It should add a button for copying the thing to the clipboard", () => {
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
expect(component.querySelector('button')).not.toBeNull();
expect(component.querySelector('button').textContent.trim()).toContain('Copy Some Thing');
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
});
2019-08-12 10:24:03 +01:00
test("It should add the 'copy-to-clipboard' class", () => {
2019-08-12 10:24:03 +01:00
expect(component.classList.contains('copy-to-clipboard')).toBe(true);
2019-08-12 10:24:03 +01:00
2020-08-25 10:26:36 +01:00
});
test("It should display the value", () => {
2020-08-25 10:26:36 +01:00
expect(component.querySelector('.copy-to-clipboard__value')).not.toBeNull();
expect(component.querySelector('.copy-to-clipboard__value').textContent).toBe(apiKey);
2020-08-25 10:26:36 +01:00
});
2019-08-12 10:24:03 +01:00
test("It should have an aria-live region for screen reader announcements", () => {
const liveRegion = component.querySelector('[aria-live]');
expect(liveRegion).not.toBeNull();
expect(liveRegion.getAttribute('aria-live')).toBe('polite');
expect(liveRegion.getAttribute('aria-atomic')).toBe('true');
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
});
2019-08-12 10:24:03 +01:00
});
2020-08-25 10:26:36 +01:00
describe("If it's one of many in the page", () => {
beforeEach(() => {
setUpDOM({ 'thing': 'ID', 'name': 'Default' });
component = document.querySelector('[data-module=copy-to-clipboard]');
2020-08-25 10:26:36 +01:00
// start the module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2020-08-25 10:26:36 +01:00
});
test("the button should have shorter label and hidden suffix", () => {
2020-08-25 10:26:36 +01:00
const button = component.querySelector('button');
expect(button.textContent).toContain('Copy ID');
2020-08-25 10:26:36 +01:00
const buttonSuffix = button.querySelector('.usa-sr-only');
2020-08-25 10:26:36 +01:00
expect(buttonSuffix).not.toBeNull();
expect(buttonSuffix.textContent).toEqual(' for Default');
});
2019-08-12 10:24:03 +01:00
});
2020-08-25 10:26:36 +01:00
describe("If it's the only one on the page", () => {
beforeEach(() => {
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
2020-08-25 10:26:36 +01:00
component = document.querySelector('[data-module=copy-to-clipboard]');
2020-08-25 10:26:36 +01:00
// start the module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2020-08-25 10:26:36 +01:00
});
test("the button should have full label without extra suffix", () => {
2020-08-25 10:26:36 +01:00
const button = component.querySelector('button');
expect(button.textContent).toContain('Copy Some Thing to clipboard');
expect(button.textContent).not.toContain(' for ');
2020-08-25 10:26:36 +01:00
})
});
2019-08-12 10:24:03 +01:00
});
describe("If you click the 'Copy' button", () => {
2019-08-12 10:24:03 +01:00
beforeEach(async () => {
2019-08-12 10:24:03 +01:00
jest.useFakeTimers();
2019-08-12 10:24:03 +01:00
setUpDOM({ 'thing': 'API key', 'name': 'API key' });
2019-08-12 10:24:03 +01:00
component = document.querySelector('[data-module=copy-to-clipboard]');
2019-08-12 10:24:03 +01:00
// start the module
window.NotifyModules.start();
2019-08-12 10:24:03 +01:00
const button = component.querySelector('button');
helpers.triggerEvent(button, 'click');
2020-08-25 10:26:36 +01:00
// Wait for async clipboard operation
await Promise.resolve();
2019-08-12 10:24:03 +01:00
});
afterEach(() => {
jest.useRealTimers();
});
2019-08-12 10:24:03 +01:00
test("The value should still be visible", () => {
2019-08-12 10:24:03 +01:00
expect(component.querySelector('.copy-to-clipboard__value')).not.toBeNull();
expect(component.querySelector('.copy-to-clipboard__value').textContent).toBe(apiKey);
2019-08-12 10:24:03 +01:00
});
2019-08-12 10:24:03 +01:00
test("The button text should change to 'Copied!'", () => {
const button = component.querySelector('button');
expect(button.textContent).toContain('Copied!');
});
2020-08-25 10:26:36 +01:00
test("The button should be disabled", () => {
2019-08-12 10:24:03 +01:00
const button = component.querySelector('button');
expect(button.disabled).toBe(true);
});
test("Screen reader announcement should confirm the copy", () => {
const liveRegion = component.querySelector('[aria-live]');
expect(liveRegion.textContent).toBe('API key copied to clipboard');
2019-08-12 10:24:03 +01:00
});
test("It should copy the value to clipboard using Clipboard API", () => {
2019-08-12 10:24:03 +01:00
expect(clipboardWriteTextMock).toHaveBeenCalledWith(apiKey);
2019-08-12 10:24:03 +01:00
});
2019-08-12 10:24:03 +01:00
test("After 2 seconds, button should reset to original text", () => {
2019-08-12 10:24:03 +01:00
const button = component.querySelector('button');
2019-08-12 10:24:03 +01:00
jest.advanceTimersByTime(2000);
2019-08-12 10:24:03 +01:00
expect(button.textContent).toContain('Copy API key');
expect(button.textContent).not.toContain('Copied!');
expect(button.disabled).toBe(false);
2019-08-12 10:24:03 +01:00
});
2019-08-12 10:24:03 +01:00
test("After 2 seconds, screen reader announcement should clear", () => {
const liveRegion = component.querySelector('[aria-live]');
jest.advanceTimersByTime(2000);
expect(liveRegion.textContent).toBe('');
2019-08-12 10:24:03 +01:00
});
});
});
});