how to mock ajax call with sencha test with extjs

521 Views Asked by At

I have to veryfiy the response of the ajax call in my sencha test. plz advise how to do it.. below is my sample code

beforeEach(()=> {

    sim = Ext.ux.ajax.SimManager.init({});
    controller = Ext.create('xxxx.controller.Search');
    AutoLink = Ext.create('xxxx.model.search.AutoLink', {
        objectType: 'myobj'
    });
});
it('Should run processResponse when doSearch executes', function() {
    const callback = () => {};

    sim.register({
        'abc.com/myurl.aspx': {
            status: 401,
            responseText: JSON.stringify({
                'success': true,
                'data': [{
                    'autoLink': false, 'status': 'OK', 'objectType': 'Person',
                    'results': [{ 'ref': 12345, 'managedBy': '01', 'ethnicAppearance': '1', 'gender': '1', 'rules': ['Forename, surname','nickname, DOB']}],
                    'gridDescriptor': [{'fields': [{'name': 'surname','text': 'Surname','width': 100}],
                        'sortOrders': ['surname','forename1']
                    }]
                }]
            })
        }          
    });

    spyOn(controller, 'doSearch'); // internal method which calls the Ext.Ajax
    spyOn(controller, 'processResponse'); // internal method which process the response        

    controller.doSearch(AutoLink, callback, this); // making an ajax call

    setTimeout(function () {
        expect(controller.processResponse).toHaveBeenCalled();
    }, 1000);
});

now when run the test case processResponse gets called, which is fine, but i want to verify the ajax response.

1

There are 1 best solutions below

0
On

This is how I am doing it:

$.ajax({
                           url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('Test%203')/items(" + itemId + ")/FieldValuesAsText",
                           method: 'GET',
                           headers: {
                               'accept': 'application/json;odata=verbose'
                           }
                       }).then(function (data) {
                           console.log(data);
                          }

I don't know if this will help you to achieve exactly what you are looking for. But I would suggest giving it a shot. Then you can go to your console and save the data object to a variable (just for debugging purposes) or just from the console itself look at the object chain and check the data which was returned by your ajax call. So in my case I would find let's say name of the employee here:- data.d.results[0].PreferredName. Then if I want to use it I can just save it in a variable. Make sure you do it in the 'then' function. Here's a sample for save the name to a var:

.then(function (data) {
     empName = data.d.results[0].PreferredName;
 }