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

Mock component.find calls

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

In this example componentFactory supplied with findMap object. It's a special object used to mock component.find() call. Here component.find('numberOfGuests'); returns null and no component.get method will be called.

Spec code

it('should bail out if no guests amount input available', () => {
  const component = componentFactory({
    findMap: {numberOfGuests: null}
  });
  helper.setupGuestsInput(component);
  expect(component.get).to.have.not.been.called;
})

Tested code

setupGuestsInput: function(component, ticket) {
  var cmp = component.find('numberOfGuests');
  if (!cmp) {
    return;
  }
  if (!ticket) {
    // Attendee has no ticket type associated
 component.find('rsvpAttendee').set('v.disabled', true);
    return;
  }

  var attendeeObj = component.get('v.attendeeObj');
  ....
},

Instead of passing null value you can pass actual component with all properties and methods set up

Spec code

it('should bail out if no guests amount input available', () => {
  const numberOfGuests = componentFactory({value: 2})
  const component = componentFactory({
    findMap: {numberOfGuests}
  });
  helper.setupGuestsInput(component);
  expect(component.get).to.have.been.called;
  ....
})

Or you can simply pass true or component adapter name (you have to register component adapted upfront with useComponentAdapters call). In this case mocha-aura automatically create and store component for you.

Spec code

it('should bail out if no guests amount input available', () => {
  const component = componentFactory({
    findMap: {numberOfGuests: true, nextButton: 'Framework:Button'}
  });
  helper.setupGuestsInput(component);
  expect(component.get).to.have.been.called;
  ....
})
Clone this wiki locally