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