Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Testing Callbacks

Yuriy Sannikov edited this page Jul 23, 2017 · 1 revision

Sometimes you have to test code callbacks. One of use cases might be $A.createComponent. A callback for createComponent do some initialization and need to be tested.

// Make a call to helper method
helper.initializeContactLookupField(component);
//Use calledWithMatch to omit some parameters (here callback itself) 
expect(global.$A.createComponent).to.have.been.calledWithMatch('markup://Framework:InputFields',
    lookupParametersBuilder({line})
);

// Use ES6 syntax to get callback
//// basically this is the same: const callback = global.$A.createComponent.getCalls()[0].args[2];
const [{args: [,, callback]}, ...rest] = global.$A.createComponent.getCalls()
// Prepare component for the callback (a component which should be created by $A.createComponent)
const createdComponent = componentFactory({
    'aura:id': 'lookup'
});

// Invoke callback (defined in helper file, 3rd argument to $A.createComponent)
callback(createdComponent);

// Test logic of that callback below:
expect(createdComponent.get('v.value')).to.eql(Object.assign({}, line, {contactId: line.assignments[0].contactId}));
expect(component.get('v.contactGlobalId')).to.equal('global-lookup');
expect(lookupDiv.get('v.body')).to.eql([createdComponent]);
Clone this wiki locally