mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Improves test coverage
This commit is contained in:
parent
467684333f
commit
b0e118f750
@ -1,8 +1,8 @@
|
||||
module.exports = function(karma) {
|
||||
karma.set({
|
||||
plugins: ['karma-browserify', 'karma-chai', 'karma-mocha', 'karma-phantomjs-launcher'],
|
||||
plugins: ['karma-browserify', 'karma-chai', 'karma-sinon', 'karma-mocha', 'karma-phantomjs-launcher'],
|
||||
|
||||
frameworks: ['browserify', 'chai', 'mocha'],
|
||||
frameworks: ['browserify', 'chai', 'sinon', 'mocha'],
|
||||
|
||||
files: [
|
||||
'src/**/*.js',
|
||||
|
@ -2,6 +2,7 @@
|
||||
"name": "clipboard",
|
||||
"version": "0.0.0",
|
||||
"description": "A modern approach to copy text to clipboard",
|
||||
"repository": "zenorocha/clipboard.js",
|
||||
"main": "src/clipboard.js",
|
||||
"keywords": [
|
||||
"clipboard",
|
||||
@ -21,6 +22,7 @@
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-mocha": "^0.2.0",
|
||||
"karma-phantomjs-launcher": "^0.2.1",
|
||||
"karma-sinon": "^1.0.4",
|
||||
"mocha": "^2.3.2",
|
||||
"phantomjs-polyfill": "0.0.1",
|
||||
"uglify": "^0.1.5"
|
||||
@ -28,6 +30,7 @@
|
||||
"scripts": {
|
||||
"publish": "npm run build && npm run minify",
|
||||
"build": "browserify src/clipboard.js -t [babelify --loose all] -o dist/clipboard.min.js",
|
||||
"minify": "uglify -s dist/clipboard.min.js -o dist/clipboard.min.js"
|
||||
"minify": "uglify -s dist/clipboard.min.js -o dist/clipboard.min.js",
|
||||
"test": "karma start --single-run"
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,18 @@
|
||||
import Clipboard from '../src/clipboard-action';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
import Emitter from 'tiny-emitter';
|
||||
|
||||
describe('ClipboardAction', () => {
|
||||
before(() => {
|
||||
global.target = document.createElement('input');
|
||||
global.target.setAttribute('id', 'target');
|
||||
document.body.appendChild(global.target);
|
||||
global.input = document.createElement('input');
|
||||
global.input.setAttribute('id', 'input');
|
||||
global.input.setAttribute('value', 'abc');
|
||||
document.body.appendChild(global.input);
|
||||
|
||||
global.trigger = document.createElement('button');
|
||||
global.trigger.setAttribute('class', 'btn');
|
||||
document.body.appendChild(global.trigger);
|
||||
global.paragraph = document.createElement('p');
|
||||
global.paragraph.setAttribute('id', 'paragraph');
|
||||
global.paragraph.textContent = 'abc';
|
||||
document.body.appendChild(global.paragraph);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
@ -16,11 +20,11 @@ describe('ClipboardAction', () => {
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since both "data-text" and "data-target" were passed', (done) => {
|
||||
it('should throw an error since both "data-text" and "data-target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
target: 'target'
|
||||
target: 'input'
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
@ -29,7 +33,7 @@ describe('ClipboardAction', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since neither "data-text" nor "data-target" were passed', (done) => {
|
||||
it('should throw an error since neither "data-text" nor "data-target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
action: ''
|
||||
@ -40,8 +44,10 @@ describe('ClipboardAction', () => {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error since "data-action" is invalid', (done) => {
|
||||
describe('#set action', () => {
|
||||
it('should throw an error since "data-action" is invalid', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
@ -53,8 +59,10 @@ describe('ClipboardAction', () => {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error since "data-target" do not match any element', (done) => {
|
||||
describe('#set target', () => {
|
||||
it('should throw an error since "data-target" do not match any element', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
target: 'foo'
|
||||
@ -66,4 +74,142 @@ describe('ClipboardAction', () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#selectText', () => {
|
||||
it('should create a fake element and select its value', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
text: 'blah'
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.fakeElem.value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeFake', () => {
|
||||
it('should remove a temporary fake element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
text: 'blah'
|
||||
});
|
||||
|
||||
clip.removeFake();
|
||||
|
||||
assert.equal(clip.fakeElem, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#selectTarget', () => {
|
||||
it('should select text from editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: 'input'
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.value);
|
||||
});
|
||||
|
||||
it('should select text from non-editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: 'paragraph'
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.textContent);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#copyText', () => {
|
||||
before(() => {
|
||||
global.stub = sinon.stub(document, 'execCommand');
|
||||
});
|
||||
|
||||
after(() => {
|
||||
global.stub.restore();
|
||||
});
|
||||
|
||||
it('should fire a success event on browsers that support copy command', done => {
|
||||
global.stub.returns(true);
|
||||
|
||||
let emitter = new Emitter()
|
||||
|
||||
emitter.on('success', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: 'input'
|
||||
});
|
||||
});
|
||||
|
||||
it('should fire an error event on browsers that support copy command', done => {
|
||||
global.stub.returns(false);
|
||||
|
||||
let emitter = new Emitter()
|
||||
|
||||
emitter.on('error', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: 'input'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#handleResult', () => {
|
||||
it('should fire a success event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: 'input'
|
||||
});
|
||||
|
||||
clip.emitter.on('success', (e) => {
|
||||
assert.property(e, 'action');
|
||||
assert.property(e, 'text');
|
||||
assert.property(e, 'trigger');
|
||||
assert.property(e, 'clearSelection');
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
clip.handleResult(true);
|
||||
});
|
||||
|
||||
it('should fire a error event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: 'input'
|
||||
});
|
||||
|
||||
clip.emitter.on('error', (e) => {
|
||||
assert.property(e, 'action');
|
||||
assert.property(e, 'trigger');
|
||||
assert.property(e, 'clearSelection');
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
clip.handleResult(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#clearSelection', () => {
|
||||
it('should remove focus from target and text selection', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: 'input'
|
||||
});
|
||||
|
||||
clip.clearSelection();
|
||||
|
||||
let selectedElem = document.activeElement;
|
||||
let selectedText = window.getSelection().toString();
|
||||
|
||||
assert.equal(selectedElem, document.body);
|
||||
assert.equal(selectedText, '');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,12 +1,7 @@
|
||||
import Clipboard from '../src/clipboard';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
|
||||
describe('Clipboard', () => {
|
||||
before(() => {
|
||||
let button = document.createElement('button');
|
||||
button.setAttribute('class', 'btn');
|
||||
document.body.appendChild(button);
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since there was no arguments passed', done => {
|
||||
try {
|
||||
@ -29,7 +24,27 @@ describe('Clipboard', () => {
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
describe('#initialize', () => {
|
||||
before(() => {
|
||||
global.button = document.createElement('button');
|
||||
global.button.setAttribute('class', 'btn');
|
||||
global.button.setAttribute('data-text', 'foo');
|
||||
document.body.appendChild(global.button);
|
||||
|
||||
global.event = {
|
||||
delegateTarget: global.button
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
it('should create a new instance of ClipboardAction', () => {
|
||||
let clipboard = new Clipboard('.btn');
|
||||
|
||||
clipboard.initialize(global.event);
|
||||
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user