UNPKG

4.05 kBJavaScriptView Raw
1import Clipboard from '../src/clipboard';
2import ClipboardAction from '../src/clipboard-action';
3import listen from 'good-listener';
4
5describe('Clipboard', () => {
6 before(() => {
7 global.button = document.createElement('button');
8 global.button.setAttribute('class', 'btn');
9 global.button.setAttribute('data-clipboard-text', 'foo');
10 document.body.appendChild(global.button);
11
12 global.span = document.createElement('span');
13 global.span.innerHTML = 'bar';
14
15 global.button.appendChild(span);
16
17 global.event = {
18 target: global.button,
19 currentTarget: global.button
20 };
21 });
22
23 after(() => {
24 document.body.innerHTML = '';
25 });
26
27 describe('#resolveOptions', () => {
28 before(() => {
29 global.fn = () => {};
30 });
31
32 it('should set action as a function', () => {
33 let clipboard = new Clipboard('.btn', {
34 action: global.fn
35 });
36
37 assert.equal(global.fn, clipboard.action);
38 });
39
40 it('should set target as a function', () => {
41 let clipboard = new Clipboard('.btn', {
42 target: global.fn
43 });
44
45 assert.equal(global.fn, clipboard.target);
46 });
47
48 it('should set text as a function', () => {
49 let clipboard = new Clipboard('.btn', {
50 text: global.fn
51 });
52
53 assert.equal(global.fn, clipboard.text);
54 });
55
56 it('should set container as an object', () => {
57 let clipboard = new Clipboard('.btn', {
58 container: document.body
59 });
60
61 assert.equal(document.body, clipboard.container);
62 });
63
64 it('should set container as body by default', () => {
65 let clipboard = new Clipboard('.btn');
66
67 assert.equal(document.body, clipboard.container);
68 });
69 });
70
71 describe('#listenClick', () => {
72 it('should add a click event listener to the passed selector', () => {
73 let clipboard = new Clipboard('.btn');
74 assert.isObject(clipboard.listener);
75 });
76 });
77
78 describe('#onClick', () => {
79 it('should create a new instance of ClipboardAction', () => {
80 let clipboard = new Clipboard('.btn');
81
82 clipboard.onClick(global.event);
83 assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
84 });
85
86 it('should use an event\'s currentTarget when not equal to target', () => {
87 let clipboard = new Clipboard('.btn');
88 let bubbledEvent = { target: global.span, currentTarget: global.button };
89
90 clipboard.onClick(bubbledEvent);
91 assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
92 });
93
94 it('should throw an exception when target is invalid', done => {
95 try {
96 const clipboard = new Clipboard('.btn', {
97 target() {
98 return null;
99 }
100 });
101
102 clipboard.onClick(global.event);
103 }
104 catch(e) {
105 assert.equal(e.message, 'Invalid "target" value, use a valid Element');
106 done();
107 }
108 });
109 });
110
111 describe('#static isSupported', () => {
112 it('should return the support of the given action', () => {
113 assert.equal(Clipboard.isSupported('copy'), false);
114 assert.equal(Clipboard.isSupported('cut'), false);
115 });
116
117 it('should return the support of the cut and copy actions', () => {
118 assert.equal(Clipboard.isSupported(), false);
119 });
120 });
121
122 describe('#destroy', () => {
123 it('should destroy an existing instance of ClipboardAction', () => {
124 let clipboard = new Clipboard('.btn');
125
126 clipboard.onClick(global.event);
127 clipboard.destroy();
128
129 assert.equal(clipboard.clipboardAction, null);
130 });
131 });
132});