UNPKG

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