2015-08-18 21:24:09 -04:00
|
|
|
import UserBadge from 'discourse/models/user-badge';
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2015-08-18 21:24:09 -04:00
|
|
|
module("model:user-badge");
|
|
|
|
|
|
|
|
const singleBadgeJson = {"badges":[{"id":874,"name":"Badge 2","description":null,"badge_type_id":7}],"badge_types":[{"id":7,"name":"Silver 2"}],"users":[{"id":13470,"username":"anne3","avatar_template":"//www.gravatar.com/avatar/a4151b1fd72089c54e2374565a87da7f.png?s={size}\u0026r=pg\u0026d=identicon"}],"user_badge":{"id":665,"granted_at":"2014-03-09T20:30:01.190-04:00","badge_id":874,"granted_by_id":13470}},
|
2014-04-16 20:06:29 -04:00
|
|
|
multipleBadgesJson = {"badges":[{"id":880,"name":"Badge 8","description":null,"badge_type_id":13}],"badge_types":[{"id":13,"name":"Silver 8"}],"users":[],"user_badges":[{"id":668,"granted_at":"2014-03-09T20:30:01.420-04:00","badge_id":880,"granted_by_id":null}]};
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-03-19 10:27:21 -04:00
|
|
|
test('createFromJson single', function() {
|
2015-08-18 21:24:09 -04:00
|
|
|
const userBadge = UserBadge.createFromJson(singleBadgeJson);
|
2014-03-05 07:52:20 -05:00
|
|
|
ok(!Array.isArray(userBadge), "does not return an array");
|
|
|
|
equal(userBadge.get('badge.name'), "Badge 2", "badge reference is set");
|
|
|
|
equal(userBadge.get('badge.badge_type.name'), "Silver 2", "badge.badge_type reference is set");
|
|
|
|
equal(userBadge.get('granted_by.username'), "anne3", "granted_by reference is set");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('createFromJson array', function() {
|
2015-08-18 21:24:09 -04:00
|
|
|
const userBadges = UserBadge.createFromJson(multipleBadgesJson);
|
2014-03-05 07:52:20 -05:00
|
|
|
ok(Array.isArray(userBadges), "returns an array");
|
|
|
|
equal(userBadges[0].get('granted_by'), null, "granted_by reference is not set when null");
|
|
|
|
});
|
2014-03-19 10:27:21 -04:00
|
|
|
|
2014-04-15 14:19:05 -04:00
|
|
|
asyncTestDiscourse('findByUsername', function() {
|
|
|
|
expect(2);
|
2014-07-30 18:56:01 -04:00
|
|
|
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson));
|
2015-08-18 21:24:09 -04:00
|
|
|
UserBadge.findByUsername("anne3").then(function(badges) {
|
2014-03-19 10:27:21 -04:00
|
|
|
ok(Array.isArray(badges), "returns an array");
|
2014-04-15 14:19:05 -04:00
|
|
|
start();
|
2014-03-19 10:27:21 -04:00
|
|
|
});
|
|
|
|
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
|
|
|
|
});
|
|
|
|
|
2014-04-15 14:19:05 -04:00
|
|
|
asyncTestDiscourse('findByBadgeId', function() {
|
|
|
|
expect(2);
|
2014-07-30 18:56:01 -04:00
|
|
|
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson));
|
2015-08-18 21:24:09 -04:00
|
|
|
UserBadge.findByBadgeId(880).then(function(badges) {
|
2014-04-16 10:56:11 -04:00
|
|
|
ok(Array.isArray(badges), "returns an array");
|
2014-04-15 14:19:05 -04:00
|
|
|
start();
|
2014-04-16 10:56:11 -04:00
|
|
|
});
|
|
|
|
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
|
|
|
|
});
|
|
|
|
|
2014-04-15 14:19:05 -04:00
|
|
|
asyncTestDiscourse('grant', function() {
|
|
|
|
expect(2);
|
2014-07-30 18:56:01 -04:00
|
|
|
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(singleBadgeJson));
|
2015-08-18 21:24:09 -04:00
|
|
|
UserBadge.grant(1, "username").then(function(userBadge) {
|
2014-03-19 10:27:21 -04:00
|
|
|
ok(!Array.isArray(userBadge), "does not return an array");
|
2014-04-15 14:19:05 -04:00
|
|
|
start();
|
2014-03-19 10:27:21 -04:00
|
|
|
});
|
|
|
|
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('revoke', function() {
|
2014-07-30 18:56:01 -04:00
|
|
|
sandbox.stub(Discourse, 'ajax');
|
2015-08-18 21:24:09 -04:00
|
|
|
const userBadge = UserBadge.create({id: 1});
|
2014-03-19 10:27:21 -04:00
|
|
|
userBadge.revoke();
|
|
|
|
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
|
|
|
|
});
|