discourse/test/javascripts/lib/computed-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

170 lines
3.8 KiB
JavaScript
Raw Normal View History

import I18n from "I18n";
import EmberObject from "@ember/object";
2015-08-07 15:08:27 -04:00
import {
setting,
propertyEqual,
propertyNotEqual,
fmt,
i18n,
url,
htmlSafe
2015-08-07 15:08:27 -04:00
} from "discourse/lib/computed";
import { setPrefix } from "discourse-common/lib/get-url";
import { discourseModule } from "helpers/qunit-helpers";
2015-08-07 15:08:27 -04:00
discourseModule("lib:computed", {
2017-06-14 13:57:58 -04:00
beforeEach() {
sandbox.stub(I18n, "t").callsFake(function(scope) {
return "%@ translated: " + scope;
});
},
2017-06-14 13:57:58 -04:00
afterEach() {
I18n.t.restore();
}
});
QUnit.test("setting", function(assert) {
var t = EmberObject.extend({
2015-08-07 15:08:27 -04:00
vehicle: setting("vehicle"),
missingProp: setting("madeUpThing")
}).create();
this.siteSettings.vehicle = "airplane";
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("vehicle"),
"airplane",
"it has the value of the site setting"
);
assert.ok(
!t.get("missingProp"),
"it is falsy when the site setting is not defined"
2018-06-15 11:03:24 -04:00
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("propertyEqual", assert => {
var t = EmberObject.extend({
2015-08-07 15:08:27 -04:00
same: propertyEqual("cookies", "biscuits")
2013-10-17 12:52:24 -04:00
}).create({
cookies: 10,
biscuits: 10
});
2017-06-14 13:57:58 -04:00
assert.ok(t.get("same"), "it is true when the properties are the same");
t.set("biscuits", 9);
2017-06-14 13:57:58 -04:00
assert.ok(!t.get("same"), "it isn't true when one property is different");
});
2017-06-14 13:57:58 -04:00
QUnit.test("propertyNotEqual", assert => {
var t = EmberObject.extend({
2015-08-07 15:08:27 -04:00
diff: propertyNotEqual("cookies", "biscuits")
2013-10-17 12:52:24 -04:00
}).create({
cookies: 10,
biscuits: 10
});
2017-06-14 13:57:58 -04:00
assert.ok(!t.get("diff"), "it isn't true when the properties are the same");
t.set("biscuits", 9);
2017-06-14 13:57:58 -04:00
assert.ok(t.get("diff"), "it is true when one property is different");
});
2017-06-14 13:57:58 -04:00
QUnit.test("fmt", assert => {
var t = EmberObject.extend({
2015-08-07 15:08:27 -04:00
exclaimyUsername: fmt("username", "!!! %@ !!!"),
multiple: fmt("username", "mood", "%@ is %@")
2013-10-17 12:52:24 -04:00
}).create({
username: "eviltrout",
mood: "happy"
});
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("exclaimyUsername"),
"!!! eviltrout !!!",
"it inserts the string"
);
assert.equal(
t.get("multiple"),
"eviltrout is happy",
"it inserts multiple strings"
2018-06-15 11:03:24 -04:00
);
t.set("username", "codinghorror");
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("multiple"),
"codinghorror is happy",
"it supports changing properties"
);
t.set("mood", "ecstatic");
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("multiple"),
"codinghorror is ecstatic",
"it supports changing another property"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("i18n", assert => {
var t = EmberObject.extend({
2015-08-07 15:08:27 -04:00
exclaimyUsername: i18n("username", "!!! %@ !!!"),
multiple: i18n("username", "mood", "%@ is %@")
2013-10-17 12:52:24 -04:00
}).create({
username: "eviltrout",
mood: "happy"
});
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("exclaimyUsername"),
"%@ translated: !!! eviltrout !!!",
"it inserts the string and then translates"
);
assert.equal(
t.get("multiple"),
"%@ translated: eviltrout is happy",
"it inserts multiple strings and then translates"
2018-06-15 11:03:24 -04:00
);
t.set("username", "codinghorror");
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("multiple"),
"%@ translated: codinghorror is happy",
"it supports changing properties"
);
t.set("mood", "ecstatic");
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("multiple"),
"%@ translated: codinghorror is ecstatic",
"it supports changing another property"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("url", assert => {
2013-10-17 12:52:24 -04:00
var t, testClass;
2017-08-21 08:56:22 -04:00
testClass = EmberObject.extend({
2017-03-28 14:27:54 -04:00
userUrl: url("username", "/u/%@")
2013-10-17 12:52:24 -04:00
});
t = testClass.create({ username: "eviltrout" });
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("userUrl"),
"/u/eviltrout",
"it supports urls without a prefix"
);
setPrefix("/prefixed");
2013-10-17 12:52:24 -04:00
t = testClass.create({ username: "eviltrout" });
2017-06-14 13:57:58 -04:00
assert.equal(
t.get("userUrl"),
"/prefixed/u/eviltrout",
"it supports urls with a prefix"
);
});
QUnit.test("htmlSafe", assert => {
const cookies = "<p>cookies and <b>biscuits</b></p>";
const t = EmberObject.extend({
desc: htmlSafe("cookies")
}).create({ cookies });
assert.equal(t.get("desc").string, cookies);
});