Move TopicTrackingState to injected object

This commit is contained in:
Robin Ward
2015-09-03 16:55:55 -04:00
parent 064b62199e
commit 8e776d0fd7
33 changed files with 256 additions and 263 deletions
+16 -3
View File
@@ -1,16 +1,29 @@
import Store from "discourse/models/store";
import RestAdapter from 'discourse/adapters/rest';
import KeyValueStore from 'discourse/lib/key-value-store';
import TopicTrackingState from 'discourse/models/topic-tracking-state';
import Resolver from 'discourse/ember/resolver';
let _restAdapter;
export default function() {
const resolver = Resolver.create();
return Store.create({
container: {
lookup(type) {
if (type === "adapter:rest") {
_restAdapter = _restAdapter || RestAdapter.create({ container: this });
return (_restAdapter);
this._restAdapter = this._restAdapter || RestAdapter.create({ container: this });
return (this._restAdapter);
}
if (type === "key-value-store:main") {
this._kvs = this._kvs || new KeyValueStore();
return (this._kvs);
}
if (type === "topic-tracking-state:main") {
this._tracker = this._tracker || TopicTrackingState.current();
return (this._tracker);
}
if (type === "site-settings:main") {
this._settings = this._settings || Discourse.SiteSettings.current();
return (this._settings);
}
},
@@ -71,7 +71,7 @@ function acceptance(name, options) {
options.teardown.call(this);
}
Discourse.User.resetCurrent();
Discourse.Site.resetCurrent(Discourse.Site.create(fixtures['site.json'].site));
Discourse.Site.resetCurrent(Discourse.Site.create(jQuery.extend(true, {}, fixtures['site.json'].site)));
Discourse.Utilities.avatarImg = oldAvatar;
Discourse.reset();
+16 -12
View File
@@ -1,3 +1,4 @@
import createStore from 'helpers/create-store';
import { blank, present } from 'helpers/qunit-helpers';
module("lib:category-link");
@@ -10,33 +11,36 @@ test("categoryBadge without a category", function() {
});
test("Regular categoryBadge", function() {
var category = Discourse.Category.create({
name: 'hello',
id: 123,
description_text: 'cool description',
color: 'ff0',
text_color: 'f00'
}),
tag = parseHTML(categoryBadgeHTML(category))[0];
const store = createStore();
const category = store.createRecord('category', {
name: 'hello',
id: 123,
description_text: 'cool description',
color: 'ff0',
text_color: 'f00'
});
const tag = parseHTML(categoryBadgeHTML(category))[0];
equal(tag.name, 'a', 'it creates a `a` wrapper tag');
equal(tag.attributes['class'].trim(), 'badge-wrapper', 'it has the correct class');
var label = tag.children[1];
const label = tag.children[1];
equal(label.attributes.title, 'cool description', 'it has the correct title');
equal(label.children[0].data, 'hello', 'it has the category name');
});
test("undefined color", function() {
var noColor = Discourse.Category.create({ name: 'hello', id: 123 }),
tag = parseHTML(categoryBadgeHTML(noColor))[0];
const store = createStore();
const noColor = store.createRecord('category', { name: 'hello', id: 123 });
const tag = parseHTML(categoryBadgeHTML(noColor))[0];
blank(tag.attributes.style, "it has no color style because there are no colors");
});
test("allowUncategorized", function() {
var uncategorized = Discourse.Category.create({name: 'uncategorized', id: 345});
const store = createStore();
const uncategorized = store.createRecord('category', {name: 'uncategorized', id: 345});
sandbox.stub(Discourse.Site, 'currentProp').withArgs('uncategorized_category_id').returns(345);
blank(categoryBadgeHTML(uncategorized), "it doesn't return HTML for uncategorized by default");
+38 -31
View File
@@ -1,27 +1,30 @@
module("Discourse.Category");
import createStore from 'helpers/create-store';
module("model:category");
test('slugFor', function(){
const store = createStore();
var slugFor = function(cat, val, text) {
const slugFor = function(cat, val, text) {
equal(Discourse.Category.slugFor(cat), val, text);
};
slugFor(Discourse.Category.create({slug: 'hello'}), "hello", "It calculates the proper slug for hello");
slugFor(Discourse.Category.create({id: 123, slug: ''}), "123-category", "It returns id-category for empty strings");
slugFor(Discourse.Category.create({id: 456}), "456-category", "It returns id-category for undefined slugs");
slugFor(Discourse.Category.create({slug: '熱帶風暴畫眉'}), "熱帶風暴畫眉", "It can be non english characters");
slugFor(store.createRecord('category', {slug: 'hello'}), "hello", "It calculates the proper slug for hello");
slugFor(store.createRecord('category', {id: 123, slug: ''}), "123-category", "It returns id-category for empty strings");
slugFor(store.createRecord('category', {id: 456}), "456-category", "It returns id-category for undefined slugs");
slugFor(store.createRecord('category', {slug: '熱帶風暴畫眉'}), "熱帶風暴畫眉", "It can be non english characters");
var parentCategory = Discourse.Category.create({id: 345, slug: 'darth'});
slugFor(Discourse.Category.create({slug: 'luke', parentCategory: parentCategory}),
const parentCategory = store.createRecord('category', {id: 345, slug: 'darth'});
slugFor(store.createRecord('category', {slug: 'luke', parentCategory: parentCategory}),
"darth/luke",
"it uses the parent slug before the child");
slugFor(Discourse.Category.create({id: 555, parentCategory: parentCategory}),
slugFor(store.createRecord('category', {id: 555, parentCategory: parentCategory}),
"darth/555-category",
"it uses the parent slug before the child and then uses id");
parentCategory.set('slug', null);
slugFor(Discourse.Category.create({id: 555, parentCategory: parentCategory}),
slugFor(store.createRecord('category', {id: 555, parentCategory: parentCategory}),
"345-category/555-category",
"it uses the parent before the child and uses ids for both");
});
@@ -30,12 +33,13 @@ test('slugFor', function(){
test('findBySlug', function() {
expect(6);
var darth = Discourse.Category.create({id: 1, slug: 'darth'}),
luke = Discourse.Category.create({id: 2, slug: 'luke', parentCategory: darth}),
hurricane = Discourse.Category.create({id: 3, slug: '熱帶風暴畫眉'}),
newsFeed = Discourse.Category.create({id: 4, slug: '뉴스피드', parentCategory: hurricane}),
time = Discourse.Category.create({id: 5, slug: '时间', parentCategory: darth}),
bah = Discourse.Category.create({id: 6, slug: 'bah', parentCategory: hurricane}),
const store = createStore();
const darth = store.createRecord('category', {id: 1, slug: 'darth'}),
luke = store.createRecord('category', {id: 2, slug: 'luke', parentCategory: darth}),
hurricane = store.createRecord('category', {id: 3, slug: '熱帶風暴畫眉'}),
newsFeed = store.createRecord('category', {id: 4, slug: '뉴스피드', parentCategory: hurricane}),
time = store.createRecord('category', {id: 5, slug: '时间', parentCategory: darth}),
bah = store.createRecord('category', {id: 6, slug: 'bah', parentCategory: hurricane}),
categoryList = [darth, luke, hurricane, newsFeed, time, bah];
sandbox.stub(Discourse.Category, 'list').returns(categoryList);
@@ -51,12 +55,13 @@ test('findBySlug', function() {
test('findSingleBySlug', function() {
expect(6);
var darth = Discourse.Category.create({id: 1, slug: 'darth'}),
luke = Discourse.Category.create({id: 2, slug: 'luke', parentCategory: darth}),
hurricane = Discourse.Category.create({id: 3, slug: '熱帶風暴畫眉'}),
newsFeed = Discourse.Category.create({id: 4, slug: '뉴스피드', parentCategory: hurricane}),
time = Discourse.Category.create({id: 5, slug: '时间', parentCategory: darth}),
bah = Discourse.Category.create({id: 6, slug: 'bah', parentCategory: hurricane}),
const store = createStore();
const darth = store.createRecord('category', {id: 1, slug: 'darth'}),
luke = store.createRecord('category', {id: 2, slug: 'luke', parentCategory: darth}),
hurricane = store.createRecord('category', {id: 3, slug: '熱帶風暴畫眉'}),
newsFeed = store.createRecord('category', {id: 4, slug: '뉴스피드', parentCategory: hurricane}),
time = store.createRecord('category', {id: 5, slug: '时间', parentCategory: darth}),
bah = store.createRecord('category', {id: 6, slug: 'bah', parentCategory: hurricane}),
categoryList = [darth, luke, hurricane, newsFeed, time, bah];
sandbox.stub(Discourse.Category, 'list').returns(categoryList);
@@ -70,9 +75,10 @@ test('findSingleBySlug', function() {
});
test('findByIds', function() {
var categories = {
1: Discourse.Category.create({id: 1}),
2: Discourse.Category.create({id: 2})
const store = createStore();
const categories = {
1: store.createRecord('category', {id: 1}),
2: store.createRecord('category', {id: 2})
};
sandbox.stub(Discourse.Category, 'idMap').returns(categories);
@@ -80,13 +86,14 @@ test('findByIds', function() {
});
test('postCountStats', function() {
var category1 = Discourse.Category.create({id: 1, slug: 'unloved', posts_year: 2, posts_month: 0, posts_week: 0, posts_day: 0}),
category2 = Discourse.Category.create({id: 2, slug: 'hasbeen', posts_year: 50, posts_month: 4, posts_week: 0, posts_day: 0}),
category3 = Discourse.Category.create({id: 3, slug: 'solastweek', posts_year: 250, posts_month: 200, posts_week: 50, posts_day: 0}),
category4 = Discourse.Category.create({id: 4, slug: 'hotstuff', posts_year: 500, posts_month: 280, posts_week: 100, posts_day: 22}),
category5 = Discourse.Category.create({id: 6, slug: 'empty', posts_year: 0, posts_month: 0, posts_week: 0, posts_day: 0});
const store = createStore();
const category1 = store.createRecord('category', {id: 1, slug: 'unloved', posts_year: 2, posts_month: 0, posts_week: 0, posts_day: 0}),
category2 = store.createRecord('category', {id: 2, slug: 'hasbeen', posts_year: 50, posts_month: 4, posts_week: 0, posts_day: 0}),
category3 = store.createRecord('category', {id: 3, slug: 'solastweek', posts_year: 250, posts_month: 200, posts_week: 50, posts_day: 0}),
category4 = store.createRecord('category', {id: 4, slug: 'hotstuff', posts_year: 500, posts_month: 280, posts_week: 100, posts_day: 22}),
category5 = store.createRecord('category', {id: 6, slug: 'empty', posts_year: 0, posts_month: 0, posts_week: 0, posts_day: 0});
var result = category1.get('postCountStats');
let result = category1.get('postCountStats');
equal(result.length, 1, "should only show year");
equal(result[0].value, 2);
equal(result[0].unit, 'year');
+17 -12
View File
@@ -1,6 +1,8 @@
import { blank } from 'helpers/qunit-helpers';
import { currentUser } from 'helpers/qunit-helpers';
import KeyValueStore from 'discourse/lib/key-value-store';
import Composer from 'discourse/models/composer';
import createStore from 'helpers/create-store';
module("model:composer");
@@ -9,10 +11,13 @@ const keyValueStore = new KeyValueStore("_test_composer");
function createComposer(opts) {
opts = opts || {};
opts.user = opts.user || currentUser();
opts.site = Discourse.Site.current();
opts.siteSettings = Discourse.SiteSettings;
opts.keyValueStore = keyValueStore;
; return Discourse.Composer.create(opts);
return createStore().createRecord('composer', opts);
}
function openComposer(opts) {
const composer = createComposer(opts);
composer.open(opts);
return composer;
}
test('replyLength', function() {
@@ -111,7 +116,7 @@ test("Title length for regular topics", function() {
test("Title length for private messages", function() {
Discourse.SiteSettings.min_private_message_title_length = 5;
Discourse.SiteSettings.max_topic_title_length = 10;
const composer = createComposer({action: Discourse.Composer.PRIVATE_MESSAGE});
const composer = createComposer({action: Composer.PRIVATE_MESSAGE});
composer.set('title', 'asdf');
ok(!composer.get('titleLengthValid'), "short titles are not valid");
@@ -126,7 +131,7 @@ test("Title length for private messages", function() {
test("Title length for private messages", function() {
Discourse.SiteSettings.min_private_message_title_length = 5;
Discourse.SiteSettings.max_topic_title_length = 10;
const composer = createComposer({action: Discourse.Composer.PRIVATE_MESSAGE});
const composer = createComposer({action: Composer.PRIVATE_MESSAGE});
composer.set('title', 'asdf');
ok(!composer.get('titleLengthValid'), "short titles are not valid");
@@ -143,7 +148,7 @@ test('editingFirstPost', function() {
ok(!composer.get('editingFirstPost'), "it's false by default");
const post = Discourse.Post.create({id: 123, post_number: 2});
composer.setProperties({post: post, action: Discourse.Composer.EDIT });
composer.setProperties({post: post, action: Composer.EDIT });
ok(!composer.get('editingFirstPost'), "it's false when not editing the first post");
post.set('post_number', 1);
@@ -170,19 +175,19 @@ test('clearState', function() {
test('initial category when uncategorized is allowed', function() {
Discourse.SiteSettings.allow_uncategorized_topics = true;
const composer = Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
const composer = openComposer({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
equal(composer.get('categoryId'),undefined,"Uncategorized by default");
});
test('initial category when uncategorized is not allowed', function() {
Discourse.SiteSettings.allow_uncategorized_topics = false;
const composer = Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
const composer = openComposer({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
ok(composer.get('categoryId') === undefined, "Uncategorized by default. Must choose a category.");
});
test('showPreview', function() {
const newComposer = function() {
return Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
return openComposer({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
};
Discourse.Mobile.mobileView = true;
@@ -199,7 +204,7 @@ test('showPreview', function() {
test('open with a quote', function() {
const quote = '[quote="neil, post:5, topic:413"]\nSimmer down you two.\n[/quote]';
const newComposer = function() {
return Discourse.Composer.open({action: Discourse.Composer.REPLY, draftKey: 'asfd', draftSequence: 1, quote: quote});
return openComposer({action: Discourse.Composer.REPLY, draftKey: 'asfd', draftSequence: 1, quote: quote});
};
equal(newComposer().get('originalText'), quote, "originalText is the quote" );
@@ -212,7 +217,7 @@ test("Title length for static page topics as admin", function() {
const composer = createComposer();
const post = Discourse.Post.create({id: 123, post_number: 2, static_doc: true});
composer.setProperties({post: post, action: Discourse.Composer.EDIT });
composer.setProperties({post: post, action: Composer.EDIT });
composer.set('title', 'asdf');
ok(composer.get('titleLengthValid'), "admins can use short titles");
+9 -9
View File
@@ -1,14 +1,14 @@
import createStore from 'helpers/create-store';
import { blank, present } from 'helpers/qunit-helpers';
module("Discourse.Site");
module("model:site");
test('create', function() {
ok(Discourse.Site.create(), 'it can create with no parameters');
});
test('instance', function() {
var site = Discourse.Site.current();
const site = Discourse.Site.current();
present(site, "We have a current site singleton");
present(site.get('categories'), "The instance has a list of categories");
@@ -18,24 +18,24 @@ test('instance', function() {
});
test('create categories', function() {
var site = Discourse.Site.create({
const store = createStore();
const site = store.createRecord('site', {
categories: [{ id: 1234, name: 'Test'},
{ id: 3456, name: 'Test Subcategory', parent_category_id: 1234},
{ id: 3456, name: 'Invalid Subcategory', parent_category_id: 6666}]
{ id: 3458, name: 'Invalid Subcategory', parent_category_id: 6666}]
});
var categories = site.get('categories');
const categories = site.get('categories');
site.get('sortedCategories');
present(categories, "The categories are present");
equal(categories.length, 3, "it loaded all three categories");
var parent = categories.findBy('id', 1234);
const parent = categories.findBy('id', 1234);
present(parent, "it loaded the parent category");
blank(parent.get('parentCategory'), 'it has no parent category');
var subcategory = categories.findBy('id', 3456);
const subcategory = categories.findBy('id', 3456);
present(subcategory, "it loaded the subcategory");
equal(subcategory.get('parentCategory'), parent, "it has associated the child with the parent");
@@ -1,21 +1,19 @@
module("Discourse.TopicTrackingState");
import TopicTrackingState from 'discourse/models/topic-tracking-state';
test("sync", function () {
module("model:topic-tracking-state");
var state = Discourse.TopicTrackingState.create();
// fake track it
test("sync", function (assert) {
const state = TopicTrackingState.create();
state.states["t111"] = {last_read_post_number: null};
state.updateSeen(111, 7);
var list = {topics: [{
const list = {topics: [{
highest_post_number: null,
id: 111,
unread: 10,
new_posts: 10
}]};
}]};
state.sync(list, "new");
equal(list.topics.length, 0, "expect new topic to be removed as it was seen");
assert.equal(list.topics.length, 0, "expect new topic to be removed as it was seen");
});