mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Adds karma test suite
This commit is contained in:
parent
56dd1aac22
commit
1febe4eecc
23
karma.conf.js
Normal file
23
karma.conf.js
Normal file
@ -0,0 +1,23 @@
|
||||
module.exports = function(karma) {
|
||||
karma.set({
|
||||
plugins: ['karma-browserify', 'karma-chai', 'karma-mocha', 'karma-phantomjs-launcher'],
|
||||
|
||||
frameworks: ['browserify', 'chai', 'mocha'],
|
||||
|
||||
files: [
|
||||
'src/**/*.js',
|
||||
'test/**/*.js'
|
||||
],
|
||||
preprocessors: {
|
||||
'src/**/*.js' : ['browserify'],
|
||||
'test/**/*.js': ['browserify']
|
||||
},
|
||||
|
||||
browserify: {
|
||||
debug: true,
|
||||
transform: ['babelify']
|
||||
},
|
||||
|
||||
browsers: ['PhantomJS']
|
||||
});
|
||||
}
|
@ -15,6 +15,11 @@
|
||||
"babelify": "^6.3.0",
|
||||
"browserify": "^11.1.0",
|
||||
"chai": "^3.2.0",
|
||||
"karma": "^0.13.10",
|
||||
"karma-browserify": "^4.3.0",
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-mocha": "^0.2.0",
|
||||
"karma-phantomjs-launcher": "^0.2.1",
|
||||
"mocha": "^2.3.2",
|
||||
"uglify": "^0.1.5"
|
||||
},
|
||||
|
42
test/clipboard-action.js
Normal file
42
test/clipboard-action.js
Normal file
@ -0,0 +1,42 @@
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
|
||||
describe('ClipboardAction', () => {
|
||||
before(() => {
|
||||
global.target = document.createElement('input');
|
||||
target.setAttribute('id', 'foo');
|
||||
document.body.appendChild(global.target);
|
||||
|
||||
global.trigger = document.createElement('button');
|
||||
trigger.setAttribute('class', 'btn');
|
||||
document.body.appendChild(global.trigger);
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since "data-action" is invalid', (done) => {
|
||||
try {
|
||||
new Clipboard({
|
||||
action: 'paste'
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since "data-target" do not match any element', (done) => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
target: 'zzz',
|
||||
trigger: global.trigger
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
});
|
@ -1,6 +1,12 @@
|
||||
var assert = chai.assert;
|
||||
import Clipboard from '../src/clipboard';
|
||||
|
||||
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 {
|
||||
@ -20,51 +26,13 @@ describe('Clipboard', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should create a NodeList and store it in a property', () => {
|
||||
it('should create a NodeList from selector and store it in a "triggers" property', () => {
|
||||
let clipboard = new Clipboard('.btn');
|
||||
return assert.instanceOf(clipboard.triggers, NodeList);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#validate', () => {
|
||||
var elem = document.createElement('input');
|
||||
let clipboard = new Clipboard('.btn');
|
||||
|
||||
it('should throw an error since there was no "data-target" or "data-text"', (done) => {
|
||||
try {
|
||||
clipboard.validate({
|
||||
currentTarget: elem
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since "data-action" is invalid', (done) => {
|
||||
try {
|
||||
elem.setAttribute('data-action', 'paste');
|
||||
|
||||
clipboard.validate({
|
||||
currentTarget: elem
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since "data-target" do not match any element', (done) => {
|
||||
try {
|
||||
elem.setAttribute('data-target', 'xyz');
|
||||
|
||||
clipboard.validate({
|
||||
currentTarget: elem
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
});
|
||||
|
@ -1,45 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Mocha Tests</title>
|
||||
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
|
||||
<div>
|
||||
<p>Copy text from attribute</p>
|
||||
<button class="btn" data-action="copy" data-text="hello">Copy</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p>Copy text from non-input element</p>
|
||||
<p id="paragraph">hello</p>
|
||||
<button class="btn" data-action="copy" data-target="paragraph">Copy</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p>Copy text from input element</p>
|
||||
<input id="foo" type="text" value="hello">
|
||||
<button class="btn" data-action="copy" data-target="foo">Copy</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p>Cut text from textarea element</p>
|
||||
<textarea id="bar">hello</textarea>
|
||||
<button class="btn" data-action="cut" data-target="bar">Cut</button>
|
||||
</div>
|
||||
|
||||
<script src="../dist/clipboard.min.js"></script>
|
||||
<script src="../node_modules/mocha/mocha.js"></script>
|
||||
<script src="../node_modules/chai/chai.js"></script>
|
||||
|
||||
<script>mocha.setup('bdd')</script>
|
||||
<script src="clipboard.js"></script>
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.globals(['Clipboard']);
|
||||
mocha.run();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user