DjangoBlockchain
DjangoBlockchain

Reputation: 564

Return promises from Ember 2.16 Component Integration Tests Mocked action

I am creating tests for my Ember 2.16 application, and running into an issue where the code is dependent on a promise being returned from an external action.

const promise = this.sendAction('action')
promise.then(() => {
  //do stuff
});

A majority of my code is based in these .then and .catch conditionals on the promise, so I want to be able to return a promise that was successful and that failed. I have heard of Sinon, but unfortunately it is only for Ember 3.4 and above.

test('', function(assert){
  this.set('action', () => {
    // do assertions
    return new Promise(() => {return true} );
  });
});

Inside my integration test I am able to mock out the action, but I run into the "promise" being undefined. I have attempted to return Text, or other values, but when putting a debugger into the component the promise is always undefined.

I can get around this by adding a conditional that checks to see if there is a promise, but since the majority of my code is inside these .then and .catchconditionals I want my tests to step through these to increase the code coverage.

How would I return a promise from a mocked out action in an Integration Test?

Upvotes: 0

Views: 97

Answers (2)

DjangoBlockchain
DjangoBlockchain

Reputation: 564

It turns out I was returning the promise incorrectly.

return new Promise((reject) => {
  reject(true);
});

Was able to return the promise as "rejected".

Upvotes: 0

stevenelberger
stevenelberger

Reputation: 1368

You can stub your call to sendAction like so in your integration test:

test('', function(assert) {
  this.sendAction = () => RSVP.resolve(true);

  this.render(hbs`{{some-component sendAction=sendAction}}`);
});

This will return a promise as your code expects.

I recommend using the RSVP library in cases where you need to use promises.

Upvotes: 1

Related Questions