UNPKG

6.37 kBJavaScriptView Raw
1import Emitter from 'tiny-emitter';
2import ClipboardAction from '../src/clipboard-action';
3
4describe('ClipboardAction', () => {
5 before(() => {
6 global.input = document.createElement('input');
7 global.input.setAttribute('id', 'input');
8 global.input.setAttribute('value', 'abc');
9 document.body.appendChild(global.input);
10
11 global.paragraph = document.createElement('p');
12 global.paragraph.setAttribute('id', 'paragraph');
13 global.paragraph.textContent = 'abc';
14 document.body.appendChild(global.paragraph);
15 });
16
17 after(() => {
18 document.body.innerHTML = '';
19 });
20
21 describe('#resolveOptions', () => {
22 it('should set base properties', () => {
23 let clip = new ClipboardAction({
24 emitter: new Emitter(),
25 container: document.body,
26 text: 'foo',
27 });
28
29 assert.property(clip, 'action');
30 assert.property(clip, 'container');
31 assert.property(clip, 'emitter');
32 assert.property(clip, 'target');
33 assert.property(clip, 'text');
34 assert.property(clip, 'trigger');
35 assert.property(clip, 'selectedText');
36 });
37 });
38
39 describe('#initSelection', () => {
40 it('should set the position right style property', (done) => {
41 // Set document direction
42 document.documentElement.setAttribute('dir', 'rtl');
43
44 let clip = new ClipboardAction({
45 emitter: new Emitter(),
46 container: document.body,
47 text: 'foo',
48 });
49
50 const el = clip.createFakeElement();
51
52 assert.equal(el.style.right, '-9999px');
53 done();
54 });
55 });
56
57 describe('#set action', () => {
58 it('should throw an error since "action" is invalid', (done) => {
59 try {
60 let clip = new ClipboardAction({
61 text: 'foo',
62 action: 'paste',
63 });
64 } catch (e) {
65 assert.equal(
66 e.message,
67 'Invalid "action" value, use either "copy" or "cut"'
68 );
69 done();
70 }
71 });
72 });
73
74 describe('#set target', () => {
75 it('should throw an error since "target" do not match any element', (done) => {
76 try {
77 let clip = new ClipboardAction({
78 target: document.querySelector('#foo'),
79 });
80 } catch (e) {
81 assert.equal(e.message, 'Invalid "target" value, use a valid Element');
82 done();
83 }
84 });
85 });
86
87 describe('#selectText', () => {
88 it('should create a fake element and select its value', () => {
89 let clip = new ClipboardAction({
90 emitter: new Emitter(),
91 container: document.body,
92 text: 'blah',
93 });
94
95 const el = clip.createFakeElement();
96
97 assert.equal(clip.selectedText, el.value);
98 });
99 });
100
101 describe('#removeFake', () => {
102 it('should remove a temporary fake element', () => {
103 let clip = new ClipboardAction({
104 emitter: new Emitter(),
105 container: document.body,
106 text: 'blah',
107 });
108
109 clip.removeFake();
110
111 assert.equal(clip.fakeElem, null);
112 });
113 });
114
115 describe('#selectTarget', () => {
116 it('should select text from editable element', () => {
117 let clip = new ClipboardAction({
118 emitter: new Emitter(),
119 container: document.body,
120 target: document.querySelector('#input'),
121 });
122
123 assert.equal(clip.selectedText, clip.target.value);
124 });
125
126 it('should select text from non-editable element', () => {
127 let clip = new ClipboardAction({
128 emitter: new Emitter(),
129 container: document.body,
130 target: document.querySelector('#paragraph'),
131 });
132
133 assert.equal(clip.selectedText, clip.target.textContent);
134 });
135 });
136
137 describe('#copyText', () => {
138 before(() => {
139 global.stub = sinon.stub(document, 'execCommand');
140 });
141
142 after(() => {
143 global.stub.restore();
144 });
145
146 it('should fire a success event on browsers that support copy command', (done) => {
147 global.stub.returns(true);
148
149 let emitter = new Emitter();
150
151 emitter.on('success', () => {
152 done();
153 });
154
155 let clip = new ClipboardAction({
156 emitter,
157 target: document.querySelector('#input'),
158 });
159 });
160
161 it('should fire an error event on browsers that support copy command', (done) => {
162 global.stub.returns(false);
163
164 let emitter = new Emitter();
165
166 emitter.on('error', () => {
167 done();
168 });
169
170 let clip = new ClipboardAction({
171 emitter,
172 target: document.querySelector('#input'),
173 });
174 });
175 });
176
177 describe('#handleResult', () => {
178 it('should fire a success event with certain properties', (done) => {
179 let clip = new ClipboardAction({
180 emitter: new Emitter(),
181 container: document.body,
182 target: document.querySelector('#input'),
183 });
184
185 clip.emitter.on('success', (e) => {
186 assert.property(e, 'action');
187 assert.property(e, 'text');
188 assert.property(e, 'trigger');
189 assert.property(e, 'clearSelection');
190
191 done();
192 });
193
194 clip.handleResult(true);
195 });
196
197 it('should fire a error event with certain properties', (done) => {
198 let clip = new ClipboardAction({
199 emitter: new Emitter(),
200 container: document.body,
201 target: document.querySelector('#input'),
202 });
203
204 clip.emitter.on('error', (e) => {
205 assert.property(e, 'action');
206 assert.property(e, 'trigger');
207 assert.property(e, 'clearSelection');
208
209 done();
210 });
211
212 clip.handleResult(false);
213 });
214 });
215
216 describe('#clearSelection', () => {
217 it('should remove focus from target and text selection', () => {
218 let clip = new ClipboardAction({
219 emitter: new Emitter(),
220 container: document.body,
221 target: document.querySelector('#input'),
222 });
223
224 clip.clearSelection();
225
226 let selectedElem = document.activeElement;
227 let selectedText = window.getSelection().toString();
228
229 assert.equal(selectedElem, document.body);
230 assert.equal(selectedText, '');
231 });
232 });
233
234 describe('#destroy', () => {
235 it('should destroy an existing fake element', () => {
236 let clip = new ClipboardAction({
237 emitter: new Emitter(),
238 container: document.body,
239 text: 'blah',
240 });
241
242 clip.selectFake();
243 clip.destroy();
244
245 assert.equal(clip.fakeElem, null);
246 });
247 });
248});