Skip to content

Instantly share code, notes, and snippets.

@Victor-Barcelo
Created December 15, 2013 18:23
Show Gist options
  • Save Victor-Barcelo/7976326 to your computer and use it in GitHub Desktop.
Save Victor-Barcelo/7976326 to your computer and use it in GitHub Desktop.
describe("Mocking Ext.Ajax.request with Jasmine", function () {
var responseText,
mockResponse = {
responseText: '{"success":true,"count":1,"data":[{name: "id", "type": "int", "maxLenght": "11", "isPrimaryKey": true}]}'
};
it("should mock the Ext.Ajax.request response manipulating the spy mostRecentCall.args", function () {
spyOn(Ext.Ajax, 'request');
Ext.Ajax.request({
url : '/testurl',
params : {
dbName : 'test',
tableName: 'table1'
},
success: function (response, opts) {
responseText = Ext.JSON.decode(response.responseText);
}
});
Ext.Ajax.request.mostRecentCall.args[0].success(mockResponse);
expect(responseText.success).toBe(true);
expect(responseText.count).toBe(1);
expect(responseText.data[0].type).toBe("int");
});
it("should mock a Ext.Ajax.request function wrapper returning the response via callback", function () {
var callbackFn = function (response) {
responseText = Ext.JSON.decode(response.responseText);
};
var requestFunction = function (callback) {
console.log("This text will never be logged out");
Ext.Ajax.request({
});
};
requestFunction = jasmine.createSpy().andCallFake(function (callback) {
callback(mockResponse);
}
);
requestFunction(callbackFn);
expect(responseText.success).toBe(true);
expect(responseText.count).toBe(1);
expect(responseText.data[0].type).toBe("int");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment