Skip to content

Instantly share code, notes, and snippets.

@Szandor72
Forked from sjurgis/lts-unit-test.MD
Created April 20, 2018 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szandor72/5c21cefb8d53fa4185f3cafb06460c89 to your computer and use it in GitHub Desktop.
Save Szandor72/5c21cefb8d53fa4185f3cafb06460c89 to your computer and use it in GitHub Desktop.
How to do actual unit testing in Lightning Testing Service

Expose helper via an public aura:method function in your abstract component's testHelperController.js:

getHelper: function(component, event, helper) {
 if ( $T ) {
  return helper;
 } else {
  throw new Error('API is only supported in test context');
 }
}

testHelper.cmp:

<aura:component abstract="true" extensible="true">
 <aura:method name="getHelper"/>
</aura:component>	

Embed it in your componentToTest.cmp:

<aura:component extends="testHelper">
  <lightning:button onclick="{!c.buttonClick}"/>
</aura:component>	

buttonClick calls componentToTestController.js

  buttonClick: function(component, event, helper) {
    var string = helper.concatStrings(['fizz','buzz'], ', ')
  }

And it's componentToTestHelper.js where code actually is:

concatStrings : function(strings, delim) {
     var string = strings.join(delim)
     return string
    }

And then call it in your test:

describe("componentToTest helpers test", function(){ 
 describe('test componentToTest helper method "concatStrings"', function() {
   it('Tests how strings are concatenated', function(done) {
     $T.createComponent("c:componentToTest", {}, true).then(function(component) {
       var string = component.getHelper().concatStrings(['foo','baz','qux'],', ')
       expect(string).toBe('foo, baz, qux')
       done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment