discourse/test/javascripts/controllers/topic-test.js.es6

127 lines
5.0 KiB
Plaintext
Raw Normal View History

2016-11-08 14:29:50 -05:00
import { mapRoutes } from 'discourse/mapping-router';
2014-07-30 17:53:14 -04:00
moduleFor('controller:topic', 'controller:topic', {
2016-11-11 16:00:28 -05:00
needs: ['controller:modal', 'controller:composer', 'controller:application'],
2017-06-14 13:57:58 -04:00
beforeEach() {
2016-11-08 14:29:50 -05:00
this.registry.register('router:main', mapRoutes());
},
2014-07-30 17:53:14 -04:00
});
import Topic from 'discourse/models/topic';
import AppEvents from 'discourse/lib/app-events';
2013-09-04 11:53:00 -04:00
var buildTopic = function() {
return Topic.create({
2013-09-04 11:53:00 -04:00
title: "Qunit Test Topic",
participants: [
{id: 1234,
post_count: 4,
username: "eviltrout"}
]
});
};
2014-07-30 17:53:14 -04:00
2017-06-14 13:57:58 -04:00
QUnit.test("editingMode", function(assert) {
2013-09-04 11:53:00 -04:00
var topic = buildTopic(),
2014-07-30 17:53:14 -04:00
topicController = this.subject({model: topic});
2017-06-14 13:57:58 -04:00
assert.ok(!topicController.get('editingTopic'), "we are not editing by default");
topicController.set('model.details.can_edit', false);
2013-09-16 14:08:55 -04:00
topicController.send('editTopic');
2017-06-14 13:57:58 -04:00
assert.ok(!topicController.get('editingTopic'), "calling editTopic doesn't enable editing unless the user can edit");
topicController.set('model.details.can_edit', true);
2013-09-16 14:08:55 -04:00
topicController.send('editTopic');
2017-06-14 13:57:58 -04:00
assert.ok(topicController.get('editingTopic'), "calling editTopic enables editing if the user can edit");
assert.equal(topicController.get('buffered.title'), topic.get('title'));
assert.equal(topicController.get('buffered.category_id'), topic.get('category_id'));
2013-09-16 14:08:55 -04:00
topicController.send('cancelEditingTopic');
2017-06-14 13:57:58 -04:00
assert.ok(!topicController.get('editingTopic'), "cancelling edit mode reverts the property value");
2013-09-04 11:53:00 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("toggledSelectedPost", function(assert) {
2014-07-30 17:53:14 -04:00
var tc = this.subject({ model: buildTopic() }),
2013-09-04 11:53:00 -04:00
post = Discourse.Post.create({id: 123, post_number: 2}),
postStream = tc.get('model.postStream');
2013-09-04 11:53:00 -04:00
postStream.appendPost(post);
postStream.appendPost(Discourse.Post.create({id: 124, post_number: 3}));
2017-06-14 13:57:58 -04:00
assert.blank(tc.get('selectedPosts'), "there are no selected posts by default");
assert.equal(tc.get('selectedPostsCount'), 0, "there is a selected post count of 0");
assert.ok(!tc.postSelected(post), "the post is not selected by default");
2013-09-04 11:53:00 -04:00
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPost', post);
2017-06-14 13:57:58 -04:00
assert.present(tc.get('selectedPosts'), "there is a selectedPosts collection");
assert.equal(tc.get('selectedPostsCount'), 1, "there is a selected post now");
assert.ok(tc.postSelected(post), "the post is now selected");
2013-09-04 11:53:00 -04:00
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPost', post);
2017-06-14 13:57:58 -04:00
assert.ok(!tc.postSelected(post), "the post is no longer selected");
2013-09-04 11:53:00 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("selectAll", function(assert) {
var tc = this.subject({model: buildTopic(), appEvents: AppEvents.create()}),
2013-09-04 11:53:00 -04:00
post = Discourse.Post.create({id: 123, post_number: 2}),
postStream = tc.get('model.postStream');
2013-09-04 11:53:00 -04:00
postStream.appendPost(post);
2017-06-14 13:57:58 -04:00
assert.ok(!tc.postSelected(post), "the post is not selected by default");
2013-09-16 14:08:55 -04:00
tc.send('selectAll');
2017-06-14 13:57:58 -04:00
assert.ok(tc.postSelected(post), "the post is now selected");
assert.ok(tc.get('allPostsSelected'), "all posts are selected");
2013-09-16 14:08:55 -04:00
tc.send('deselectAll');
2017-06-14 13:57:58 -04:00
assert.ok(!tc.postSelected(post), "the post is deselected again");
assert.ok(!tc.get('allPostsSelected'), "all posts are not selected");
2013-09-04 11:53:00 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("Automating setting of allPostsSelected", function(assert) {
2013-09-04 11:53:00 -04:00
var topic = buildTopic(),
2014-07-30 17:53:14 -04:00
tc = this.subject({model: topic}),
2013-09-04 11:53:00 -04:00
post = Discourse.Post.create({id: 123, post_number: 2}),
postStream = tc.get('model.postStream');
2013-09-04 11:53:00 -04:00
topic.set('posts_count', 1);
postStream.appendPost(post);
2017-06-14 13:57:58 -04:00
assert.ok(!tc.get('allPostsSelected'), "all posts are not selected by default");
2013-09-04 11:53:00 -04:00
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPost', post);
2017-06-14 13:57:58 -04:00
assert.ok(tc.get('allPostsSelected'), "all posts are selected if we select the only post");
2013-09-04 11:53:00 -04:00
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPost', post);
2017-06-14 13:57:58 -04:00
assert.ok(!tc.get('allPostsSelected'), "the posts are no longer automatically selected");
2013-09-04 11:53:00 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("Select Replies when present", function(assert) {
2013-09-04 11:53:00 -04:00
var topic = buildTopic(),
2017-05-30 15:13:56 -04:00
tc = this.subject({ model: topic, appEvents: AppEvents.create() }),
2013-09-04 11:53:00 -04:00
p1 = Discourse.Post.create({id: 1, post_number: 1, reply_count: 1}),
p2 = Discourse.Post.create({id: 2, post_number: 2}),
p3 = Discourse.Post.create({id: 2, post_number: 3, reply_to_post_number: 1});
2013-09-04 11:53:00 -04:00
2017-06-14 13:57:58 -04:00
assert.ok(!tc.postSelected(p3), "replies are not selected by default");
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPostReplies', p1);
2017-06-14 13:57:58 -04:00
assert.ok(tc.postSelected(p1), "it selects the post");
assert.ok(!tc.postSelected(p2), "it doesn't select a post that's not a reply");
assert.ok(tc.postSelected(p3), "it selects a post that is a reply");
assert.equal(tc.get('selectedPostsCount'), 2, "it has a selected posts count of two");
2013-09-04 11:53:00 -04:00
// If we deselected the post whose replies are selected...
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPost', p1);
2017-06-14 13:57:58 -04:00
assert.ok(!tc.postSelected(p1), "it deselects the post");
assert.ok(!tc.postSelected(p3), "it deselects the replies too");
2013-09-04 11:53:00 -04:00
// If we deselect a reply, it should deselect the parent's replies selected attribute. Weird but what else would make sense?
2013-09-16 14:08:55 -04:00
tc.send('toggledSelectedPostReplies', p1);
tc.send('toggledSelectedPost', p3);
2017-06-14 13:57:58 -04:00
assert.ok(tc.postSelected(p1), "the post stays selected");
assert.ok(!tc.postSelected(p3), "it deselects the replies too");
2013-09-04 11:53:00 -04:00
});