Skip to content

Instantly share code, notes, and snippets.

@zhjuncai
Last active October 17, 2017 10:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhjuncai/85dd0e42bfa6a7e092a1 to your computer and use it in GitHub Desktop.
Save zhjuncai/85dd0e42bfa6a7e092a1 to your computer and use it in GitHub Desktop.
SAPUI5 JSONModel Test case example
test("test JSONModel destroy", function(){
var testModel = new sap.ui.model.json.JSONModel();
testModel.attachRequestCompleted(function() {
ok(false, "Request should be aborted!");
});
testModel.attachRequestFailed(function() {
ok(false, "Error handler should not be called when request is aborted via destroy!");
});
var spy = this.spy(jQuery, "ajax");
testModel.loadData("testdata.json");
testModel.destroy();
ok(testModel.bDestroyed, "Model should be destroyed");
equal(spy.callCount, 1, "number of requests should be still 1");
equal(spy.getCall(0).returnValue.statusText, "abort", "should be abort");
});
test("test JSONModel loadData after destroy", function(){
//var server = this.sandbox.useFakeServer();
//server.respondWith("data.json", function(xhr, id) {
//ok(false, "Request should not be sent after calling destroy!");
//xhr.respond(200, { "Content-Type": "application/json" }, '{ "test": "data" }');
//});
var spy = this.spy(jQuery, "ajax");
var testModel = new sap.ui.model.json.JSONModel();
testModel.attachRequestCompleted(function() {
ok(false, "Request should be aborted!");
});
testModel.attachRequestFailed(function() {
ok(false, "Error handler should not be called when request is aborted via destroy!");
});
testModel.loadData("testdata.json", null, true);
testModel.destroy();
ok(testModel.bDestroyed, "Model should be destroyed");
equal(spy.callCount, 1, "number of requests");
equal(spy.getCall(0).returnValue.statusText, "abort", "should be abort");
// call loaddata again
testModel.loadData("testdata.json", null, true);
ok(testModel.bDestroyed, "Model should be destroyed");
equal(spy.callCount, 1, "number of requests should be still 1");
equal(spy.getCall(0).returnValue.statusText, "abort", "should be abort");
});
test("tap", function(){
// system under test
var oCheckBox = new sap.m.CheckBox();
var oSpy = this.spy();
oCheckBox.attachSelect(oSpy);
// arrange
oCheckBox.placeAt("content");
sap.ui.getCore().applyChanges();
// assertions
equal(oCheckBox.getSelected(), false, "CheckBox should not be selected");
strictEqual(oCheckBox.$().attr("aria-checked"), "false", "Property 'aria-checked': Default value should be 'false'");
qutils.triggerEvent("tap", oCheckBox.getId());
ok(oSpy.calledOnce, "Event 'select' should have been fired");
equal(oCheckBox.getSelected(), true, "CheckBox should be selected");
strictEqual(oCheckBox.$().attr("aria-checked"), "true", "Property 'aria-checked': Default value should be 'true'");
qutils.triggerEvent("tap", oCheckBox.getId());
ok(oSpy.calledTwice, "Event 'select' should have been fired");
equal(oCheckBox.getSelected(), false, "CheckBox should not be selected");
oCheckBox.setEditable(false);
qutils.triggerEvent("tap", oCheckBox.getId());
ok(oSpy.calledTwice, "Event 'select' should have been fired");
equal(oCheckBox.getSelected(), false, "CheckBox should not be selected");
// cleanup
oCheckBox.destroy();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment