Skip to content

Instantly share code, notes, and snippets.

@tatey
Last active December 25, 2015 12:09
Show Gist options
  • Save tatey/6974197 to your computer and use it in GitHub Desktop.
Save tatey/6974197 to your computer and use it in GitHub Desktop.
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;
});
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();
});
});
});
app.factory('payment', function($window) {
return $window.jQuery.payment;
});
app.controller('cardController', function($scope, card) {
$scope.card = new card();
$scope.submit = function() {
$scope.card.isValid();
};
});
<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