Last active
December 25, 2015 12:09
-
-
Save tatey/6974197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory('card', function(payment) { | |
card = function(attrs) { | |
this.errors = {}; | |
return this; | |
}; | |
card.prototype.isValid = function() { | |
if (!payment.validateCardNumber(this.number)) { | |
this.errors.number = 'invalid'; | |
} | |
}; | |
return card; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('card', function() { | |
describe('#isValid', function() { | |
var payment, card; | |
beforeEach(function() { | |
payment = {validateCardNumber: jasmine.createSpy()}; | |
module(function($provide) { | |
$provide.value('payment', payment); | |
}); | |
inject(function($injector) { | |
card = $injector.get('card'); | |
}); | |
}); | |
it('sets error when number is invalid', function() { | |
spyOn(payment, 'validateCardNumber').andReturn(false); | |
var c = new card(); | |
c.isValid(); | |
expect(c.errors.number).toEqual('invalid'); | |
}); | |
it('does nothing when number is valid', function() { | |
spyOn(payment, 'validateCardNumber').andReturn(true); | |
var c = new card(); | |
c.isValid(); | |
expect(c.errors.number).toBeUndefined(); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.factory('payment', function($window) { | |
return $window.jQuery.payment; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.controller('cardController', function($scope, card) { | |
$scope.card = new card(); | |
$scope.submit = function() { | |
$scope.card.isValid(); | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form ng-controller="cardController" ng-submit="submit()"> | |
<input type="text" ng-model="card.number"> | |
<span ng-show="card.errors.number"><%= t('card.errors.invalid') %></span> | |
<button>Submit</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment