discourse/test/javascripts/mixins/singleton-test.js.es6

96 lines
2.4 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import Singleton from "discourse/mixins/singleton";
2015-08-07 15:08:27 -04:00
2017-06-14 13:57:58 -04:00
QUnit.module("mixin:singleton");
2017-06-14 13:57:58 -04:00
QUnit.test("current", assert => {
var DummyModel = Ember.Object.extend({});
2015-08-07 15:08:27 -04:00
DummyModel.reopenClass(Singleton);
var current = DummyModel.current();
2018-06-15 11:03:24 -04:00
assert.present(current, "current returns the current instance");
assert.equal(
current,
DummyModel.current(),
"calling it again returns the same instance"
);
assert.notEqual(
current,
DummyModel.create({}),
"we can create other instances that are not the same as current"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("currentProp reading", assert => {
var DummyModel = Ember.Object.extend({});
2015-08-07 15:08:27 -04:00
DummyModel.reopenClass(Singleton);
var current = DummyModel.current();
2018-06-15 11:03:24 -04:00
assert.blank(
DummyModel.currentProp("evil"),
"by default attributes are blank"
);
current.set("evil", "trout");
assert.equal(
DummyModel.currentProp("evil"),
"trout",
"after changing the instance, the value is set"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("currentProp writing", assert => {
var DummyModel = Ember.Object.extend({});
2015-08-07 15:08:27 -04:00
DummyModel.reopenClass(Singleton);
2018-06-15 11:03:24 -04:00
assert.blank(
DummyModel.currentProp("adventure"),
"by default attributes are blank"
);
var result = DummyModel.currentProp("adventure", "time");
assert.equal(result, "time", "it returns the new value");
assert.equal(
DummyModel.currentProp("adventure"),
"time",
"after calling currentProp the value is set"
);
2018-06-15 11:03:24 -04:00
DummyModel.currentProp("count", 0);
assert.equal(DummyModel.currentProp("count"), 0, "we can set the value to 0");
2018-06-15 11:03:24 -04:00
DummyModel.currentProp("adventure", null);
assert.equal(
DummyModel.currentProp("adventure"),
null,
"we can set the value to null"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("createCurrent", assert => {
var Shoe = Ember.Object.extend({});
2015-08-07 15:08:27 -04:00
Shoe.reopenClass(Singleton, {
createCurrent: function() {
2018-06-15 11:03:24 -04:00
return Shoe.create({ toes: 5 });
}
});
2018-06-15 11:03:24 -04:00
assert.equal(
Shoe.currentProp("toes"),
5,
"it created the class using `createCurrent`"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test("createCurrent that returns null", assert => {
var Missing = Ember.Object.extend({});
2015-08-07 15:08:27 -04:00
Missing.reopenClass(Singleton, {
createCurrent: function() {
return null;
}
});
2017-06-14 13:57:58 -04:00
assert.blank(Missing.current(), "it doesn't return an instance");
2018-06-15 11:03:24 -04:00
assert.blank(
Missing.currentProp("madeup"),
"it won't raise an error asking for a property. Will just return null."
);
});