2023-06-08 17:13:50 +03:00
|
|
|
import {assert} from '@open-wc/testing'
|
|
|
|
import {ClipboardCopyElement} from '../src/index.ts'
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
describe('clipboard-copy element', function () {
|
|
|
|
describe('element creation', function () {
|
|
|
|
it('creates from document.createElement', function () {
|
2018-04-17 23:10:02 +03:00
|
|
|
const el = document.createElement('clipboard-copy')
|
|
|
|
assert.equal('CLIPBOARD-COPY', el.nodeName)
|
2023-06-08 17:13:50 +03:00
|
|
|
assert(el instanceof ClipboardCopyElement)
|
2018-04-17 23:10:02 +03:00
|
|
|
assert(el instanceof window.ClipboardCopyElement)
|
|
|
|
})
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
it('creates from constructor', function () {
|
2018-04-17 23:10:02 +03:00
|
|
|
const el = new window.ClipboardCopyElement()
|
|
|
|
assert.equal('CLIPBOARD-COPY', el.nodeName)
|
|
|
|
})
|
|
|
|
})
|
2018-04-18 01:02:37 +03:00
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
describe('clicking the button', function () {
|
|
|
|
beforeEach(function () {
|
2018-04-18 01:02:37 +03:00
|
|
|
const container = document.createElement('div')
|
|
|
|
container.innerHTML = `
|
2018-04-18 21:06:52 +03:00
|
|
|
<clipboard-copy value="target text">
|
2018-04-18 01:02:37 +03:00
|
|
|
Copy
|
|
|
|
</clipboard-copy>`
|
|
|
|
document.body.append(container)
|
|
|
|
})
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
afterEach(function () {
|
2018-04-18 01:02:37 +03:00
|
|
|
document.body.innerHTML = ''
|
|
|
|
})
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
it('retains focus on the button', function () {
|
2018-04-18 01:02:37 +03:00
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.focus()
|
|
|
|
assert.equal(document.activeElement, button)
|
|
|
|
button.click()
|
|
|
|
assert.equal(document.activeElement, button)
|
|
|
|
})
|
|
|
|
})
|
2018-08-11 03:22:49 +03:00
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
describe('target element', function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const nativeClipboard = navigator.clipboard
|
|
|
|
let whenCopied
|
2023-09-28 20:34:37 +03:00
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
beforeEach(function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const container = document.createElement('div')
|
|
|
|
container.innerHTML = `
|
|
|
|
<clipboard-copy for="copy-target">
|
|
|
|
Copy
|
|
|
|
</clipboard-copy>`
|
|
|
|
document.body.append(container)
|
|
|
|
|
|
|
|
let copiedText = null
|
|
|
|
defineClipboard({
|
|
|
|
writeText(text) {
|
|
|
|
copiedText = text
|
|
|
|
return Promise.resolve()
|
2023-06-08 17:13:50 +03:00
|
|
|
},
|
2023-09-28 20:34:37 +03:00
|
|
|
readText() {
|
|
|
|
return Promise.resolve(copiedText)
|
|
|
|
},
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
whenCopied = new Promise(resolve => {
|
2023-06-08 17:13:50 +03:00
|
|
|
document.addEventListener('clipboard-copy', () => resolve(copiedText), {
|
|
|
|
once: true,
|
|
|
|
})
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
afterEach(function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
document.body.innerHTML = ''
|
|
|
|
defineClipboard(nativeClipboard)
|
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('node', async function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const target = document.createElement('div')
|
|
|
|
target.innerHTML = 'Hello <b>world!</b>'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'Hello world!')
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('hidden input', async function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const target = document.createElement('input')
|
|
|
|
target.type = 'hidden'
|
|
|
|
target.value = 'Hello world!'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'Hello world!')
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('input field', async function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const target = document.createElement('input')
|
|
|
|
target.value = 'Hello world!'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'Hello world!')
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('textarea', async function () {
|
2018-08-11 03:22:49 +03:00
|
|
|
const target = document.createElement('textarea')
|
|
|
|
target.value = 'Hello world!'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'Hello world!')
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
2018-08-11 03:37:42 +03:00
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('a[href]', async function () {
|
2018-08-11 03:37:42 +03:00
|
|
|
const target = document.createElement('a')
|
|
|
|
target.href = '/hello#world'
|
|
|
|
target.textContent = 'I am a link'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, `${location.origin}/hello#world`)
|
2018-08-11 03:37:42 +03:00
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('a[id]', async function () {
|
2018-08-11 03:37:42 +03:00
|
|
|
const target = document.createElement('a')
|
|
|
|
target.textContent = 'I am a link'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'I am a link')
|
2018-08-11 03:37:42 +03:00
|
|
|
})
|
2023-09-27 20:45:55 +03:00
|
|
|
|
|
|
|
it('does not copy when disabled', async function () {
|
|
|
|
const target = document.createElement('div')
|
|
|
|
target.innerHTML = 'Hello world!'
|
|
|
|
target.id = 'copy-target'
|
|
|
|
document.body.append(target)
|
|
|
|
|
|
|
|
const button = document.querySelector('clipboard-copy')
|
|
|
|
button.setAttribute('aria-disabled', 'true')
|
2023-09-28 20:34:37 +03:00
|
|
|
|
|
|
|
let fired = false
|
|
|
|
document.addEventListener(
|
|
|
|
'clipboard-copy',
|
|
|
|
() => {
|
|
|
|
fired = true
|
|
|
|
},
|
|
|
|
{once: true},
|
|
|
|
)
|
|
|
|
|
2023-09-27 20:45:55 +03:00
|
|
|
button.click()
|
2023-09-28 20:34:37 +03:00
|
|
|
|
2023-09-28 20:00:48 +03:00
|
|
|
await new Promise(setTimeout)
|
2023-09-28 20:34:37 +03:00
|
|
|
assert.equal(fired, false)
|
|
|
|
assert.equal(null, await navigator.clipboard.readText())
|
2023-09-27 20:45:55 +03:00
|
|
|
})
|
2018-08-11 03:22:49 +03:00
|
|
|
})
|
2019-11-05 01:36:48 +03:00
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
describe('shadow DOM context', function () {
|
2019-11-05 01:36:48 +03:00
|
|
|
const nativeClipboard = navigator.clipboard
|
|
|
|
let whenCopied
|
2020-05-21 23:10:07 +03:00
|
|
|
beforeEach(function () {
|
2019-11-05 01:36:48 +03:00
|
|
|
const container = document.createElement('div')
|
|
|
|
container.id = 'shadow'
|
|
|
|
const elementInDocument = document.createElement('div')
|
|
|
|
elementInDocument.id = 'copy-target'
|
|
|
|
elementInDocument.textContent = 'Target in Document'
|
|
|
|
const shadowRoot = container.attachShadow({mode: 'open'})
|
|
|
|
shadowRoot.innerHTML = `
|
|
|
|
<clipboard-copy for="copy-target">
|
|
|
|
Copy
|
|
|
|
</clipboard-copy>
|
|
|
|
<div id="copy-target">Target in shadowRoot</div>`
|
|
|
|
document.body.append(container)
|
|
|
|
document.body.append(elementInDocument)
|
|
|
|
container.click()
|
|
|
|
|
|
|
|
let copiedText = null
|
|
|
|
defineClipboard({
|
|
|
|
writeText(text) {
|
|
|
|
copiedText = text
|
|
|
|
return Promise.resolve()
|
2023-06-08 17:13:50 +03:00
|
|
|
},
|
2019-11-05 01:36:48 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
whenCopied = new Promise(resolve => {
|
|
|
|
shadowRoot.addEventListener('clipboard-copy', () => resolve(copiedText), {once: true})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-21 23:10:07 +03:00
|
|
|
afterEach(function () {
|
2019-11-05 01:36:48 +03:00
|
|
|
document.body.innerHTML = ''
|
|
|
|
defineClipboard(nativeClipboard)
|
|
|
|
})
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
it('copies from within its shadow root', async function () {
|
2019-11-05 01:36:48 +03:00
|
|
|
const shadow = document.querySelector('#shadow')
|
|
|
|
shadow.shadowRoot.querySelector('clipboard-copy').click()
|
|
|
|
|
2020-05-23 00:22:22 +03:00
|
|
|
const text = await whenCopied
|
|
|
|
assert.equal(text, 'Target in shadowRoot')
|
2019-11-05 01:36:48 +03:00
|
|
|
})
|
|
|
|
})
|
2018-04-17 23:10:02 +03:00
|
|
|
})
|
2019-11-05 01:44:29 +03:00
|
|
|
|
|
|
|
function defineClipboard(customClipboard) {
|
|
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
get() {
|
|
|
|
return customClipboard
|
2023-06-08 17:13:50 +03:00
|
|
|
},
|
2019-11-05 01:44:29 +03:00
|
|
|
})
|
|
|
|
}
|