From f267fb88f213d0a24cdfcb655fb537de963ae7ac Mon Sep 17 00:00:00 2001 From: Beto Muniz Date: Sun, 11 Apr 2021 14:13:39 -0300 Subject: [PATCH] Add/update tests --- test/clipboard-action-copy.js | 55 ++++++++++++++++++++++++ test/clipboard-action-cut.js | 32 ++++++++++++++ test/clipboard-action-default.js | 67 ++---------------------------- test/common/command.js | 35 ++++++++++++++++ test/common/create-fake-element.js | 13 ++++++ 5 files changed, 138 insertions(+), 64 deletions(-) create mode 100644 test/clipboard-action-copy.js create mode 100644 test/clipboard-action-cut.js create mode 100644 test/common/command.js create mode 100644 test/common/create-fake-element.js diff --git a/test/clipboard-action-copy.js b/test/clipboard-action-copy.js new file mode 100644 index 0000000..c676736 --- /dev/null +++ b/test/clipboard-action-copy.js @@ -0,0 +1,55 @@ +import ClipboardActionCopy from '../src/clipboard-action-copy'; + +describe('ClipboardActionCopy', () => { + before(() => { + global.input = document.createElement('input'); + global.input.setAttribute('id', 'input'); + global.input.setAttribute('value', 'abc'); + document.body.appendChild(global.input); + + global.paragraph = document.createElement('p'); + global.paragraph.setAttribute('id', 'paragraph'); + global.paragraph.textContent = 'abc'; + document.body.appendChild(global.paragraph); + }); + + after(() => { + document.body.innerHTML = ''; + }); + + describe.only('#selectText', () => { + it('should select its value based on input target', () => { + const selectedText = ClipboardActionCopy( + document.querySelector('#input'), + { + container: document.body, + } + ); + + assert.equal(selectedText, document.querySelector('#input').value); + }); + + it('should select its value based on element target', () => { + const selectedText = ClipboardActionCopy( + document.querySelector('#paragraph'), + { + container: document.body, + } + ); + + assert.equal( + selectedText, + document.querySelector('#paragraph').textContent + ); + }); + + it('should select its value based on text', () => { + const text = 'abc'; + const selectedText = ClipboardActionCopy(text, { + container: document.body, + }); + + assert.equal(selectedText, text); + }); + }); +}); diff --git a/test/clipboard-action-cut.js b/test/clipboard-action-cut.js new file mode 100644 index 0000000..08c2de6 --- /dev/null +++ b/test/clipboard-action-cut.js @@ -0,0 +1,32 @@ +import ClipboardActionCut from '../src/clipboard-action-cut'; + +describe('ClipboardActionCut', () => { + before(() => { + global.input = document.createElement('input'); + global.input.setAttribute('id', 'input'); + global.input.setAttribute('value', 'abc'); + document.body.appendChild(global.input); + + global.paragraph = document.createElement('p'); + global.paragraph.setAttribute('id', 'paragraph'); + global.paragraph.textContent = 'abc'; + document.body.appendChild(global.paragraph); + }); + + after(() => { + document.body.innerHTML = ''; + }); + + describe.only('#selectText', () => { + it('should select its value', () => { + const selectedText = ClipboardActionCut( + document.querySelector('#input'), + { + container: document.body, + } + ); + + assert.equal(selectedText, document.querySelector('#input').value); + }); + }); +}); diff --git a/test/clipboard-action-default.js b/test/clipboard-action-default.js index 4ee6826..90227b6 100644 --- a/test/clipboard-action-default.js +++ b/test/clipboard-action-default.js @@ -36,24 +36,6 @@ describe('ClipboardActionDefault', () => { }); }); - describe('#initSelection', () => { - xit('should set the position right style property', (done) => { - // Set document direction - document.documentElement.setAttribute('dir', 'rtl'); - - let clip = new ClipboardActionDefault({ - emitter: new Emitter(), - container: document.body, - text: 'foo', - }); - - const el = clip.createFakeElement(); - - assert.equal(el.style.right, '-9999px'); - done(); - }); - }); - describe('#set action', () => { it('should throw an error since "action" is invalid', (done) => { try { @@ -84,34 +66,6 @@ describe('ClipboardActionDefault', () => { }); }); - describe('#selectText', () => { - xit('should create a fake element and select its value', () => { - let clip = new ClipboardActionDefault({ - emitter: new Emitter(), - container: document.body, - text: 'blah', - }); - - const el = clip.createFakeElement(); - - assert.equal(clip.selectedText, el.value); - }); - }); - - describe('#removeFake', () => { - xit('should remove a temporary fake element', () => { - let clip = new ClipboardActionDefault({ - emitter: new Emitter(), - container: document.body, - text: 'blah', - }); - - clip.removeFake(); - - assert.equal(clip.fakeElem, null); - }); - }); - describe('#selectTarget', () => { it('should select text from editable element', () => { let clip = new ClipboardActionDefault({ @@ -158,9 +112,7 @@ describe('ClipboardActionDefault', () => { }); }); - xit('should fire an error event on browsers that support copy command', (done) => { - global.stub.returns(false); - + it('should fire an error event on browsers that support cut command', (done) => { let emitter = new Emitter(); emitter.on('error', () => { @@ -171,6 +123,8 @@ describe('ClipboardActionDefault', () => { emitter, target: document.querySelector('#input'), }); + + clip.handleResult(false); }); }); @@ -230,19 +184,4 @@ describe('ClipboardActionDefault', () => { assert.equal(selectedText, ''); }); }); - - describe('#destroy', () => { - xit('should destroy an existing fake element', () => { - let clip = new ClipboardActionDefault({ - emitter: new Emitter(), - container: document.body, - text: 'blah', - }); - - clip.selectFake(); - clip.destroy(); - - assert.equal(clip.fakeElem, null); - }); - }); }); diff --git a/test/common/command.js b/test/common/command.js new file mode 100644 index 0000000..7d1e3c9 --- /dev/null +++ b/test/common/command.js @@ -0,0 +1,35 @@ +import select from 'select'; +import command from '../../src/common/command'; + +describe('command', () => { + before(() => { + global.input = document.createElement('input'); + global.input.setAttribute('id', 'input'); + global.input.setAttribute('value', 'abc'); + document.body.appendChild(global.input); + }); + + after(() => { + document.body.innerHTML = ''; + }); + + it('should execute cut command', (done) => { + // Set document direction + document.documentElement.setAttribute('dir', 'rtl'); + + select(document.querySelector('#input')); + + assert.isTrue(command('cut')); + done(); + }); + + it('should execute copy command', (done) => { + // Set document direction + document.documentElement.setAttribute('dir', 'rtl'); + + select(document.querySelector('#input')); + + assert.isTrue(command('copy')); + done(); + }); +}); diff --git a/test/common/create-fake-element.js b/test/common/create-fake-element.js new file mode 100644 index 0000000..98934a8 --- /dev/null +++ b/test/common/create-fake-element.js @@ -0,0 +1,13 @@ +import createFakeElement from '../../src/common/create-fake-element'; + +describe('createFakeElement', () => { + it('should define a fake element and set the position right style property', (done) => { + // Set document direction + document.documentElement.setAttribute('dir', 'rtl'); + + const el = createFakeElement(document.body); + + assert.equal(el.style.right, '-9999px'); + done(); + }); +});