clipboard-copy-element/test/test.js

239 строки
6.6 KiB
JavaScript
Исходник Обычный вид История

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)
})
})
2020-05-21 23:10:07 +03:00
describe('clicking the button', function () {
beforeEach(function () {
const container = document.createElement('div')
container.innerHTML = `
<clipboard-copy value="target text">
Copy
</clipboard-copy>`
document.body.append(container)
})
2020-05-21 23:10:07 +03:00
afterEach(function () {
document.body.innerHTML = ''
})
2020-05-21 23:10:07 +03:00
it('retains focus on the button', function () {
const button = document.querySelector('clipboard-copy')
button.focus()
assert.equal(document.activeElement, button)
button.click()
assert.equal(document.activeElement, button)
})
})
2020-05-21 23:10:07 +03:00
describe('target element', function () {
const nativeClipboard = navigator.clipboard
let whenCopied
2020-05-21 23:10:07 +03:00
beforeEach(function () {
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
},
readText() {
return Promise.resolve(copiedText)
},
})
whenCopied = new Promise(resolve => {
2023-06-08 17:13:50 +03:00
document.addEventListener('clipboard-copy', () => resolve(copiedText), {
once: true,
})
})
})
2020-05-21 23:10:07 +03:00
afterEach(function () {
document.body.innerHTML = ''
defineClipboard(nativeClipboard)
})
2020-05-23 00:22:22 +03:00
it('node', async function () {
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!')
})
2020-05-23 00:22:22 +03:00
it('hidden input', async function () {
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!')
})
2020-05-23 00:22:22 +03:00
it('input field', async function () {
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!')
})
2020-05-23 00:22:22 +03:00
it('textarea', async function () {
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!')
})
2020-05-23 00:22:22 +03:00
it('a[href]', async function () {
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`)
})
2020-05-23 00:22:22 +03:00
it('a[id]', async function () {
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')
})
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')
let fired = false
document.addEventListener(
'clipboard-copy',
() => {
fired = true
},
{once: true},
)
2023-09-27 20:45:55 +03:00
button.click()
await new Promise(setTimeout)
assert.equal(fired, false)
assert.equal(null, await navigator.clipboard.readText())
2023-09-27 20:45:55 +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
})
}