diff --git a/test/client-old/spec/controllers/settingsCtrlSpec.js b/test/client-old/spec/controllers/settingsCtrlSpec.js index ef960ae204..6d2cc8a149 100644 --- a/test/client-old/spec/controllers/settingsCtrlSpec.js +++ b/test/client-old/spec/controllers/settingsCtrlSpec.js @@ -1,7 +1,7 @@ 'use strict'; describe('Settings Controller', function () { - var rootScope, scope, user, User, ctrl; + var rootScope, scope, $httpBackend, user, User, ctrl, Notification; const actionClickEvent = { target: document.createElement('button'), @@ -29,18 +29,27 @@ describe('Settings Controller', function () { releaseBoth: sandbox.stub(), }; + Notification = { + error: sandbox.stub(), + text: sandbox.stub() + }; + + $provide.value('Notification', Notification); $provide.value('User', User); $provide.value('Guide', sandbox.stub()); }); - inject(function(_$rootScope_, _$controller_) { + inject(function(_$rootScope_, _$controller_, _$httpBackend_) { scope = _$rootScope_.$new(); rootScope = _$rootScope_; + $httpBackend = _$httpBackend_; + + $httpBackend.whenGET(/partials/).respond(); // Load RootCtrl to ensure shared behaviors are loaded - _$controller_('RootCtrl', {$scope: scope, User: User}); + _$controller_('RootCtrl', {$scope: scope, User: User, Notification: Notification}); - ctrl = _$controller_('SettingsCtrl', {$scope: scope, User: User}); + ctrl = _$controller_('SettingsCtrl', {$scope: scope, User: User, Notification: Notification}); }); }); @@ -281,4 +290,48 @@ describe('Settings Controller', function () { }); }); }); + + context('Validating coupons', function () { + describe('#applyCoupon', function () { + it('displays an error when an invalid coupon is applied', function () { + $httpBackend + .whenPOST('/api/v3/coupons/validate/INVALID_COUPON?userV=undefined') + .respond(200, { + success: true, + data: { + valid: false + }, + notifications: [], + userV: 'undefined' + }); + + scope.applyCoupon('INVALID_COUPON'); + + $httpBackend.flush(); + + expect(Notification.error).to.be.called; + expect(Notification.error).to.be.calledWith(env.t('invalidCoupon'), true); + }); + + it('displays an confirmation when a valid coupon is applied', function () { + $httpBackend + .whenPOST('/api/v3/coupons/validate/VALID_COUPON?userV=undefined') + .respond(200, { + success: true, + data: { + valid: true + }, + notifications: [], + userV: 'undefined' + }); + + scope.applyCoupon('VALID_COUPON'); + + $httpBackend.flush(); + + expect(Notification.error).to.not.be.called; + expect(Notification.text).to.be.calledWith('Coupon applied!'); + }); + }); + }); }); diff --git a/website/client-old/js/controllers/settingsCtrl.js b/website/client-old/js/controllers/settingsCtrl.js index 5461d467b7..fae242e1b0 100644 --- a/website/client-old/js/controllers/settingsCtrl.js +++ b/website/client-old/js/controllers/settingsCtrl.js @@ -269,7 +269,11 @@ habitrpg.controller('SettingsCtrl', $scope.applyCoupon = function(coupon){ $http.post(ApiUrl.get() + '/api/v3/coupons/validate/'+coupon) - .success(function(){ + .success(function(res, code){ + if (!res.data.valid) { + Notification.error(env.t('invalidCoupon'), true); + return; + } Notification.text("Coupon applied!"); var subs = Content.subscriptionBlocks; subs["basic_6mo"].discount = true;