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

76 lines
2.1 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import Badge from "discourse/models/badge";
2015-08-19 09:24:09 +08:00
2017-06-15 01:57:58 +08:00
QUnit.module("model:badge");
2014-03-05 20:52:20 +08:00
2018-06-15 23:03:24 +08:00
QUnit.test("newBadge", assert => {
const badge1 = Badge.create({ name: "New Badge" }),
badge2 = Badge.create({ id: 1, name: "Old Badge" });
assert.ok(badge1.get("newBadge"), "badges without ids are new");
assert.ok(!badge2.get("newBadge"), "badges with ids are not new");
2014-03-05 20:52:20 +08:00
});
2018-06-15 23:03:24 +08:00
QUnit.test("createFromJson array", assert => {
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
2017-06-15 01:57:58 +08:00
assert.ok(Array.isArray(badges), "returns an array");
2018-06-15 23:03:24 +08:00
assert.equal(badges[0].get("name"), "Badge 1", "badge details are set");
assert.equal(
badges[0].get("badge_type.name"),
"Silver 1",
"badge_type reference is set"
);
2014-03-05 20:52:20 +08:00
});
2018-06-15 23:03:24 +08:00
QUnit.test("createFromJson single", assert => {
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
2017-06-15 01:57:58 +08:00
assert.ok(!Array.isArray(badge), "does not returns an array");
2014-03-05 20:52:20 +08:00
});
2018-06-15 23:03:24 +08:00
QUnit.test("updateFromJson", assert => {
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);
2018-06-15 23:03:24 +08:00
assert.equal(badge.get("id"), 1126, "id is set");
assert.equal(
badge.get("badge_type.name"),
"Silver 1",
"badge_type reference is set"
);
2014-03-05 20:52:20 +08:00
});
2018-06-15 23:03:24 +08:00
QUnit.test("save", assert => {
2017-06-15 01:57:58 +08:00
assert.expect(0);
2018-06-15 23:03:24 +08:00
const badge = Badge.create({
name: "New Badge",
description: "This is a new badge.",
badge_type_id: 1
});
2016-07-01 01:55:44 +08:00
return badge.save(["name", "description", "badge_type_id"]);
2014-03-05 20:52:20 +08:00
});
2018-06-15 23:03:24 +08:00
QUnit.test("destroy", assert => {
2017-06-15 01:57:58 +08:00
assert.expect(0);
2018-06-15 23:03:24 +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();
2018-06-15 23:03:24 +08:00
badge.set("id", 3);
2016-07-01 01:55:44 +08:00
return badge.destroy();
2018-06-15 23:03:24 +08:00
});