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