Verifying the response by mocking the server response using Jasmine spy

The next it function that we want to write tests the response from the server. Since we do not want to perform the actual API calls in the test method, we use mock data. We will use the spy function with the callFake strategy (this strategy is provided by the Jasmine framework) to perform testing.

Another thing to note in this example is that we have used the Lightning:button component, which is in the Lightning namespace, and hence it is currently not supported in the LTS service to simulate the click event. However, we can work around this by creating an aura:method that calls the function that is invoked on a button click. 

To help us with our testing, we add the following lines to our YouTubeSearch component, as shown in the following code:

<aura:method name="search" action="{!c.handleClick}"/>

The test spec for this use case is shown in the following code. The complete code can be found at the Git repository at https://github.com/PacktPublishing/Learning-Salesforce-Lightning-Application-Development/blob/master/Chapter14/force-app/main/default/staticresources/YoutubeSearchAppTest.resource:

describe("YoutubeSearchAppTest", function(){ 

var responseObject ;

afterEach(function () {
$T.clearRenderedTestComponents();
});

beforeEach(function (){
//provide mock response here
responseObject = { }
});

describe("A Suite that tests youtube search component functionality", function() {
describe('c:youtubesearch', function () {
it('verify component rendering for search test', function (done) {
$T.createComponent('c:youtubesearch', {searchTerm :"searchString"}, true)
.then(function(component) {
expect(component.find("searchTermRendered").getElement().innerHTML).toBe(' You searched for searchString');
done();
}).catch(function (e) {
done.fail(e);
});
});
//Tests for the server Response
it('verify that server methods were called', function (done) {
$T.createComponent('c:youtubesearch', {searchTerm :"searchString"}, true)
.then(function(component) {
var mockResponse = {
getState: function () {
return "SUCCESS";
},
getReturnValue: function () {
return JSON.stringify(responseObject);
}
};
spyOn($A, "enqueueAction").and.callFake(function (action) {
var cb = action.getCallback("SUCCESS");
cb.fn.apply(cb.s, [mockResponse]);
});
component.search();
expect(component.get("v.data").items.length).toBe(2);
done();
}).catch(function (e) {
done.fail(e);
});
});
});
});

});

Observe the code in bold lines. We added the following elements to these lines:

  • We added the mock response data in the beforeEach() so that we can use this to test against without having to call the service:
beforeEach(function (){
responseObject = {} //dummy data
  • We will leverage the spyOn  function for the  enqueueAction method on the $A object to return a fakeresponse, as shown in the following code:
$T.createComponent('c:youtubesearch', {searchTerm :"searchString"}, true)
.then(function(component) {
var mockResponse = {
getState: function () {
return "SUCCESS";
},
getReturnValue: function () {
return JSON.stringify(responseObject);
}
};
spyOn($A, "enqueueAction").and.callFake(function (action) {
var cb = action.getCallback("SUCCESS");
cb.fn.apply(cb.s, [mockResponse]);
});
//call the search method here

On a successful run, you will see the following output on the CLI:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.208.97