| 1 |
const { assert } = require('chai'); |
| 2 |
const { createScopedPrismDom } = require('../../helper/prism-dom-util'); |
| 3 |
|
| 4 |
|
| 5 |
class DummyClipboard { |
| 6 |
constructor() { |
| 7 |
this.text = ''; |
| 8 |
} |
| 9 |
async readText() { |
| 10 |
return this.text; |
| 11 |
} |
| 12 |
|
| 13 |
writeText(data) { |
| 14 |
this.text = data; |
| 15 |
return Promise.resolve(); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
describe('Copy to Clipboard', function () { |
| 20 |
const { Prism, document, window } = createScopedPrismDom(this, { |
| 21 |
languages: 'javascript', |
| 22 |
plugins: 'copy-to-clipboard', |
| 23 |
}); |
| 24 |
|
| 25 |
|
| 26 |
it('should work', function () { |
| 27 |
const clipboard = new DummyClipboard(); |
| 28 |
window.navigator.clipboard = clipboard; |
| 29 |
|
| 30 |
document.body.innerHTML = `<pre class="language-none"><code>foo</code></pre>`; |
| 31 |
Prism.highlightAll(); |
| 32 |
|
| 33 |
const button = document.querySelector('button'); |
| 34 |
assert.notStrictEqual(button, null); |
| 35 |
|
| 36 |
button.click(); |
| 37 |
|
| 38 |
assert.strictEqual(clipboard.text, 'foo'); |
| 39 |
}); |
| 40 |
|
| 41 |
it('should copy the current text even after the code block changes its text', function () { |
| 42 |
const clipboard = new DummyClipboard(); |
| 43 |
window.navigator.clipboard = clipboard; |
| 44 |
|
| 45 |
document.body.innerHTML = `<pre class="language-none"><code>foo</code></pre>`; |
| 46 |
Prism.highlightAll(); |
| 47 |
|
| 48 |
const button = document.querySelector('button'); |
| 49 |
assert.notStrictEqual(button, null); |
| 50 |
|
| 51 |
button.click(); |
| 52 |
|
| 53 |
assert.strictEqual(clipboard.text, 'foo'); |
| 54 |
|
| 55 |
|
| 56 |
document.querySelector('code').textContent = 'bar'; |
| 57 |
|
| 58 |
button.click(); |
| 59 |
|
| 60 |
assert.strictEqual(clipboard.text, 'bar'); |
| 61 |
|
| 62 |
|
| 63 |
document.querySelector('code').textContent = 'baz'; |
| 64 |
Prism.highlightAll(); |
| 65 |
|
| 66 |
button.click(); |
| 67 |
|
| 68 |
assert.strictEqual(clipboard.text, 'baz'); |
| 69 |
}); |
| 70 |
|
| 71 |
}); |