mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 09:10:25 +08:00
Add Discourse.Singleton
class mixin for creating singletons.
This commit is contained in:
parent
9564a6ce09
commit
8e1fae0459
10 changed files with 90 additions and 52 deletions
37
test/javascripts/mixins/singleton_test.js
Normal file
37
test/javascripts/mixins/singleton_test.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
module("Discourse.Singleton");
|
||||
|
||||
test("current", function() {
|
||||
var DummyModel = Ember.Object.extend({});
|
||||
DummyModel.reopenClass(Discourse.Singleton);
|
||||
|
||||
var current = DummyModel.current();
|
||||
present(current, 'current returns the current instance');
|
||||
equal(current, DummyModel.current(), 'calling it again returns the same instance');
|
||||
notEqual(current, DummyModel.create({}), 'we can create other instances that are not the same as current');
|
||||
});
|
||||
|
||||
test("currentProp reading", function() {
|
||||
var DummyModel = Ember.Object.extend({});
|
||||
DummyModel.reopenClass(Discourse.Singleton);
|
||||
var current = DummyModel.current();
|
||||
|
||||
blank(DummyModel.currentProp('evil'), 'by default attributes are blank');
|
||||
current.set('evil', 'trout');
|
||||
equal(DummyModel.currentProp('evil'), 'trout', 'after changing the instance, the value is set');
|
||||
});
|
||||
|
||||
test("currentProp writing", function() {
|
||||
var DummyModel = Ember.Object.extend({});
|
||||
DummyModel.reopenClass(Discourse.Singleton);
|
||||
|
||||
blank(DummyModel.currentProp('adventure'), 'by default attributes are blank');
|
||||
var result = DummyModel.currentProp('adventure', 'time');
|
||||
equal(result, 'time', 'it returns the new value');
|
||||
equal(DummyModel.currentProp('adventure'), 'time', 'after calling currentProp the value is set');
|
||||
|
||||
DummyModel.currentProp('count', 0);
|
||||
equal(DummyModel.currentProp('count'), 0, 'we can set the value to 0');
|
||||
|
||||
DummyModel.currentProp('adventure', null);
|
||||
equal(DummyModel.currentProp('adventure'), null, 'we can set the value to null');
|
||||
});
|
|
@ -1,25 +1,6 @@
|
|||
module("Discourse.Session");
|
||||
|
||||
test('current', function(){
|
||||
var session = Discourse.Session.current();
|
||||
|
||||
present(session, "We have a current site session");
|
||||
equal(session, Discourse.Session.current(), "Calling it a second time returns the same instance");
|
||||
|
||||
blank(Discourse.Session.current('orange'), "by default properties are nil");
|
||||
session.set('orange', 'newBlack');
|
||||
equal(Discourse.Session.current('orange'), "newBlack", "it remembers values");
|
||||
|
||||
Discourse.Session.current('orange', 'juice');
|
||||
equal(session.get('orange'), "juice", "it can be updated");
|
||||
|
||||
Discourse.Session.current('zero', 0);
|
||||
equal(session.get('zero'), 0);
|
||||
});
|
||||
|
||||
test('highestSeenByTopic', function() {
|
||||
|
||||
var session = Discourse.Session.current();
|
||||
deepEqual(session.get('highestSeenByTopic'), {}, "by default it returns an empty object");
|
||||
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue