2013-10-09 12:24:33 -04:00
|
|
|
module("Discourse.Computed", {
|
|
|
|
setup: function() {
|
|
|
|
sinon.stub(I18n, "t", function(scope) {
|
|
|
|
return "%@ translated: " + scope;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
teardown: function() {
|
|
|
|
I18n.t.restore();
|
|
|
|
}
|
|
|
|
});
|
2013-07-11 19:35:52 -04:00
|
|
|
|
2014-04-24 17:39:34 -04:00
|
|
|
test("setting", function() {
|
|
|
|
var t = Em.Object.extend({
|
|
|
|
vehicle: Discourse.computed.setting('vehicle'),
|
|
|
|
missingProp: Discourse.computed.setting('madeUpThing')
|
|
|
|
}).create();
|
|
|
|
|
|
|
|
Discourse.SiteSettings.vehicle = "airplane";
|
|
|
|
equal(t.get('vehicle'), "airplane", "it has the value of the site setting");
|
|
|
|
ok(!t.get('missingProp'), "it is falsy when the site setting is not defined");
|
|
|
|
});
|
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
test("propertyEqual", function() {
|
2013-10-17 12:52:24 -04:00
|
|
|
var t = Em.Object.extend({
|
|
|
|
same: Discourse.computed.propertyEqual('cookies', 'biscuits')
|
|
|
|
}).create({
|
2013-07-11 19:35:52 -04:00
|
|
|
cookies: 10,
|
|
|
|
biscuits: 10
|
|
|
|
});
|
|
|
|
|
|
|
|
ok(t.get('same'), "it is true when the properties are the same");
|
|
|
|
t.set('biscuits', 9);
|
|
|
|
ok(!t.get('same'), "it isn't true when one property is different");
|
|
|
|
});
|
|
|
|
|
2013-07-12 16:18:32 -04:00
|
|
|
test("propertyNotEqual", function() {
|
2013-10-17 12:52:24 -04:00
|
|
|
var t = Em.Object.extend({
|
|
|
|
diff: Discourse.computed.propertyNotEqual('cookies', 'biscuits')
|
|
|
|
}).create({
|
2013-07-12 16:18:32 -04:00
|
|
|
cookies: 10,
|
|
|
|
biscuits: 10
|
|
|
|
});
|
|
|
|
|
|
|
|
ok(!t.get('diff'), "it isn't true when the properties are the same");
|
|
|
|
t.set('biscuits', 9);
|
|
|
|
ok(t.get('diff'), "it is true when one property is different");
|
|
|
|
});
|
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
|
|
|
|
test("fmt", function() {
|
2013-10-17 12:52:24 -04:00
|
|
|
var t = Em.Object.extend({
|
|
|
|
exclaimyUsername: Discourse.computed.fmt('username', "!!! %@ !!!"),
|
|
|
|
multiple: Discourse.computed.fmt('username', 'mood', "%@ is %@")
|
|
|
|
}).create({
|
2013-07-11 19:35:52 -04:00
|
|
|
username: 'eviltrout',
|
|
|
|
mood: "happy"
|
|
|
|
});
|
|
|
|
|
|
|
|
equal(t.get('exclaimyUsername'), '!!! eviltrout !!!', "it inserts the string");
|
2013-10-09 12:24:33 -04:00
|
|
|
equal(t.get('multiple'), "eviltrout is happy", "it inserts multiple strings");
|
2013-07-11 19:35:52 -04:00
|
|
|
|
|
|
|
t.set('username', 'codinghorror');
|
2013-10-09 12:24:33 -04:00
|
|
|
equal(t.get('multiple'), "codinghorror is happy", "it supports changing properties");
|
2013-07-11 19:35:52 -04:00
|
|
|
t.set('mood', 'ecstatic');
|
2013-10-09 12:24:33 -04:00
|
|
|
equal(t.get('multiple'), "codinghorror is ecstatic", "it supports changing another property");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("i18n", function() {
|
2013-10-17 12:52:24 -04:00
|
|
|
var t = Em.Object.extend({
|
|
|
|
exclaimyUsername: Discourse.computed.i18n('username', "!!! %@ !!!"),
|
|
|
|
multiple: Discourse.computed.i18n('username', 'mood', "%@ is %@")
|
|
|
|
}).create({
|
2013-10-09 12:24:33 -04:00
|
|
|
username: 'eviltrout',
|
|
|
|
mood: "happy"
|
|
|
|
});
|
|
|
|
|
2013-10-17 12:52:24 -04:00
|
|
|
equal(t.get('exclaimyUsername'), '%@ translated: !!! eviltrout !!!', "it inserts the string and then translates");
|
|
|
|
equal(t.get('multiple'), "%@ translated: eviltrout is happy", "it inserts multiple strings and then translates");
|
2013-10-09 12:24:33 -04:00
|
|
|
|
|
|
|
t.set('username', 'codinghorror');
|
2013-10-17 12:52:24 -04:00
|
|
|
equal(t.get('multiple'), "%@ translated: codinghorror is happy", "it supports changing properties");
|
2013-10-09 12:24:33 -04:00
|
|
|
t.set('mood', 'ecstatic');
|
2013-10-17 12:52:24 -04:00
|
|
|
equal(t.get('multiple'), "%@ translated: codinghorror is ecstatic", "it supports changing another property");
|
2013-07-11 19:35:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-10-17 12:52:24 -04:00
|
|
|
test("url", function() {
|
|
|
|
var t, testClass;
|
|
|
|
|
|
|
|
testClass = Em.Object.extend({
|
|
|
|
userUrl: Discourse.computed.url('username', "/users/%@")
|
|
|
|
});
|
|
|
|
|
|
|
|
t = testClass.create({ username: 'eviltrout' });
|
|
|
|
equal(t.get('userUrl'), "/users/eviltrout", "it supports urls without a prefix");
|
2013-07-11 19:35:52 -04:00
|
|
|
|
|
|
|
Discourse.BaseUri = "/prefixed/";
|
2013-10-17 12:52:24 -04:00
|
|
|
t = testClass.create({ username: 'eviltrout' });
|
|
|
|
equal(t.get('userUrl'), "/prefixed/users/eviltrout", "it supports urls with a prefix");
|
2013-10-09 12:24:33 -04:00
|
|
|
});
|