summaryrefslogtreecommitdiff
path: root/js/test/main.js
blob: e89821025d0c4930cd5bd6a71fad57789a0c86ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const ava = require("ava");
const sinon = require("sinon");

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(){};
  global.alert = console.log;

  // Load back-office code
  t.context.bo = require("../backoffice");
});

// Remove mocks from global scope
ava.test.afterEach(t => {
  // Is this done by the garbage collector anyway?
  delete global.XMLHttpRequest;
  delete global.document;
  delete global.alert;
  delete t.context.xhr;
  delete t.context.requests;
});

ava.test("Order id not found", (t) => {
  /**
   * FIXME: improve the reaction to a order id not found.
   * Alerting a message is not acceptable.
   */
  var alert = sinon.spy(global, "alert");
  t.context.bo.track_order(22);
  t.context.requests[0].respond(404);
  sinon.assert.calledWith(alert, "Order ID unknown");
  t.pass();
});