Remove unnecessary dependency

This commit is contained in:
Beto Muniz 2021-01-20 12:57:58 -03:00
parent 6479739564
commit e5d3f91f35
1 changed files with 53 additions and 49 deletions

View File

@ -1,127 +1,131 @@
import Clipboard from '../src/clipboard'; import Clipboard from "../src/clipboard";
import ClipboardAction from '../src/clipboard-action'; import ClipboardAction from "../src/clipboard-action";
import listen from 'good-listener';
describe('Clipboard', () => { describe("Clipboard", () => {
before(() => { before(() => {
global.button = document.createElement('button'); global.button = document.createElement("button");
global.button.setAttribute('class', 'btn'); global.button.setAttribute("class", "btn");
global.button.setAttribute('data-clipboard-text', 'foo'); global.button.setAttribute("data-clipboard-text", "foo");
document.body.appendChild(global.button); document.body.appendChild(global.button);
global.span = document.createElement('span'); global.span = document.createElement("span");
global.span.innerHTML = 'bar'; global.span.innerHTML = "bar";
global.button.appendChild(span); global.button.appendChild(span);
global.event = { global.event = {
target: global.button, target: global.button,
currentTarget: global.button currentTarget: global.button,
}; };
}); });
after(() => { after(() => {
document.body.innerHTML = ''; document.body.innerHTML = "";
}); });
describe('#resolveOptions', () => { describe("#resolveOptions", () => {
before(() => { before(() => {
global.fn = () => {}; global.fn = () => {};
}); });
it('should set action as a function', () => { it("should set action as a function", () => {
let clipboard = new Clipboard('.btn', { let clipboard = new Clipboard(".btn", {
action: global.fn action: global.fn,
}); });
assert.equal(global.fn, clipboard.action); assert.equal(global.fn, clipboard.action);
}); });
it('should set target as a function', () => { it("should set target as a function", () => {
let clipboard = new Clipboard('.btn', { let clipboard = new Clipboard(".btn", {
target: global.fn target: global.fn,
}); });
assert.equal(global.fn, clipboard.target); assert.equal(global.fn, clipboard.target);
}); });
it('should set text as a function', () => { it("should set text as a function", () => {
let clipboard = new Clipboard('.btn', { let clipboard = new Clipboard(".btn", {
text: global.fn text: global.fn,
}); });
assert.equal(global.fn, clipboard.text); assert.equal(global.fn, clipboard.text);
}); });
it('should set container as an object', () => { it("should set container as an object", () => {
let clipboard = new Clipboard('.btn', { let clipboard = new Clipboard(".btn", {
container: document.body container: document.body,
}); });
assert.equal(document.body, clipboard.container); assert.equal(document.body, clipboard.container);
}); });
it('should set container as body by default', () => { it("should set container as body by default", () => {
let clipboard = new Clipboard('.btn'); let clipboard = new Clipboard(".btn");
assert.equal(document.body, clipboard.container); assert.equal(document.body, clipboard.container);
}); });
}); });
describe('#listenClick', () => { describe("#listenClick", () => {
it('should add a click event listener to the passed selector', () => { it("should add a click event listener to the passed selector", () => {
let clipboard = new Clipboard('.btn'); let clipboard = new Clipboard(".btn");
assert.isObject(clipboard.listener); assert.isObject(clipboard.listener);
}); });
}); });
describe('#onClick', () => { describe("#onClick", () => {
it('should create a new instance of ClipboardAction', () => { it("should create a new instance of ClipboardAction", () => {
let clipboard = new Clipboard('.btn'); let clipboard = new Clipboard(".btn");
clipboard.onClick(global.event); clipboard.onClick(global.event);
assert.instanceOf(clipboard.clipboardAction, ClipboardAction); assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
}); });
it('should use an event\'s currentTarget when not equal to target', () => { it("should use an event's currentTarget when not equal to target", () => {
let clipboard = new Clipboard('.btn'); let clipboard = new Clipboard(".btn");
let bubbledEvent = { target: global.span, currentTarget: global.button }; let bubbledEvent = {
target: global.span,
currentTarget: global.button,
};
clipboard.onClick(bubbledEvent); clipboard.onClick(bubbledEvent);
assert.instanceOf(clipboard.clipboardAction, ClipboardAction); assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
}); });
it('should throw an exception when target is invalid', done => { it("should throw an exception when target is invalid", (done) => {
try { try {
const clipboard = new Clipboard('.btn', { const clipboard = new Clipboard(".btn", {
target() { target() {
return null; return null;
} },
}); });
clipboard.onClick(global.event); clipboard.onClick(global.event);
} } catch (e) {
catch(e) { assert.equal(
assert.equal(e.message, 'Invalid "target" value, use a valid Element'); e.message,
'Invalid "target" value, use a valid Element'
);
done(); done();
} }
}); });
}); });
describe('#static isSupported', () => { describe("#static isSupported", () => {
it('should return the support of the given action', () => { it("should return the support of the given action", () => {
assert.equal(Clipboard.isSupported('copy'), true); assert.equal(Clipboard.isSupported("copy"), true);
assert.equal(Clipboard.isSupported('cut'), true); assert.equal(Clipboard.isSupported("cut"), true);
}); });
it('should return the support of the cut and copy actions', () => { it("should return the support of the cut and copy actions", () => {
assert.equal(Clipboard.isSupported(), true); assert.equal(Clipboard.isSupported(), true);
}); });
}); });
describe('#destroy', () => { describe("#destroy", () => {
it('should destroy an existing instance of ClipboardAction', () => { it("should destroy an existing instance of ClipboardAction", () => {
let clipboard = new Clipboard('.btn'); let clipboard = new Clipboard(".btn");
clipboard.onClick(global.event); clipboard.onClick(global.event);
clipboard.destroy(); clipboard.destroy();