Skip to content

Instantly share code, notes, and snippets.

@sjurgis
Created April 18, 2018 07:20
Show Gist options
  • Save sjurgis/ba80ac0245f03de05877cdd6aa9a06bf to your computer and use it in GitHub Desktop.
Save sjurgis/ba80ac0245f03de05877cdd6aa9a06bf 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