2020-05-14 22:04:08 +08:00
|
|
|
import I18n from "I18n";
|
2019-10-30 03:23:50 +08:00
|
|
|
import EmberObject from "@ember/object";
|
2015-08-08 03:08:27 +08:00
|
|
|
import {
|
|
|
|
setting,
|
|
|
|
propertyEqual,
|
|
|
|
propertyNotEqual,
|
|
|
|
fmt,
|
|
|
|
i18n,
|
2020-03-11 16:23:10 +08:00
|
|
|
url,
|
|
|
|
htmlSafe
|
2015-08-08 03:08:27 +08:00
|
|
|
} from "discourse/lib/computed";
|
2020-06-04 00:45:26 +08:00
|
|
|
import { setPrefix } from "discourse-common/lib/get-url";
|
2020-07-10 03:54:53 +08:00
|
|
|
import { discourseModule } from "helpers/qunit-helpers";
|
2015-08-08 03:08:27 +08:00
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
discourseModule("lib:computed", {
|
2017-06-15 01:57:58 +08:00
|
|
|
beforeEach() {
|
2018-10-04 23:06:14 +08:00
|
|
|
sandbox.stub(I18n, "t").callsFake(function(scope) {
|
2013-10-10 00:24:33 +08:00
|
|
|
return "%@ translated: " + scope;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
afterEach() {
|
2013-10-10 00:24:33 +08:00
|
|
|
I18n.t.restore();
|
|
|
|
}
|
|
|
|
});
|
2013-07-12 07:35:52 +08:00
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
QUnit.test("setting", function(assert) {
|
2019-10-30 03:23:50 +08:00
|
|
|
var t = EmberObject.extend({
|
2015-08-08 03:08:27 +08:00
|
|
|
vehicle: setting("vehicle"),
|
|
|
|
missingProp: setting("madeUpThing")
|
2014-04-25 05:39:34 +08:00
|
|
|
}).create();
|
|
|
|
|
2020-07-10 03:54:53 +08:00
|
|
|
this.siteSettings.vehicle = "airplane";
|
2017-06-15 01:57:58 +08: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 23:03:24 +08:00
|
|
|
);
|
2014-04-25 05:39:34 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("propertyEqual", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var t = EmberObject.extend({
|
2015-08-08 03:08:27 +08:00
|
|
|
same: propertyEqual("cookies", "biscuits")
|
2013-10-18 00:52:24 +08:00
|
|
|
}).create({
|
2013-07-12 07:35:52 +08:00
|
|
|
cookies: 10,
|
|
|
|
biscuits: 10
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(t.get("same"), "it is true when the properties are the same");
|
2013-07-12 07:35:52 +08:00
|
|
|
t.set("biscuits", 9);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(!t.get("same"), "it isn't true when one property is different");
|
2013-07-12 07:35:52 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("propertyNotEqual", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var t = EmberObject.extend({
|
2015-08-08 03:08:27 +08:00
|
|
|
diff: propertyNotEqual("cookies", "biscuits")
|
2013-10-18 00:52:24 +08:00
|
|
|
}).create({
|
2013-07-13 04:18:32 +08:00
|
|
|
cookies: 10,
|
|
|
|
biscuits: 10
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(!t.get("diff"), "it isn't true when the properties are the same");
|
2013-07-13 04:18:32 +08:00
|
|
|
t.set("biscuits", 9);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(t.get("diff"), "it is true when one property is different");
|
2013-07-13 04:18:32 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("fmt", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var t = EmberObject.extend({
|
2015-08-08 03:08:27 +08:00
|
|
|
exclaimyUsername: fmt("username", "!!! %@ !!!"),
|
|
|
|
multiple: fmt("username", "mood", "%@ is %@")
|
2013-10-18 00:52:24 +08:00
|
|
|
}).create({
|
2013-07-12 07:35:52 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
mood: "happy"
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08: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 23:03:24 +08:00
|
|
|
);
|
|
|
|
|
2013-07-12 07:35:52 +08:00
|
|
|
t.set("username", "codinghorror");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("multiple"),
|
|
|
|
"codinghorror is happy",
|
|
|
|
"it supports changing properties"
|
|
|
|
);
|
2013-07-12 07:35:52 +08:00
|
|
|
t.set("mood", "ecstatic");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("multiple"),
|
|
|
|
"codinghorror is ecstatic",
|
|
|
|
"it supports changing another property"
|
|
|
|
);
|
2013-10-10 00:24:33 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("i18n", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var t = EmberObject.extend({
|
2015-08-08 03:08:27 +08:00
|
|
|
exclaimyUsername: i18n("username", "!!! %@ !!!"),
|
|
|
|
multiple: i18n("username", "mood", "%@ is %@")
|
2013-10-18 00:52:24 +08:00
|
|
|
}).create({
|
2013-10-10 00:24:33 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
mood: "happy"
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08: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 23:03:24 +08:00
|
|
|
);
|
|
|
|
|
2013-10-10 00:24:33 +08:00
|
|
|
t.set("username", "codinghorror");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("multiple"),
|
|
|
|
"%@ translated: codinghorror is happy",
|
|
|
|
"it supports changing properties"
|
|
|
|
);
|
2013-10-10 00:24:33 +08:00
|
|
|
t.set("mood", "ecstatic");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("multiple"),
|
|
|
|
"%@ translated: codinghorror is ecstatic",
|
|
|
|
"it supports changing another property"
|
|
|
|
);
|
2013-07-12 07:35:52 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("url", assert => {
|
2013-10-18 00:52:24 +08:00
|
|
|
var t, testClass;
|
2017-08-21 20:56:22 +08:00
|
|
|
|
2019-10-30 03:23:50 +08:00
|
|
|
testClass = EmberObject.extend({
|
2017-03-29 02:27:54 +08:00
|
|
|
userUrl: url("username", "/u/%@")
|
2013-10-18 00:52:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
t = testClass.create({ username: "eviltrout" });
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("userUrl"),
|
|
|
|
"/u/eviltrout",
|
|
|
|
"it supports urls without a prefix"
|
|
|
|
);
|
2013-07-12 07:35:52 +08:00
|
|
|
|
2020-06-04 00:45:26 +08:00
|
|
|
setPrefix("/prefixed");
|
2013-10-18 00:52:24 +08:00
|
|
|
t = testClass.create({ username: "eviltrout" });
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
t.get("userUrl"),
|
|
|
|
"/prefixed/u/eviltrout",
|
|
|
|
"it supports urls with a prefix"
|
|
|
|
);
|
2013-10-10 00:24:33 +08:00
|
|
|
});
|
2020-03-11 16:23:10 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|