Testing application events

The $T global function provides a utility method called $T.fireApplicationEvent to fire an event when testing components that handle the application event.

The code snippet to fire an application event from the testing service is as follows:

$T.fireApplicationEvent(eventName, eventParams)

The following is a sample code snippet that shows how to test a component that handles the application event:

describe('c:componentsubscribingToAppEvent', function () {
it('verify application event', function (done) {
$T.createComponent("c:componentsubscribingToAppEvent")
.then(function (component) {
$T.fireApplicationEvent("c:applicationEvent", {"eventData": "sample event Data"});
expect(component.get("v.eventDataCaptured")).toBe("sample event Data");
done();
}).catch(function (e) {
done.fail(e);
});
});
});

The $T utility function provides other methods, and you can explore the code in the open source repository at https://github.com/forcedotcom/LightningTestingService/blob/master/Lightning-component-tests/test/default/staticresources/lts_testutil.resource.

Also, for more advanced use cases, such as testing for Lightning Data Services, you can refer to the sample code provided in the repository at https://github.com/forcedotcom/LightningTestingService/blob/master/Lightning-component-tests/test/default/staticresources/jasmineLightningDataServiceTests.resource.

For actions that involve the DML operation in Salesforce, if you do not spy and substitute with the mock data, the test data will not be rolled back. Hence, you should always make sure that you are not running these tests in production and consider using this only in scratch Orgs or sandboxes.
..................Content has been hidden....................

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