diff --git a/app/assets/javascripts/discourse/models/badge.js b/app/assets/javascripts/discourse/models/badge.js index 4bc61a95eca..c802f356d23 100644 --- a/app/assets/javascripts/discourse/models/badge.js +++ b/app/assets/javascripts/discourse/models/badge.js @@ -62,14 +62,18 @@ Discourse.Badge = Discourse.Model.extend({ **/ updateFromJson: function(json) { var self = this; - Object.keys(json.badge).forEach(function(key) { - self.set(key, json.badge[key]); - }); - json.badge_types.forEach(function(badgeType) { - if (badgeType.id === self.get('badge_type_id')) { - self.set('badge_type', Object.create(badgeType)); - } - }); + if (json.badge) { + Object.keys(json.badge).forEach(function(key) { + self.set(key, json.badge[key]); + }); + } + if (json.badge_types) { + json.badge_types.forEach(function(badgeType) { + if (badgeType.id === self.get('badge_type_id')) { + self.set('badge_type', Object.create(badgeType)); + } + }); + } }, /** diff --git a/test/javascripts/models/user_badge_test.js b/test/javascripts/models/user_badge_test.js index 589d34752ed..c4e896d8c00 100644 --- a/test/javascripts/models/user_badge_test.js +++ b/test/javascripts/models/user_badge_test.js @@ -17,26 +17,32 @@ test('createFromJson array', function() { equal(userBadges[0].get('granted_by'), null, "granted_by reference is not set when null"); }); -test('findByUsername', function() { +asyncTestDiscourse('findByUsername', function() { + expect(2); this.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson)); Discourse.UserBadge.findByUsername("anne3").then(function(badges) { ok(Array.isArray(badges), "returns an array"); + start(); }); ok(Discourse.ajax.calledOnce, "makes an AJAX call"); }); -test('findByBadgeId', function() { +asyncTestDiscourse('findByBadgeId', function() { + expect(2); this.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson)); Discourse.UserBadge.findByBadgeId(880).then(function(badges) { ok(Array.isArray(badges), "returns an array"); + start(); }); ok(Discourse.ajax.calledOnce, "makes an AJAX call"); }); -test('grant', function() { +asyncTestDiscourse('grant', function() { + expect(2); this.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(singleBadgeJson)); Discourse.UserBadge.grant(1, "username").then(function(userBadge) { ok(!Array.isArray(userBadge), "does not return an array"); + start(); }); ok(Discourse.ajax.calledOnce, "makes an AJAX call"); });