Skip to content

Instantly share code, notes, and snippets.

@rehnen
Created July 26, 2016 20:31
Show Gist options
  • Save rehnen/cd690180b9289102e7aa5401edd0f90c to your computer and use it in GitHub Desktop.
Save rehnen/cd690180b9289102e7aa5401edd0f90c to your computer and use it in GitHub Desktop.
mocha chai sinon async test
<html>
<head>
<title>This is a test page for testing tests</title>
<link rel="stylesheet" type="text/css" href="http://cdnjs.cloudflare.com/ajax/libs/mocha/1.20.1/mocha.css">
</head>
<body>
<script src="http://cdnjs.cloudflare.com/ajax/libs/mocha/1.20.1/mocha.js" type="application/ecmascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/chai/1.9.1/chai.min.js" type="application/ecmascript"></script>
<script src="http://sinonjs.org/releases/sinon-1.10.3.js" type="application/ecmascript"></script>
<div id="asd"></div>
<div id="mocha"></div>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
//your code to run since DOM is loaded and ready
//console.log(mocha, chai, sinon, decribe);
mocha.setup('bdd');
var expect = chai.expect,
assert = chai.assert;
console.log(describe);
describe("trying to write some test code", function() {
it("all test libs should be imported", function() {
expect(chai).not.to.be.undefined;
expect(mocha).not.to.be.undefined;
expect(sinon).not.to.be.undefined;
});
it("shal be able to handle xhr", function(done) {
var server = sinon.fakeServer.create();
server.autoRespond = true;
server.respondWith(/rest/, function(xhr) {
xhr.respond(200, {}, JSON.stringify([]));
});
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("asd").innerHTML = "fisk";
expect(document.getElementById("asd").innerHTML).to.equal("fisk");
console.log(123);
done();
}
};
xhttp.open("GET", "rest/fisk");
xhttp.send();
setTimeout(function(){
expect(1).to.equal(1);
}, 1000);
});
it("shal be able to handle xhr", function(done) {
var server = sinon.fakeServer.create();
server.autoRespond = true;
server.respondWith(/rest/, function(xhr) {
xhr.respond(200, {}, JSON.stringify([]));
});
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("asd").innerHTML = "potato";
expect(document.getElementById("asd").innerHTML).to.equal("potato");
console.log(123);
//done();
}
};
xhttp.open("GET", "rest/fisk");
xhttp.send();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("asd").innerHTML = "potato2";
expect(document.getElementById("asd").innerHTML).to.equal("potato2");
console.log(123);
done();
}
};
xhttp.open("GET", "rest/fisk");
xhttp.send();
setTimeout(function(){
expect(1).to.equal(1);
}, 1000);
});
});
mocha.run();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment