Skip to content

Commit 5a0b97b

Browse files
author
Ari Madian
authored
Issue #34 - Unit and e2e tests for CopyButton component (#46)
Issue #34 - Unit and e2e tests for CopyButton component
2 parents 8ed120e + 888eac3 commit 5a0b97b

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"babel-register": "^6.22.0",
4646
"chalk": "^2.0.1",
4747
"chromedriver": "^2.27.2",
48+
"clipboardy": "^2.1.0",
4849
"copy-webpack-plugin": "^4.0.1",
4950
"cross-spawn": "^5.0.1",
5051
"css-loader": "^0.28.0",
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const clipboardy = require('clipboardy');
2+
3+
module.exports = {
4+
'Copybutton.vue - testing the #copy-richtext-btn flow': function (browser) {
5+
browser
6+
.url(browser.globals.devServerURL)
7+
.waitForElementVisible('#app', 5000)
8+
.assert.elementPresent('#copy-richtext-btn')
9+
.assert.elementPresent('#attribution-richtext')
10+
.click('#copy-richtext-btn')
11+
.assert.containsText('#copy-richtext-btn', "Copied!")
12+
.getText('#attribution-richtext', function (result) {
13+
this.assert.equal(result.value, clipboardy.readSync().trim());
14+
})
15+
.expect.element('#copy-richtext-btn').text.to.equal('Copy Rich Text').after(3000);
16+
},
17+
18+
'Copybutton.vue - testing the #copy-html-btn flow': function (browser) {
19+
browser
20+
.url(browser.globals.devServerURL)
21+
.waitForElementVisible('#app', 5000)
22+
.assert.elementPresent('#copy-html-btn')
23+
.assert.elementPresent('#attribution-html')
24+
.click('#copy-html-btn')
25+
.assert.containsText('#copy-html-btn', "Copied!")
26+
.getValue('#attribution-html', function (result) {
27+
this.assert.equal(result.value, clipboardy.readSync().trim());
28+
})
29+
.expect.element('#copy-html-btn').text.to.equal('Copy HTML').after(3000);
30+
}
31+
}
32+
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import Vue from 'vue'
2+
import { mount } from '@vue/test-utils'
3+
import CopyButton from '@/components/CopyButton.vue'
4+
import { doesNotThrow } from 'assert';
5+
6+
const DOM_SOURCE_ID = 'el-test';
7+
const DOM_SOURCE_VALUE = 'test clipboard value';
8+
9+
const slots = { default: 'Copy button' };
10+
const propsData = { id: 'copy-button', el: `#${DOM_SOURCE_ID}` }
11+
const defaults = {
12+
slots,
13+
propsData,
14+
attachToDocument: true,
15+
}
16+
17+
let wrapper
18+
19+
// mock dom source
20+
function _mockDOMSource(value) {
21+
// creates the span and append to JSDOM
22+
const span = document.createElement('span')
23+
span.setAttribute('id', DOM_SOURCE_ID);
24+
span.textContent = value;
25+
document.body.appendChild(span)
26+
}
27+
28+
// mock dom methods
29+
function _mockDomMethodsForClipboardJS(value) {
30+
window.getSelection = () => ({
31+
addRange: () => {},
32+
removeAllRanges: () => {},
33+
toString: () => value,
34+
});
35+
36+
document.execCommand = () => value;
37+
document.createRange = () => ({ selectNodeContents: () => { } });
38+
}
39+
40+
beforeEach(() => {
41+
_mockDOMSource(DOM_SOURCE_VALUE)
42+
_mockDomMethodsForClipboardJS(DOM_SOURCE_VALUE)
43+
wrapper = mount(CopyButton, defaults)
44+
})
45+
46+
afterEach(() => {
47+
wrapper.destroy()
48+
})
49+
50+
describe('CopyButton.vue', () => {
51+
it('should render the right content text', () => {
52+
expect(wrapper.text()).toBe(slots.default);
53+
})
54+
55+
it('should change the text when clicked to \'Copied!\' and 2 secs later swich back to the original text', done => {
56+
wrapper.trigger('click');
57+
expect(wrapper.text()).toBe('Copied!');
58+
setTimeout(() => {
59+
expect(wrapper.text()).toBe(slots.default);
60+
done();
61+
}, 2000);
62+
})
63+
64+
it('should emit only the \'copied\' event with a valid value', () => {
65+
wrapper.trigger('click');
66+
67+
const emittedEvents = wrapper.emitted()
68+
expect(emittedEvents).toHaveProperty('copied');
69+
expect(emittedEvents.copied).toBeTruthy();
70+
expect(emittedEvents.copied.length).toBe(1);
71+
expect(emittedEvents.copied[0].length).toBe(1);
72+
expect(emittedEvents.copied[0][0].content).toBe(DOM_SOURCE_VALUE);
73+
expect(emittedEvents.copyFailed).toBeFalsy();
74+
})
75+
76+
it('should emit only the \'copyFailed\' event', () => {
77+
document.execCommand = () => false // force error on clipboard.js
78+
wrapper.trigger('click');
79+
80+
const emittedEvents = wrapper.emitted()
81+
expect(emittedEvents).toHaveProperty('copyFailed');
82+
expect(emittedEvents.copied).toBeFalsy();
83+
})
84+
})

0 commit comments

Comments
 (0)