2015-09-04 04:55:55 +08:00
|
|
|
import createStore from 'helpers/create-store';
|
2016-04-01 05:33:25 +08:00
|
|
|
import Category from 'discourse/models/category';
|
2015-09-04 04:55:55 +08:00
|
|
|
|
|
|
|
module("model:category");
|
2013-06-17 23:38:30 +08:00
|
|
|
|
|
|
|
test('slugFor', function(){
|
2015-09-04 04:55:55 +08:00
|
|
|
const store = createStore();
|
2013-06-17 23:38:30 +08:00
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
const slugFor = function(cat, val, text) {
|
2013-10-24 02:40:39 +08:00
|
|
|
equal(Discourse.Category.slugFor(cat), val, text);
|
2013-06-22 02:06:20 +08:00
|
|
|
};
|
2013-06-17 23:38:30 +08:00
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
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");
|
2013-06-17 23:38:30 +08:00
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
const parentCategory = store.createRecord('category', {id: 345, slug: 'darth'});
|
|
|
|
slugFor(store.createRecord('category', {slug: 'luke', parentCategory: parentCategory}),
|
2013-10-24 02:40:39 +08:00
|
|
|
"darth/luke",
|
|
|
|
"it uses the parent slug before the child");
|
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
slugFor(store.createRecord('category', {id: 555, parentCategory: parentCategory}),
|
2013-10-24 02:40:39 +08:00
|
|
|
"darth/555-category",
|
|
|
|
"it uses the parent slug before the child and then uses id");
|
|
|
|
|
|
|
|
parentCategory.set('slug', null);
|
2015-09-04 04:55:55 +08:00
|
|
|
slugFor(store.createRecord('category', {id: 555, parentCategory: parentCategory}),
|
2013-10-24 02:40:39 +08:00
|
|
|
"345-category/555-category",
|
|
|
|
"it uses the parent before the child and uses ids for both");
|
2013-10-24 01:29:20 +08:00
|
|
|
});
|
|
|
|
|
2013-10-24 02:40:39 +08:00
|
|
|
|
|
|
|
test('findBySlug', function() {
|
2015-04-13 22:50:41 +08:00
|
|
|
expect(6);
|
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
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}),
|
2015-04-13 22:50:41 +08:00
|
|
|
categoryList = [darth, luke, hurricane, newsFeed, time, bah];
|
|
|
|
|
|
|
|
sandbox.stub(Discourse.Category, 'list').returns(categoryList);
|
|
|
|
|
|
|
|
deepEqual(Discourse.Category.findBySlug('darth'), darth, 'we can find a category');
|
|
|
|
deepEqual(Discourse.Category.findBySlug('luke', 'darth'), luke, 'we can find the other category with parent category');
|
|
|
|
deepEqual(Discourse.Category.findBySlug('熱帶風暴畫眉'), hurricane, 'we can find a category with CJK slug');
|
|
|
|
deepEqual(Discourse.Category.findBySlug('뉴스피드', '熱帶風暴畫眉'), newsFeed, 'we can find a category with CJK slug whose parent slug is also CJK');
|
|
|
|
deepEqual(Discourse.Category.findBySlug('时间', 'darth'), time, 'we can find a category with CJK slug whose parent slug is english');
|
|
|
|
deepEqual(Discourse.Category.findBySlug('bah', '熱帶風暴畫眉'), bah, 'we can find a category with english slug whose parent slug is CJK');
|
2016-04-01 05:33:25 +08:00
|
|
|
|
|
|
|
sandbox.restore();
|
2015-04-13 22:50:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('findSingleBySlug', function() {
|
|
|
|
expect(6);
|
|
|
|
|
2015-09-04 04:55:55 +08:00
|
|
|
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}),
|
2015-04-13 22:50:41 +08:00
|
|
|
categoryList = [darth, luke, hurricane, newsFeed, time, bah];
|
2013-10-24 02:40:39 +08:00
|
|
|
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse.Category, 'list').returns(categoryList);
|
2013-10-24 02:40:39 +08:00
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('darth'), darth, 'we can find a category');
|
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('darth/luke'), luke, 'we can find the other category with parent category');
|
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('熱帶風暴畫眉'), hurricane, 'we can find a category with CJK slug');
|
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('熱帶風暴畫眉/뉴스피드'), newsFeed, 'we can find a category with CJK slug whose parent slug is also CJK');
|
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('darth/时间'), time, 'we can find a category with CJK slug whose parent slug is english');
|
|
|
|
deepEqual(Discourse.Category.findSingleBySlug('熱帶風暴畫眉/bah'), bah, 'we can find a category with english slug whose parent slug is CJK');
|
2013-12-14 04:15:51 +08:00
|
|
|
});
|
|
|
|
|
2014-07-23 11:47:11 +08:00
|
|
|
test('findByIds', function() {
|
2015-09-04 04:55:55 +08:00
|
|
|
const store = createStore();
|
|
|
|
const categories = {
|
|
|
|
1: store.createRecord('category', {id: 1}),
|
|
|
|
2: store.createRecord('category', {id: 2})
|
2014-07-23 11:47:11 +08:00
|
|
|
};
|
2014-02-04 12:14:10 +08:00
|
|
|
|
2014-07-31 06:56:01 +08:00
|
|
|
sandbox.stub(Discourse.Category, 'idMap').returns(categories);
|
2014-07-23 11:47:11 +08:00
|
|
|
deepEqual(Discourse.Category.findByIds([1,2,3]), _.values(categories));
|
2014-02-04 12:14:10 +08:00
|
|
|
});
|
|
|
|
|
2013-12-18 05:50:34 +08:00
|
|
|
test('postCountStats', function() {
|
2015-09-04 04:55:55 +08:00
|
|
|
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});
|
|
|
|
|
|
|
|
let result = category1.get('postCountStats');
|
2013-12-18 05:50:34 +08:00
|
|
|
equal(result.length, 1, "should only show year");
|
|
|
|
equal(result[0].value, 2);
|
|
|
|
equal(result[0].unit, 'year');
|
2013-12-14 04:15:51 +08:00
|
|
|
|
2013-12-18 05:50:34 +08:00
|
|
|
result = category2.get('postCountStats');
|
2013-12-14 04:15:51 +08:00
|
|
|
equal(result.length, 2, "should show month and year");
|
2013-12-18 05:50:34 +08:00
|
|
|
equal(result[0].value, 4);
|
|
|
|
equal(result[0].unit, 'month');
|
|
|
|
equal(result[1].value, 50);
|
|
|
|
equal(result[1].unit, 'year');
|
2013-12-14 04:15:51 +08:00
|
|
|
|
2013-12-18 05:50:34 +08:00
|
|
|
result = category3.get('postCountStats');
|
2013-12-14 04:15:51 +08:00
|
|
|
equal(result.length, 2, "should show week and month");
|
2013-12-18 05:50:34 +08:00
|
|
|
equal(result[0].value, 50);
|
|
|
|
equal(result[0].unit, 'week');
|
|
|
|
equal(result[1].value, 200);
|
|
|
|
equal(result[1].unit, 'month');
|
2013-12-14 04:15:51 +08:00
|
|
|
|
2013-12-18 05:50:34 +08:00
|
|
|
result = category4.get('postCountStats');
|
2013-12-14 04:15:51 +08:00
|
|
|
equal(result.length, 2, "should show day and week");
|
2013-12-18 05:50:34 +08:00
|
|
|
equal(result[0].value, 22);
|
|
|
|
equal(result[0].unit, 'day');
|
|
|
|
equal(result[1].value, 100);
|
|
|
|
equal(result[1].unit, 'week');
|
|
|
|
|
|
|
|
result = category5.get('postCountStats');
|
|
|
|
equal(result.length, 0, "should show nothing");
|
2013-12-14 04:15:51 +08:00
|
|
|
});
|
2016-04-01 05:33:25 +08:00
|
|
|
|
|
|
|
test('search with category name', () => {
|
|
|
|
const store = createStore(),
|
|
|
|
category1 = store.createRecord('category', { id: 1, name: 'middle term', slug: 'different-slug' }),
|
|
|
|
category2 = store.createRecord('category', { id: 2, name: 'middle term', slug: 'another-different-slug' });
|
|
|
|
|
|
|
|
sandbox.stub(Category, "listByActivity").returns([category1, category2]);
|
|
|
|
|
|
|
|
deepEqual(Category.search('term', { limit: 0 }), [], "returns an empty array when limit is 0");
|
|
|
|
deepEqual(Category.search(''), [category1, category2], "orders by activity if no term is matched");
|
|
|
|
deepEqual(Category.search('term'), [category1, category2], "orders by activity");
|
|
|
|
|
|
|
|
category2.set('name', 'TeRm start');
|
|
|
|
deepEqual(Category.search('tErM'), [category2, category1], "ignores case of category name and search term");
|
|
|
|
|
|
|
|
category2.set('name', 'term start');
|
|
|
|
deepEqual(Category.search('term'), [category2, category1], "orders matching begin with and then contains");
|
|
|
|
|
|
|
|
sandbox.restore();
|
|
|
|
|
|
|
|
const child_category1 = store.createRecord('category', { id: 3, name: 'term start', parent_category_id: category1.get('id') }),
|
|
|
|
read_restricted_category = store.createRecord('category', { id: 4, name: 'some term', read_restricted: true });
|
|
|
|
|
|
|
|
sandbox.stub(Category, "listByActivity").returns([read_restricted_category, category1, child_category1, category2]);
|
|
|
|
|
|
|
|
deepEqual(Category.search(''),
|
|
|
|
[category1, category2, read_restricted_category],
|
|
|
|
"prioritize non read_restricted and does not include child categories when term is blank");
|
|
|
|
|
|
|
|
deepEqual(Category.search('', { limit: 3 }),
|
|
|
|
[category1, category2, read_restricted_category],
|
|
|
|
"prioritize non read_restricted and does not include child categories categories when term is blank with limit");
|
|
|
|
|
|
|
|
deepEqual(Category.search('term'),
|
|
|
|
[child_category1, category2, category1, read_restricted_category],
|
|
|
|
"prioritize non read_restricted");
|
|
|
|
|
|
|
|
deepEqual(Category.search('term', { limit: 3 }),
|
|
|
|
[child_category1, category2, read_restricted_category],
|
|
|
|
"prioritize non read_restricted with limit");
|
|
|
|
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('search with category slug', () => {
|
|
|
|
const store = createStore(),
|
|
|
|
category1 = store.createRecord('category', { id: 1, name: 'middle term', slug: 'different-slug' }),
|
|
|
|
category2 = store.createRecord('category', { id: 2, name: 'middle term', slug: 'another-different-slug' });
|
|
|
|
|
|
|
|
sandbox.stub(Category, "listByActivity").returns([category1, category2]);
|
|
|
|
|
|
|
|
deepEqual(Category.search('different-slug'), [category1, category2], "returns the right categories");
|
|
|
|
deepEqual(Category.search('another-different'), [category2], "returns the right categories");
|
|
|
|
|
|
|
|
category2.set('slug', 'ANOTher-DIFfereNT');
|
|
|
|
deepEqual(Category.search('anOtHer-dIfFeREnt'), [category2], "ignores case of category slug and search term");
|
|
|
|
});
|