discourse/test/javascripts/models/badge-test.js.es6

82 lines
3.4 KiB
Plaintext
Raw Normal View History

2015-08-19 09:24:09 +08:00
import Badge from 'discourse/models/badge';
module("model:badge");
2014-03-05 20:52:20 +08:00
test('newBadge', function() {
2015-08-19 09:24:09 +08:00
const badge1 = Badge.create({name: "New Badge"}),
badge2 = Badge.create({id: 1, name: "Old Badge"});
2014-03-05 20:52:20 +08:00
ok(badge1.get('newBadge'), "badges without ids are new");
ok(!badge2.get('newBadge'), "badges with ids are not new");
});
test('displayName', function() {
2015-08-19 09:24:09 +08:00
const badge1 = Badge.create({id: 1, name: "Test Badge 1"});
2014-03-05 20:52:20 +08:00
equal(badge1.get('displayName'), "Test Badge 1", "falls back to the original name in the absence of a translation");
2014-07-31 06:56:01 +08:00
sandbox.stub(I18n, "t").returnsArg(0);
2015-08-19 09:24:09 +08:00
const badge2 = Badge.create({id: 2, name: "Test Badge 2"});
equal(badge2.get('displayName'), "badges.badge.test_badge_2.name", "uses translation when available");
2014-03-05 20:52:20 +08:00
});
test('translatedDescription', function() {
2015-08-19 09:24:09 +08:00
const badge1 = Badge.create({id: 1, name: "Test Badge 1", description: "TEST"});
2014-05-21 15:22:42 +08:00
equal(badge1.get('translatedDescription'), null, "returns null when no translation exists");
2014-03-05 20:52:20 +08:00
2015-08-19 09:24:09 +08:00
const badge2 = Badge.create({id: 2, name: "Test Badge 2 **"});
2014-07-31 06:56:01 +08:00
sandbox.stub(I18n, "t").returns("description translation");
2014-03-05 20:52:20 +08:00
equal(badge2.get('translatedDescription'), "description translation", "users translated description");
});
2014-05-21 15:22:42 +08:00
test('displayDescription', function() {
2015-08-19 09:24:09 +08:00
const badge1 = Badge.create({id: 1, name: "Test Badge 1", description: "TEST"});
2014-05-21 15:22:42 +08:00
equal(badge1.get('displayDescription'), "TEST", "returns original description when no translation exists");
2015-08-19 09:24:09 +08:00
const badge2 = Badge.create({id: 2, name: "Test Badge 2 **"});
2014-07-31 06:56:01 +08:00
sandbox.stub(I18n, "t").returns("description translation");
2014-05-21 15:22:42 +08:00
equal(badge2.get('displayDescription'), "description translation", "users translated description");
});
2014-03-05 20:52:20 +08:00
test('createFromJson array', function() {
2015-08-19 09:24:09 +08:00
const badgesJson = {"badge_types":[{"id":6,"name":"Silver 1"}],"badges":[{"id":1126,"name":"Badge 1","description":null,"badge_type_id":6}]};
2014-03-05 20:52:20 +08:00
2015-08-19 09:24:09 +08:00
const badges = Badge.createFromJson(badgesJson);
2014-03-05 20:52:20 +08:00
ok(Array.isArray(badges), "returns an array");
equal(badges[0].get('name'), "Badge 1", "badge details are set");
equal(badges[0].get('badge_type.name'), "Silver 1", "badge_type reference is set");
});
test('createFromJson single', function() {
2015-08-19 09:24:09 +08:00
const badgeJson = {"badge_types":[{"id":6,"name":"Silver 1"}],"badge":{"id":1126,"name":"Badge 1","description":null,"badge_type_id":6}};
2014-03-05 20:52:20 +08:00
2015-08-19 09:24:09 +08:00
const badge = Badge.createFromJson(badgeJson);
2014-03-05 20:52:20 +08:00
ok(!Array.isArray(badge), "does not returns an array");
});
test('updateFromJson', function() {
2015-08-19 09:24:09 +08:00
const badgeJson = {"badge_types":[{"id":6,"name":"Silver 1"}],"badge":{"id":1126,"name":"Badge 1","description":null,"badge_type_id":6}};
const badge = Badge.create({name: "Badge 1"});
2014-03-05 20:52:20 +08:00
badge.updateFromJson(badgeJson);
equal(badge.get('id'), 1126, "id is set");
equal(badge.get('badge_type.name'), "Silver 1", "badge_type reference is set");
});
test('save', function() {
2014-07-31 06:56:01 +08:00
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve({}));
2015-08-19 09:24:09 +08:00
const badge = Badge.create({name: "New Badge", description: "This is a new badge.", badge_type_id: 1});
// TODO: clean API
badge.save(["name", "description", "badge_type_id"]);
2014-03-05 20:52:20 +08:00
ok(Discourse.ajax.calledOnce, "saved badge");
});
test('destroy', function() {
2014-07-31 06:56:01 +08:00
sandbox.stub(Discourse, 'ajax');
2015-08-19 09:24:09 +08:00
const badge = Badge.create({name: "New Badge", description: "This is a new badge.", badge_type_id: 1});
2014-03-05 20:52:20 +08:00
badge.destroy();
ok(!Discourse.ajax.calledOnce, "no AJAX call for a new badge");
badge.set('id', 3);
badge.destroy();
ok(Discourse.ajax.calledOnce, "AJAX call was made");
});