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

105 lines
3.1 KiB
Plaintext
Raw Normal View History

2015-08-07 15:08:27 -04:00
import { setting, propertyEqual, propertyNotEqual, fmt, i18n, url } from 'discourse/lib/computed';
module("lib:computed", {
setup: function() {
2014-07-30 18:56:01 -04:00
sandbox.stub(I18n, "t", function(scope) {
return "%@ translated: " + scope;
});
},
teardown: function() {
I18n.t.restore();
}
});
test("setting", function() {
var t = Em.Object.extend({
2015-08-07 15:08:27 -04:00
vehicle: setting('vehicle'),
missingProp: 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");
});
test("propertyEqual", function() {
2013-10-17 12:52:24 -04:00
var t = Em.Object.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
});
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");
});
test("propertyNotEqual", function() {
2013-10-17 12:52:24 -04:00
var t = Em.Object.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
});
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");
});
test("fmt", function() {
2013-10-17 12:52:24 -04:00
var t = Em.Object.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"
});
equal(t.get('exclaimyUsername'), '!!! eviltrout !!!', "it inserts the string");
equal(t.get('multiple'), "eviltrout is happy", "it inserts multiple strings");
t.set('username', 'codinghorror');
equal(t.get('multiple'), "codinghorror is happy", "it supports changing properties");
t.set('mood', 'ecstatic');
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({
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"
});
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");
t.set('username', 'codinghorror');
2013-10-17 12:52:24 -04:00
equal(t.get('multiple'), "%@ translated: codinghorror is happy", "it supports changing properties");
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-10-17 12:52:24 -04:00
test("url", function() {
var t, testClass;
testClass = Em.Object.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-03-28 14:27:54 -04:00
equal(t.get('userUrl'), "/u/eviltrout", "it supports urls without a prefix");
Discourse.BaseUri = "/prefixed";
2013-10-17 12:52:24 -04:00
t = testClass.create({ username: 'eviltrout' });
2017-03-28 14:27:54 -04:00
equal(t.get('userUrl'), "/prefixed/u/eviltrout", "it supports urls with a prefix");
});