1 | import Clipboard from '../src/clipboard';
|
2 |
|
3 | describe('Clipboard', () => {
|
4 | before(() => {
|
5 | global.button = document.createElement('button');
|
6 | global.button.setAttribute('class', 'btn');
|
7 | global.button.setAttribute('data-clipboard-text', 'foo');
|
8 | document.body.appendChild(global.button);
|
9 |
|
10 | global.span = document.createElement('span');
|
11 | global.span.innerHTML = 'bar';
|
12 |
|
13 | global.button.appendChild(span);
|
14 |
|
15 | global.event = {
|
16 | target: global.button,
|
17 | currentTarget: global.button,
|
18 | };
|
19 | });
|
20 |
|
21 | after(() => {
|
22 | document.body.innerHTML = '';
|
23 | });
|
24 |
|
25 | describe('#resolveOptions', () => {
|
26 | before(() => {
|
27 | global.fn = () => {};
|
28 | });
|
29 |
|
30 | it('should set action as a function', () => {
|
31 | let clipboard = new Clipboard('.btn', {
|
32 | action: global.fn,
|
33 | });
|
34 |
|
35 | assert.equal(global.fn, clipboard.action);
|
36 | });
|
37 |
|
38 | it('should set target as a function', () => {
|
39 | let clipboard = new Clipboard('.btn', {
|
40 | target: global.fn,
|
41 | });
|
42 |
|
43 | assert.equal(global.fn, clipboard.target);
|
44 | });
|
45 |
|
46 | it('should set text as a function', () => {
|
47 | let clipboard = new Clipboard('.btn', {
|
48 | text: global.fn,
|
49 | });
|
50 |
|
51 | assert.equal(global.fn, clipboard.text);
|
52 | });
|
53 |
|
54 | it('should set container as an object', () => {
|
55 | let clipboard = new Clipboard('.btn', {
|
56 | container: document.body,
|
57 | });
|
58 |
|
59 | assert.equal(document.body, clipboard.container);
|
60 | });
|
61 |
|
62 | it('should set container as body by default', () => {
|
63 | let clipboard = new Clipboard('.btn');
|
64 |
|
65 | assert.equal(document.body, clipboard.container);
|
66 | });
|
67 | });
|
68 |
|
69 | describe('#listenClick', () => {
|
70 | it('should add a click event listener to the passed selector', () => {
|
71 | let clipboard = new Clipboard('.btn');
|
72 | assert.isObject(clipboard.listener);
|
73 | });
|
74 | });
|
75 |
|
76 | describe('#onClick', () => {
|
77 | it('should init when called', (done) => {
|
78 | let clipboard = new Clipboard('.btn');
|
79 |
|
80 | clipboard.on('success', () => {
|
81 | done();
|
82 | });
|
83 |
|
84 | clipboard.onClick(global.event);
|
85 | });
|
86 |
|
87 | it("should use an event's currentTarget when not equal to target", (done) => {
|
88 | let clipboard = new Clipboard('.btn');
|
89 | let bubbledEvent = {
|
90 | target: global.span,
|
91 | currentTarget: global.button,
|
92 | };
|
93 |
|
94 | clipboard.on('success', () => {
|
95 | done();
|
96 | });
|
97 |
|
98 | clipboard.onClick(bubbledEvent);
|
99 | });
|
100 |
|
101 | it('should throw an exception when target is invalid', (done) => {
|
102 | try {
|
103 | const clipboard = new Clipboard('.btn', {
|
104 | target() {
|
105 | return null;
|
106 | },
|
107 | });
|
108 |
|
109 | clipboard.onClick(global.event);
|
110 | } catch (e) {
|
111 | assert.equal(e.message, 'Invalid "target" value, use a valid Element');
|
112 | done();
|
113 | }
|
114 | });
|
115 | });
|
116 |
|
117 | describe('#static isSupported', () => {
|
118 | it('should return the support of the given action', () => {
|
119 | assert.equal(Clipboard.isSupported('copy'), true);
|
120 | assert.equal(Clipboard.isSupported('cut'), true);
|
121 | });
|
122 |
|
123 | it('should return the support of the cut and copy actions', () => {
|
124 | assert.equal(Clipboard.isSupported(), true);
|
125 | });
|
126 | });
|
127 |
|
128 | describe('#static copy', () => {
|
129 | it('should copy in an programatic way based on text', () => {
|
130 | assert.equal(Clipboard.copy('lorem'), 'lorem');
|
131 | });
|
132 |
|
133 | it('should copy in an programatic way based on target', () => {
|
134 | assert.equal(Clipboard.copy(document.querySelector('span')), 'bar');
|
135 | });
|
136 | });
|
137 |
|
138 | describe('#static cut', () => {
|
139 | it('should cut in an programatic way based on text', () => {
|
140 | assert.equal(Clipboard.cut(document.querySelector('span')), 'bar');
|
141 | });
|
142 | });
|
143 |
|
144 | describe('#destroy', () => {
|
145 | it('should destroy an existing instance of ClipboardActionDefault', () => {
|
146 | let clipboard = new Clipboard('.btn');
|
147 |
|
148 | clipboard.onClick(global.event);
|
149 | clipboard.destroy();
|
150 |
|
151 | assert.equal(clipboard.clipboardAction, null);
|
152 | });
|
153 | });
|
154 |
|
155 | describe('#events', () => {
|
156 | it('should fire a success event with certain properties', (done) => {
|
157 | let clipboard = new Clipboard('.btn');
|
158 |
|
159 | clipboard.on('success', (e) => {
|
160 | assert.property(e, 'action');
|
161 | assert.equal(e.action, 'copy');
|
162 | assert.property(e, 'text');
|
163 | assert.property(e, 'trigger');
|
164 | assert.property(e, 'clearSelection');
|
165 |
|
166 | done();
|
167 | });
|
168 |
|
169 | clipboard.onClick(global.event);
|
170 | });
|
171 | });
|
172 |
|
173 | describe('#clearSelection', () => {
|
174 | it('should clear text selection without moving focus', (done) => {
|
175 | let clipboard = new Clipboard('.btn');
|
176 |
|
177 | clipboard.on('success', (e) => {
|
178 | e.clearSelection();
|
179 |
|
180 | let selectedElem = document.activeElement;
|
181 | let selectedText = window.getSelection().toString();
|
182 |
|
183 | assert.equal(selectedElem, e.trigger);
|
184 | assert.equal(selectedText, '');
|
185 |
|
186 | done();
|
187 | });
|
188 |
|
189 | clipboard.onClick(global.event);
|
190 | });
|
191 | });
|
192 | });
|