summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2018-01-10 13:15:19 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2018-01-10 13:15:19 +0100
commita260f2755ba9532cad599b1a5374b694f169ea57 (patch)
tree1133e93599cfea5b2acbcbf2bb98ec2c05259794
parent29bda13eeba3b26a0e49e8d114cf6c5e2e6857ed (diff)
downloadbackoffice-a260f2755ba9532cad599b1a5374b694f169ea57.tar.gz
backoffice-a260f2755ba9532cad599b1a5374b694f169ea57.tar.bz2
backoffice-a260f2755ba9532cad599b1a5374b694f169ea57.zip
Define "set up" and "tear down" methods for tests
-rw-r--r--js/test/main.js36
1 files changed, 25 insertions, 11 deletions
diff --git a/js/test/main.js b/js/test/main.js
index 777c020..2ded74e 100644
--- a/js/test/main.js
+++ b/js/test/main.js
@@ -1,19 +1,33 @@
const ava = require("ava");
const sinon = require("sinon");
-var document = global.document = {};
-document.addEventListener = function(){};
-const bo = require("../backoffice");
-
-ava.test("orders tracking", (t) => {
- var xhr = sinon.useFakeXMLHttpRequest();
- var requests = [];
- xhr.onCreate = function(xhr){
- requests.push(xhr);
+ava.test.beforeEach(t => {
+ // Global XHR mock
+ t.context.xhr = sinon.useFakeXMLHttpRequest();
+ t.context.requests = [];
+ t.context.xhr.onCreate = function(xhr){
+ t.context.requests.push(xhr);
};
+ global.XMLHttpRequest = t.context.xhr;
+
+ // Global 'document' mock
+ global.document = {};
+ global.document.addEventListener = function(){};
+
+ // Load back-office code
+ t.context.bo = require("../backoffice");
+});
+
+// Remove mocks from global scope
+ava.test.afterEach(t => {
+ delete global.XMLHttpRequest;
+ delete global.document;
+});
- bo.track_order(22); // Throws: XMLHttpRequest is not defined.
+ava.test("orders tracking", (t) => {
+ t.context.bo.track_order(22);
- xhr.restore();
+ // Clean up
+ t.context.xhr.restore();
t.pass();
});