diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2018-01-10 13:15:19 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2018-01-10 13:15:19 +0100 |
commit | a260f2755ba9532cad599b1a5374b694f169ea57 (patch) | |
tree | 1133e93599cfea5b2acbcbf2bb98ec2c05259794 | |
parent | 29bda13eeba3b26a0e49e8d114cf6c5e2e6857ed (diff) | |
download | backoffice-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.js | 36 |
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(); }); |