mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
* (fix) If coupon is not valid, display an error * (test) Add valid and invalid coupon tests for applyCoupon * Add test descriptions
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
describe('Settings Controller', function () {
|
describe('Settings Controller', function () {
|
||||||
var rootScope, scope, user, User, ctrl;
|
var rootScope, scope, $httpBackend, user, User, ctrl, Notification;
|
||||||
|
|
||||||
const actionClickEvent = {
|
const actionClickEvent = {
|
||||||
target: document.createElement('button'),
|
target: document.createElement('button'),
|
||||||
@@ -29,18 +29,27 @@ describe('Settings Controller', function () {
|
|||||||
releaseBoth: sandbox.stub(),
|
releaseBoth: sandbox.stub(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Notification = {
|
||||||
|
error: sandbox.stub(),
|
||||||
|
text: sandbox.stub()
|
||||||
|
};
|
||||||
|
|
||||||
|
$provide.value('Notification', Notification);
|
||||||
$provide.value('User', User);
|
$provide.value('User', User);
|
||||||
$provide.value('Guide', sandbox.stub());
|
$provide.value('Guide', sandbox.stub());
|
||||||
});
|
});
|
||||||
|
|
||||||
inject(function(_$rootScope_, _$controller_) {
|
inject(function(_$rootScope_, _$controller_, _$httpBackend_) {
|
||||||
scope = _$rootScope_.$new();
|
scope = _$rootScope_.$new();
|
||||||
rootScope = _$rootScope_;
|
rootScope = _$rootScope_;
|
||||||
|
$httpBackend = _$httpBackend_;
|
||||||
|
|
||||||
|
$httpBackend.whenGET(/partials/).respond();
|
||||||
|
|
||||||
// Load RootCtrl to ensure shared behaviors are loaded
|
// 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!');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -269,7 +269,11 @@ habitrpg.controller('SettingsCtrl',
|
|||||||
|
|
||||||
$scope.applyCoupon = function(coupon){
|
$scope.applyCoupon = function(coupon){
|
||||||
$http.post(ApiUrl.get() + '/api/v3/coupons/validate/'+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!");
|
Notification.text("Coupon applied!");
|
||||||
var subs = Content.subscriptionBlocks;
|
var subs = Content.subscriptionBlocks;
|
||||||
subs["basic_6mo"].discount = true;
|
subs["basic_6mo"].discount = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user