diff --git a/src/clipboard-action.js b/src/clipboard-action.js index 537d4f4..8e1c926 100644 --- a/src/clipboard-action.js +++ b/src/clipboard-action.js @@ -8,19 +8,25 @@ class ClipboardAction { */ constructor(options) { this.action = options.action; - this.emitter = options.emitter; + this.emitter = options.emitter; this.target = options.target; this.text = options.text; this.trigger = options.trigger; this.selectedText = ''; - if (this.text) { + if (this.text && this.target) { + throw new Error('Multiple attributes declared, use either "data-target" or "data-text"'); + } + else if (this.text) { this.selectFake(); } else if (this.target) { this.selectTarget(); } + else { + throw new Error('Missing required attributes, use either "data-target" or "data-text"'); + } } /** diff --git a/test/clipboard-action.js b/test/clipboard-action.js index d6a2d10..5d741a4 100644 --- a/test/clipboard-action.js +++ b/test/clipboard-action.js @@ -12,6 +12,31 @@ describe('ClipboardAction', () => { }); describe('#constructor', () => { + it('should throw an error since both "data-text" and "data-target" were passed', (done) => { + try { + new ClipboardAction({ + text: 'foo', + target: 'target' + }); + } + catch(e) { + assert.equal(e.message, 'Multiple attributes declared, use either "data-target" or "data-text"'); + done(); + } + }); + + it('should throw an error since neither "data-text" nor "data-target" were passed', (done) => { + try { + new ClipboardAction({ + action: '' + }); + } + catch(e) { + assert.equal(e.message, 'Missing required attributes, use either "data-target" or "data-text"'); + done(); + } + }); + it('should throw an error since "data-action" is invalid', (done) => { try { new ClipboardAction({