Skip to content

Commit

Permalink
Merge pull request #27 from ride/clean-run-loop
Browse files Browse the repository at this point in the history
[fix] ember run loop, as suggested in #17 and #18
  • Loading branch information
buritica committed Jul 19, 2015
2 parents d28a63c + c1907ef commit ad77990
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 58 deletions.
33 changes: 4 additions & 29 deletions app/services/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,21 @@ function debug() {
function createCardToken (card) {
debug('card.createToken:', card);

// manually start Ember loop
Ember.run.begin();

return new Ember.RSVP.Promise(function (resolve, reject) {
Stripe.card.createToken(card, function (status, response) {

debug('card.createToken handler - status %s, response:', status, response);

if (response.error) {
reject(response);
return Ember.run.end();
return Ember.run(null, reject, response);
}

resolve(response);
Ember.run(null, resolve, response);

Ember.run.end();
});
});
}

/**
* Alias to `card.createToken`, exposed as `createCardToken`
* @deprecated please see `card.createToken` for usage
*/
function createCardTokenDeprecated(card) {
Ember.deprecate(
'`EmberStripeService.createToken` has been deprecated in ' +
'favor of `EmberStripeService.card.createToken` to match ' +
'the Stripe API.'
);
return createCardToken(card);
}

/**
* Creates a BankAccout token using Stripe.js API, exposed as `bankAccount.createToken`
* @param {ojbect} bankAccount
Expand All @@ -74,22 +56,16 @@ function createCardTokenDeprecated(card) {
function createBankAccountToken(bankAccount) {
debug('bankAccount.createToken:', bankAccount);

// manually start Ember loop
Ember.run.begin();

return new Ember.RSVP.Promise(function (resolve, reject) {
Stripe.bankAccount.createToken(bankAccount, function (status, response) {

debug('bankAccount.createToken handler - status %s, response:', status, response);

if (response.error) {
reject(response);
return Ember.run.end();
return Ember.run(null, reject, response);
}

resolve(response);

Ember.run.end();
Ember.run(null, resolve, response);
});
});
}
Expand All @@ -98,7 +74,6 @@ function createBankAccountToken(bankAccount) {
* Expose module
*/
export default Ember.Service.extend({
createToken: createCardTokenDeprecated,
card: {
createToken: createCardToken,
},
Expand Down
17 changes: 6 additions & 11 deletions tests/integration/stripe-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global Stripe */

import Ember from 'ember';
import { moduleFor, test } from 'ember-qunit';
import env from 'dummy/config/environment';

Expand Down Expand Up @@ -28,22 +27,18 @@ Stripe.setPublishableKey(env.stripe.publishableKey);
test('card.createToken sets the token and returns a promise', function(assert) {
var service = this.subject();

Ember.run(function(){
service.card.createToken(cc)
.then(function(res) {
assert.ok(res.id, 'correct token set');
});
return service.card.createToken(cc)
.then(function(res) {
assert.ok(res.id, 'correct token set');
});
});

test('bankAccount.createToken sets the token and returns a promise', function(assert) {
var service = this.subject();

Ember.run(function(){
service.bankAccount.createToken(bankAccount)
.then(function(res) {
assert.ok(res.id, 'correct token set');
});
return service.bankAccount.createToken(bankAccount)
.then(function(res) {
assert.ok(res.id, 'correct token set');
});
});

18 changes: 0 additions & 18 deletions tests/unit/services/stripe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,6 @@ test('card.createToken rejects the promise if Stripe errors', function(assert) {
});
});

test('createToken syntax is still supported', function(assert) {
var service = this.subject();
var response = {
id: 'the_token'
};

var createToken = sinon.stub(Stripe.card, 'createToken', function(card, cb) {
assert.equal(card, cc, 'called with sample creditcard');
cb(200, response);
});

return service.createToken(cc)
.then(function(res) {
assert.equal(res.id, 'the_token');
createToken.restore();
});
});

// Bank accounts

var ba = {
Expand Down

0 comments on commit ad77990

Please sign in to comment.