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
|
|
|
|
|
document.queryCommandSupported = undefined;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +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 thing;
|
|
|
|
|
let component;
|
2020-08-25 10:26:36 +01:00
|
|
|
let selectionMock;
|
|
|
|
|
let rangeMock;
|
|
|
|
|
|
|
|
|
|
const setUpDOM = function (options) {
|
|
|
|
|
|
|
|
|
|
// set up DOM
|
|
|
|
|
document.body.innerHTML =`
|
2021-08-27 17:32:06 +01:00
|
|
|
<h2 class="copy-to-clipboard__name">
|
2020-08-25 10:26:36 +01:00
|
|
|
${options.thing}
|
|
|
|
|
</h2>
|
2021-08-27 17:32:06 +01:00
|
|
|
<div data-module="copy-to-clipboard" data-value="${apiKey}" data-thing="${options.thing}" data-name="${options.name}">
|
2023-06-16 14:55:24 -04:00
|
|
|
<span class="copy-to-clipboard__value" aria-live="assertive">${(options.name === options.thing) ? '<span class="usa-sr-only">' + options.thing + ': </span>' : ''}${apiKey}</span>
|
2021-08-27 17:32:06 +01:00
|
|
|
<span class="copy-to-clipboard__notice" aria-live="assertive"></span>
|
2020-08-25 10:26:36 +01:00
|
|
|
</div>`;
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-15 12:53:58 +01:00
|
|
|
beforeEach(() => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
// mock objects used to manipulate the page selection
|
|
|
|
|
selectionMock = new helpers.SelectionMock(jest);
|
|
|
|
|
rangeMock = new helpers.RangeMock(jest);
|
|
|
|
|
|
|
|
|
|
// plug gaps in JSDOM's API for manipulation of selections
|
|
|
|
|
window.getSelection = jest.fn(() => selectionMock);
|
|
|
|
|
document.createRange = jest.fn(() => rangeMock);
|
|
|
|
|
|
|
|
|
|
// plug JSDOM not having execCommand
|
|
|
|
|
document.execCommand = jest.fn(() => {});
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
// mock sticky JS
|
|
|
|
|
window.GOVUK.stickAtBottomWhenScrolling = {
|
|
|
|
|
recalculate: jest.fn(() => {})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("If copy command isn't available, nothing should happen", () => {
|
|
|
|
|
|
|
|
|
|
// fake support for the copy command not being available
|
|
|
|
|
document.queryCommandSupported = jest.fn(command => false);
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
require('../../app/assets/javascripts/copyToClipboard.js');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(component.querySelector('button')).toBeNull();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("If copy command is available", () => {
|
|
|
|
|
|
2019-08-20 13:45:56 +01:00
|
|
|
let componentHeightOnLoad;
|
|
|
|
|
|
2019-08-12 10:24:03 +01:00
|
|
|
beforeAll(() => {
|
|
|
|
|
|
|
|
|
|
// assume copy command is available
|
|
|
|
|
document.queryCommandSupported = jest.fn(command => command === 'copy');
|
|
|
|
|
|
|
|
|
|
// force module require to not come from cache
|
|
|
|
|
jest.resetModules();
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
require('../../app/assets/javascripts/copyToClipboard.js');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
afterEach(() => {
|
|
|
|
|
screenMock.reset();
|
|
|
|
|
});
|
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(() => {
|
2019-11-08 12:40:54 +00:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
|
2019-11-08 12:40:54 +00:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2019-08-20 13:45:56 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// set default style for component height (queried by jQuery before checking DOM APIs)
|
|
|
|
|
const stylesheet = document.createElement('style');
|
2021-08-27 17:32:06 +01:00
|
|
|
stylesheet.innerHTML = '[data-module=copy-to-clipboard] { height: auto; }'; // set to browser default
|
2020-08-25 10:26:36 +01:00
|
|
|
document.getElementsByTagName('head')[0].appendChild(stylesheet);
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
componentHeightOnLoad = 50;
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// mock the DOM APIs called for the position & dimension of the component
|
|
|
|
|
screenMock = new helpers.ScreenMock(jest);
|
|
|
|
|
screenMock.setWindow({
|
|
|
|
|
width: 1990,
|
|
|
|
|
height: 940,
|
|
|
|
|
scrollTop: 0
|
|
|
|
|
});
|
|
|
|
|
screenMock.mockPositionAndDimension('component', component, {
|
|
|
|
|
offsetHeight: componentHeightOnLoad,
|
|
|
|
|
offsetWidth: 641,
|
|
|
|
|
offsetTop: 0
|
|
|
|
|
});
|
2019-11-08 12:40:54 +00:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.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
|
|
|
|
2021-08-27 17:32:06 +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();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
});
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
test("It should add the 'copy-to-clipboard' class", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +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 tell any sticky JS present the page has changed", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// recalculate forces the sticky JS to recalculate any stored DOM position/dimensions
|
|
|
|
|
expect(window.GOVUK.stickAtBottomWhenScrolling.recalculate).toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("It should set the component's minimum height based on its height when the page loads", () => {
|
|
|
|
|
|
|
|
|
|
// to prevent the position of the button moving when the state changes
|
|
|
|
|
expect(window.getComputedStyle(component)['min-height']).toEqual(`${componentHeightOnLoad}px`);
|
|
|
|
|
|
|
|
|
|
});
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2022-05-09 12:38:18 +01:00
|
|
|
test("It should render the 'thing' without extra whitespace", () => {
|
|
|
|
|
|
|
|
|
|
expect(component.querySelector('.copy-to-clipboard__value').textContent).toBe('00000000-0000-0000-0000-000000000000');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
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(() => {
|
|
|
|
|
|
|
|
|
|
// If 'thing' (what the id is) and 'name' (its specific idenifier on the page) are
|
|
|
|
|
// different, it will be one of others called the same 'thing'.
|
|
|
|
|
setUpDOM({ 'thing': 'ID', 'name': 'Default' });
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Because it is not the only 'thing' on the page, the id will not have a heading
|
|
|
|
|
// and so needs some prefix text to label it
|
|
|
|
|
test("The id should have a hidden prefix to label what it is", () => {
|
|
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const value = component.querySelector('.copy-to-clipboard__value .usa-sr-only');
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(value).not.toBeNull();
|
|
|
|
|
expect(value.textContent).toEqual('ID: ');
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("the button should have a hidden suffix naming the id it is for", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const buttonSuffix = component.querySelector('button .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(() => {
|
|
|
|
|
|
|
|
|
|
// The heading is added if 'thing' (what the id is) has the same value as 'name'
|
|
|
|
|
// (its specific identifier on the page) because this means it can assume it is
|
|
|
|
|
// the only one of its type there
|
2021-08-27 17:32:06 +01:00
|
|
|
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
|
2020-08-25 10:26:36 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
2019-08-20 13:45:56 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("Its button and id shouldn't have extra hidden text to identify them", () => {
|
|
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const value = component.querySelector('.copy-to-clipboard__value .usa-sr-only');
|
|
|
|
|
const buttonSuffix = component.querySelector('button .usa-sr-only');
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(value).toBeNull();
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(buttonSuffix).toBeNull();
|
|
|
|
|
|
|
|
|
|
})
|
2019-08-20 13:45:56 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-12 10:24:03 +01:00
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
describe("If you click the 'Copy Some Thing to clipboard' button", () => {
|
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
|
|
|
let keyEl;
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
beforeEach(() => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
|
|
|
|
keyEl = component.querySelector('.copy-to-clipboard__value');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
helpers.triggerEvent(component.querySelector('button'), 'click');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
});
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
test("The live-region should be shown and its text should confirm the copy action", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
const liveRegion = component.querySelector('.copy-to-clipboard__notice');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
expect(liveRegion.classList.contains('usa-sr-only')).toBe(false);
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(liveRegion.textContent.trim()).toEqual(
|
|
|
|
|
expect.stringContaining('Copied to clipboard')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// The button also says this but its text after being changed is not announced due to being
|
|
|
|
|
// lower priority than the live-region
|
|
|
|
|
test("The live-region should contain some hidden text giving context to the statement shown", () => {
|
|
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const liveRegionHiddenText = component.querySelectorAll('.copy-to-clipboard__notice .usa-sr-only');
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
expect(liveRegionHiddenText.length).toEqual(2);
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(liveRegionHiddenText[0].textContent).toEqual('Some Thing ');
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(liveRegionHiddenText[1].textContent).toEqual(', press button to show in page');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
test("It should swap the button for one to show the Some Thing", () => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
expect(component.querySelector('button').textContent.trim()).toEqual(
|
2021-08-27 17:32:06 +01:00
|
|
|
expect.stringContaining('Show Some Thing')
|
2020-08-25 10:26:36 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
});
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
test("It should remove the id from the page", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(component.querySelector('.copy-to-clipboard__value')).toBeNull();
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
test("It should copy the thing to the clipboard", () => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
// it should make a selection (a range) from the contents of the element containing the Some Thing
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(rangeMock.selectNodeContents.mock.calls[0]).toEqual([keyEl]);
|
|
|
|
|
|
|
|
|
|
// that selection (a range) should be added to that for the page (a selection)
|
|
|
|
|
expect(selectionMock.addRange.mock.calls[0]).toEqual([rangeMock]);
|
|
|
|
|
|
|
|
|
|
expect(document.execCommand).toHaveBeenCalled();
|
|
|
|
|
expect(document.execCommand.mock.calls[0]).toEqual(['copy']);
|
|
|
|
|
|
|
|
|
|
// reset any methods in the global space
|
|
|
|
|
window.queryCommandSupported = undefined;
|
|
|
|
|
window.getSelection = undefined;
|
|
|
|
|
document.createRange = undefined;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
describe("If you then click the 'Show Some Thing'", () => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
|
|
|
|
helpers.triggerEvent(component.querySelector('button'), 'click');
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
test("It should change the text to show the Some Thing", () => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(component.querySelector('.copy-to-clipboard__value')).not.toBeNull();
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
test("It should swap the button for one to copy the thing to the clipboard", () => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
expect(component.querySelector('button').textContent.trim()).toEqual(
|
2021-08-27 17:32:06 +01:00
|
|
|
expect.stringContaining('Copy Some Thing to clipboard')
|
2020-08-25 10:26:36 +01: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", () => {
|
|
|
|
|
|
2020-09-15 12:53:58 +01:00
|
|
|
beforeEach(() => {
|
2020-08-25 10:26:36 +01:00
|
|
|
|
|
|
|
|
// If 'thing' (what the id is) and 'name' (its specific idenifier on the page) are
|
|
|
|
|
// different, it will be one of others called the same 'thing'.
|
|
|
|
|
setUpDOM({ 'thing': 'ID', 'name': 'Default' });
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
helpers.triggerEvent(component.querySelector('button'), 'click');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-09-15 12:53:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("the button should have a hidden suffix naming the id it is for", () => {
|
|
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const buttonSuffix = component.querySelector('button .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
|
|
|
|
2022-05-10 10:33:27 +01:00
|
|
|
test("the copied selection (range) should start after visually hidden prefix", () => {
|
2020-09-15 12:53:58 +01:00
|
|
|
|
2022-05-09 12:38:18 +01:00
|
|
|
// that selection (a range) should have a startOffset of 1:
|
2022-05-10 10:33:27 +01:00
|
|
|
// index 0: the visually hidden prefix node, for example "Template ID: " or "API key: "
|
2022-05-09 12:38:18 +01:00
|
|
|
// index 1: the value node
|
2020-09-15 12:53:58 +01:00
|
|
|
expect(rangeMock.setStart).toHaveBeenCalled();
|
2022-05-09 12:38:18 +01:00
|
|
|
expect(rangeMock.setStart.mock.calls[0][1]).toEqual(1);
|
2020-09-15 12:53:58 +01:00
|
|
|
|
|
|
|
|
// reset any methods in the global space
|
|
|
|
|
window.queryCommandSupported = undefined;
|
|
|
|
|
window.getSelection = undefined;
|
|
|
|
|
document.createRange = undefined;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
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", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// The heading is added if 'thing' (what the id is) has the same value as 'name'
|
|
|
|
|
// (its specific identifier on the page) because this means it can assume it is
|
|
|
|
|
// the only one of its type there
|
2021-08-27 17:32:06 +01:00
|
|
|
setUpDOM({ 'thing': 'Some Thing', 'name': 'Some Thing' });
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
// start the module
|
|
|
|
|
window.GOVUK.modules.start();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2021-08-27 17:32:06 +01:00
|
|
|
component = document.querySelector('[data-module=copy-to-clipboard]');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
helpers.triggerEvent(component.querySelector('button'), 'click');
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2020-08-25 10:26:36 +01:00
|
|
|
test("Its button and id shouldn't have extra hidden text to identify them", () => {
|
2019-08-12 10:24:03 +01:00
|
|
|
|
2023-06-16 14:55:24 -04:00
|
|
|
const prefix = component.querySelector('.copy-to-clipboard__value .usa-sr-only');
|
|
|
|
|
const buttonSuffix = component.querySelector('button .usa-sr-only');
|
2021-08-27 17:32:06 +01:00
|
|
|
expect(prefix).toBeNull();
|
2020-08-25 10:26:36 +01:00
|
|
|
expect(buttonSuffix).toBeNull();
|
2019-08-12 10:24:03 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
2020-09-15 17:07:01 +01:00
|
|
|
test("the copied selection (range) should start at the default position", () => {
|
2020-09-15 12:53:58 +01:00
|
|
|
|
|
|
|
|
// that selection (a range) shouldn't call setStart to avoid the prefix:
|
|
|
|
|
expect(rangeMock.setStart).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
|
|
// reset any methods in the global space
|
|
|
|
|
window.queryCommandSupported = undefined;
|
|
|
|
|
window.getSelection = undefined;
|
|
|
|
|
document.createRange = undefined;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-12 10:24:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|