Throws error if either data-target or data-text were passed and throws error if neither data-target nor data-text were passed too

This commit is contained in:
Zeno Rocha 2015-09-26 09:25:15 -07:00
parent ce7b9652c7
commit abeee82bdc
2 changed files with 33 additions and 2 deletions

View File

@ -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"');
}
}
/**

View File

@ -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({