I have added it to jet Book.
webix
is not stored in JetApp instances, but it’s stored in JetView instances. You can refer to it as a global variable in these tests due to the setup.
I am sorry for some misinformation about async data in tests. Seems that Mocha does not work with network and you have to add something else to imitate network activity. For example, Sinon. There’s also the issue with asynchronisity in Mocha: you must call done
when testing is done (alternatives would be to return a promise or to use async/await).
For imitating webix.ajax() request that gets some data, I wrote this very simple example using sinon.fake
:
var chai = require("chai");
var expect = chai.expect;
var sinon = require("sinon");
const data = [
{ id:1, title:"The Shawshank Redemption", year:1994, votes:678790, rating:9.2, rank:1},
{ id:2, title:"The Godfather", year:1972, votes:511495, rating:9.2, rank:2},
{ id:3, title:"The Godfather: Part II", year:1974, votes:319352, rating:9.0, rank:3},
{ id:4, title:"The Good, the Bad and the Ugly", year:1966, votes:213030, rating:8.9, rank:4},
{ id:5, title:"My Fair Lady", year:1964, votes:533848, rating:8.9, rank:5},
{ id:6, title:"12 Angry Men", year:1957, votes:164558, rating:8.9, rank:6}
];
describe("model: records async", function() {
before(function(){
sinon.replace(webix, "ajax", sinon.fake.returns(Promise.resolve(data)));
});
after(function () {
sinon.restore();
});
it("has some data", function(done) {
webix.ajax("some/path").then(data => {
// since my fake returns a ready array, no need to call .json() as you would with the real webix.ajax()
expect(data.length).to.be.above(0);
done();
});
});
});
For more detailed testing, you can also create a fake server (Sinon docs).