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