When it comes to frontend unit testing using Jasmine, one of the challenges I faced was mocking all the requests in my application.
Luckily, I have already tackled a method to mock all my proxies successfully:
proxy: appname.classes.proxy.ProxyNegotiator.getModelProxy("User")
This method essentially determines the type of proxy based on configuration settings:
getModelProxy: function(config) {
var url = this.getUrl(config);
if (this.getProxyType() == 'api') {
return appname.classes.proxy.WebApiProxy.getModelProxy(url);
} else if (this.getProxyType() == 'test') {
return appname.classes.proxy.LocalTestProxy.getModelProxy(url);
}
return undefined;
}
By configuring the ProxyType, I am able to seamlessly switch between web api proxy and local test proxy for CRUD operations.
However, another issue that arises is dealing with other types of requests in the application, such as validating usernames:
//check if Username is Valid
Ext.Ajax.request({
url: '/api/User',
method: 'GET',
async: false,
params: { id: user.get('Id'), username: user.get('UserName') },
failure: function(response, opts) {
myErrors.push({
field: 'UserName',
message: appname.locale.ErrorInvalidUsername
});
}
});
Unfortunately, I am struggling with mocking these Ext.Ajax.request calls. Despite searching extensively online, I haven't found a satisfactory solution yet. Any advice or ideas on best practices for mocking such requests would be highly appreciated. Please assist!