2019-10-30 03:23:50 +08:00
|
|
|
import EmberObject from "@ember/object";
|
2015-08-08 03:08:27 +08:00
|
|
|
import Singleton from "discourse/mixins/singleton";
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.module("mixin:singleton");
|
2013-08-09 00:00:58 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("current", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var DummyModel = EmberObject.extend({});
|
2015-08-08 03:08:27 +08:00
|
|
|
DummyModel.reopenClass(Singleton);
|
2013-08-09 00:00:58 +08:00
|
|
|
|
|
|
|
var current = DummyModel.current();
|
2017-06-15 01:57:58 +08: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"
|
|
|
|
);
|
2013-08-09 00:00:58 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("currentProp reading", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var DummyModel = EmberObject.extend({});
|
2015-08-08 03:08:27 +08:00
|
|
|
DummyModel.reopenClass(Singleton);
|
2013-08-09 00:00:58 +08:00
|
|
|
var current = DummyModel.current();
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(
|
|
|
|
DummyModel.currentProp("evil"),
|
|
|
|
"by default attributes are blank"
|
|
|
|
);
|
2013-08-09 00:00:58 +08:00
|
|
|
current.set("evil", "trout");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
DummyModel.currentProp("evil"),
|
|
|
|
"trout",
|
|
|
|
"after changing the instance, the value is set"
|
|
|
|
);
|
2013-08-09 00:00:58 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("currentProp writing", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var DummyModel = EmberObject.extend({});
|
2015-08-08 03:08:27 +08:00
|
|
|
DummyModel.reopenClass(Singleton);
|
2013-08-09 00:00:58 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(
|
|
|
|
DummyModel.currentProp("adventure"),
|
|
|
|
"by default attributes are blank"
|
|
|
|
);
|
2013-08-09 00:00:58 +08:00
|
|
|
var result = DummyModel.currentProp("adventure", "time");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(result, "time", "it returns the new value");
|
|
|
|
assert.equal(
|
|
|
|
DummyModel.currentProp("adventure"),
|
|
|
|
"time",
|
|
|
|
"after calling currentProp the value is set"
|
|
|
|
);
|
2013-08-09 00:00:58 +08:00
|
|
|
|
|
|
|
DummyModel.currentProp("count", 0);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(DummyModel.currentProp("count"), 0, "we can set the value to 0");
|
2013-08-09 00:00:58 +08:00
|
|
|
|
|
|
|
DummyModel.currentProp("adventure", null);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
DummyModel.currentProp("adventure"),
|
|
|
|
null,
|
|
|
|
"we can set the value to null"
|
|
|
|
);
|
2013-08-09 00:42:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("createCurrent", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var Shoe = EmberObject.extend({});
|
2015-08-08 03:08:27 +08:00
|
|
|
Shoe.reopenClass(Singleton, {
|
2013-08-09 00:42:08 +08:00
|
|
|
createCurrent: function() {
|
|
|
|
return Shoe.create({ toes: 5 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(
|
|
|
|
Shoe.currentProp("toes"),
|
|
|
|
5,
|
|
|
|
"it created the class using `createCurrent`"
|
|
|
|
);
|
2013-08-09 00:42:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("createCurrent that returns null", assert => {
|
2019-10-30 03:23:50 +08:00
|
|
|
var Missing = EmberObject.extend({});
|
2015-08-08 03:08:27 +08:00
|
|
|
Missing.reopenClass(Singleton, {
|
2013-08-09 00:42:08 +08:00
|
|
|
createCurrent: function() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(Missing.current(), "it doesn't return an instance");
|
|
|
|
assert.blank(
|
|
|
|
Missing.currentProp("madeup"),
|
|
|
|
"it won't raise an error asking for a property. Will just return null."
|
|
|
|
);
|
|
|
|
});
|