Skip to content

Instantly share code, notes, and snippets.

@mitchlloyd
Last active January 13, 2017 21:34
Show Gist options
  • Save mitchlloyd/15980bc78fbccae76f421f6e9c6e9c78 to your computer and use it in GitHub Desktop.
Save mitchlloyd/15980bc78fbccae76f421f6e9c6e9c78 to your computer and use it in GitHub Desktop.
Wrap Third Party APIs - Fake
import Ember from 'ember';
const { Service, RSVP } = Ember;
export default Service.extend({
init() {
this._super(...arguments);
// We record both the charges sent and notifications sent to verify in
// our tests.
this.chargesSent = [];
this.notificationsSent = [];
},
charge(payment) {
this.chargesSent.push(payment);
let notificationsSent = this.notificationsSent;
return new RSVP.Promise((resolve, reject) => {
resolve({
result: {
// Here we use stubbed data, but you may consider passing this information
// into the fake to handle more test cases.
token: { id: 'fake-token-id' },
shippingContact 'Fake Shipping Contact'
},
notify: {
success() {
notificationsSent.push('success')
},
failure() {
notificationsSent.push('failure')
}
}
})
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment