Forked from Victor-Barcelo/Mocking Ext.Ajax with Jasmine spies
Created
July 14, 2017 13:38
-
-
Save jaderdev/5af941affab9bbf6cef635ad35770d53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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