2015-08-11 12:27:07 -04:00
|
|
|
import { blank } from 'helpers/qunit-helpers';
|
2015-04-01 14:18:46 -04:00
|
|
|
import { currentUser } from 'helpers/qunit-helpers';
|
|
|
|
|
|
|
|
module("model:composer");
|
|
|
|
|
|
|
|
function createComposer(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
opts.user = opts.user || currentUser();
|
|
|
|
opts.site = Discourse.Site.current();
|
|
|
|
opts.siteSettings = Discourse.SiteSettings;
|
|
|
|
return Discourse.Composer.create(opts);
|
|
|
|
}
|
2013-06-17 11:38:30 -04:00
|
|
|
|
|
|
|
test('replyLength', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const replyLength = function(val, expectedLength) {
|
|
|
|
const composer = createComposer({ reply: val });
|
2013-06-17 11:38:30 -04:00
|
|
|
equal(composer.get('replyLength'), expectedLength);
|
|
|
|
};
|
|
|
|
|
|
|
|
replyLength("basic reply", 11, "basic reply length");
|
|
|
|
replyLength(" \nbasic reply\t", 11, "trims whitespaces");
|
|
|
|
replyLength("ba sic\n\nreply", 12, "count only significant whitespaces");
|
|
|
|
replyLength("1[quote=]not counted[/quote]2[quote=]at all[/quote]3", 3, "removes quotes");
|
|
|
|
replyLength("1[quote=]not[quote=]counted[/quote]yay[/quote]2", 2, "handles nested quotes correctly");
|
2013-06-20 12:33:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('missingReplyCharacters', function() {
|
2015-03-19 10:17:55 -04:00
|
|
|
Discourse.SiteSettings.min_first_post_length = 40;
|
2015-04-01 14:18:46 -04:00
|
|
|
const missingReplyCharacters = function(val, isPM, isFirstPost, expected, message) {
|
|
|
|
const composer = createComposer({ reply: val, creatingPrivateMessage: isPM, creatingTopic: isFirstPost });
|
2013-06-20 12:33:21 -04:00
|
|
|
equal(composer.get('missingReplyCharacters'), expected, message);
|
2013-06-21 14:06:20 -04:00
|
|
|
};
|
2013-06-20 12:33:21 -04:00
|
|
|
|
2015-03-19 10:17:55 -04:00
|
|
|
missingReplyCharacters('hi', false, false, Discourse.SiteSettings.min_post_length - 2, 'too short public post');
|
|
|
|
missingReplyCharacters('hi', false, true, Discourse.SiteSettings.min_first_post_length - 2, 'too short first post');
|
|
|
|
missingReplyCharacters('hi', true, false, Discourse.SiteSettings.min_private_message_post_length - 2, 'too short private message');
|
2013-06-20 12:33:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('missingTitleCharacters', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const missingTitleCharacters = function(val, isPM, expected, message) {
|
|
|
|
const composer = createComposer({ title: val, creatingPrivateMessage: isPM });
|
2013-06-20 12:33:21 -04:00
|
|
|
equal(composer.get('missingTitleCharacters'), expected, message);
|
2013-06-21 14:06:20 -04:00
|
|
|
};
|
2013-06-20 12:33:21 -04:00
|
|
|
|
|
|
|
missingTitleCharacters('hi', false, Discourse.SiteSettings.min_topic_title_length - 2, 'too short post title');
|
|
|
|
missingTitleCharacters('z', true, Discourse.SiteSettings.min_private_message_title_length - 1, 'too short pm title');
|
2013-07-04 13:35:15 -04:00
|
|
|
});
|
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
test('replyDirty', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer();
|
2013-07-05 11:53:09 -04:00
|
|
|
ok(!composer.get('replyDirty'), "by default it's false");
|
2013-07-04 13:35:15 -04:00
|
|
|
|
|
|
|
composer.setProperties({
|
|
|
|
originalText: "hello",
|
|
|
|
reply: "hello"
|
|
|
|
});
|
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
ok(!composer.get('replyDirty'), "it's false when the originalText is the same as the reply");
|
2013-07-04 13:35:15 -04:00
|
|
|
composer.set('reply', 'hello world');
|
2013-07-05 11:53:09 -04:00
|
|
|
ok(composer.get('replyDirty'), "it's true when the reply changes");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("appendText", function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer();
|
2013-07-05 11:53:09 -04:00
|
|
|
|
|
|
|
blank(composer.get('reply'), "the reply is blank by default");
|
|
|
|
|
|
|
|
composer.appendText("hello");
|
|
|
|
equal(composer.get('reply'), "hello", "it appends text to nothing");
|
|
|
|
composer.appendText(" world");
|
|
|
|
equal(composer.get('reply'), "hello world", "it appends text to existing text");
|
|
|
|
|
2014-06-18 01:17:32 -04:00
|
|
|
composer.clearState();
|
|
|
|
composer.appendText("a\n\n\n\nb");
|
|
|
|
composer.appendText("c",3,{block: true});
|
|
|
|
|
|
|
|
equal(composer.get("reply"), "a\n\nc\n\nb");
|
|
|
|
|
|
|
|
composer.clearState();
|
|
|
|
composer.appendText("ab");
|
|
|
|
composer.appendText("c",1,{block: true});
|
|
|
|
|
|
|
|
equal(composer.get("reply"), "a\n\nc\n\nb");
|
|
|
|
|
|
|
|
composer.clearState();
|
|
|
|
composer.appendText("\nab");
|
|
|
|
composer.appendText("c",0,{block: true});
|
|
|
|
|
|
|
|
equal(composer.get("reply"), "c\n\nab");
|
2013-07-04 13:35:15 -04:00
|
|
|
});
|
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
test("Title length for regular topics", function() {
|
|
|
|
Discourse.SiteSettings.min_topic_title_length = 5;
|
|
|
|
Discourse.SiteSettings.max_topic_title_length = 10;
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer();
|
2013-07-05 11:53:09 -04:00
|
|
|
|
|
|
|
composer.set('title', 'asdf');
|
|
|
|
ok(!composer.get('titleLengthValid'), "short titles are not valid");
|
|
|
|
|
|
|
|
composer.set('title', 'this is a long title');
|
|
|
|
ok(!composer.get('titleLengthValid'), "long titles are not valid");
|
|
|
|
|
|
|
|
composer.set('title', 'just right');
|
|
|
|
ok(composer.get('titleLengthValid'), "in the range is okay");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("Title length for private messages", function() {
|
|
|
|
Discourse.SiteSettings.min_private_message_title_length = 5;
|
|
|
|
Discourse.SiteSettings.max_topic_title_length = 10;
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer({action: Discourse.Composer.PRIVATE_MESSAGE});
|
2013-07-05 11:53:09 -04:00
|
|
|
|
|
|
|
composer.set('title', 'asdf');
|
|
|
|
ok(!composer.get('titleLengthValid'), "short titles are not valid");
|
2013-07-04 13:35:15 -04:00
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
composer.set('title', 'this is a long title');
|
|
|
|
ok(!composer.get('titleLengthValid'), "long titles are not valid");
|
|
|
|
|
|
|
|
composer.set('title', 'just right');
|
|
|
|
ok(composer.get('titleLengthValid'), "in the range is okay");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("Title length for private messages", function() {
|
|
|
|
Discourse.SiteSettings.min_private_message_title_length = 5;
|
|
|
|
Discourse.SiteSettings.max_topic_title_length = 10;
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer({action: Discourse.Composer.PRIVATE_MESSAGE});
|
2013-07-05 11:53:09 -04:00
|
|
|
|
|
|
|
composer.set('title', 'asdf');
|
|
|
|
ok(!composer.get('titleLengthValid'), "short titles are not valid");
|
|
|
|
|
|
|
|
composer.set('title', 'this is a long title');
|
|
|
|
ok(!composer.get('titleLengthValid'), "long titles are not valid");
|
|
|
|
|
|
|
|
composer.set('title', 'just right');
|
|
|
|
ok(composer.get('titleLengthValid'), "in the range is okay");
|
|
|
|
});
|
2013-07-04 13:35:15 -04:00
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
test('editingFirstPost', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer();
|
2013-07-05 11:53:09 -04:00
|
|
|
ok(!composer.get('editingFirstPost'), "it's false by default");
|
|
|
|
|
2015-04-01 14:18:46 -04:00
|
|
|
const post = Discourse.Post.create({id: 123, post_number: 2});
|
2013-07-05 11:53:09 -04:00
|
|
|
composer.setProperties({post: post, action: Discourse.Composer.EDIT });
|
|
|
|
ok(!composer.get('editingFirstPost'), "it's false when not editing the first post");
|
|
|
|
|
|
|
|
post.set('post_number', 1);
|
|
|
|
ok(composer.get('editingFirstPost'), "it's true when editing the first post");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-07-05 13:01:51 -04:00
|
|
|
test('clearState', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = createComposer({
|
2013-07-05 13:01:51 -04:00
|
|
|
originalText: 'asdf',
|
|
|
|
reply: 'asdf2',
|
|
|
|
post: Discourse.Post.create({id: 1}),
|
|
|
|
title: 'wat'
|
|
|
|
});
|
|
|
|
|
|
|
|
composer.clearState();
|
|
|
|
|
|
|
|
blank(composer.get('originalText'));
|
|
|
|
blank(composer.get('reply'));
|
|
|
|
blank(composer.get('post'));
|
|
|
|
blank(composer.get('title'));
|
|
|
|
|
|
|
|
});
|
2013-08-21 16:14:04 -04:00
|
|
|
|
|
|
|
test('initial category when uncategorized is allowed', function() {
|
|
|
|
Discourse.SiteSettings.allow_uncategorized_topics = true;
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
|
2013-07-17 18:58:25 -04:00
|
|
|
equal(composer.get('categoryId'),undefined,"Uncategorized by default");
|
2013-08-21 16:14:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('initial category when uncategorized is not allowed', function() {
|
|
|
|
Discourse.SiteSettings.allow_uncategorized_topics = false;
|
2015-04-01 14:18:46 -04:00
|
|
|
const composer = Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
|
2013-10-08 14:40:31 -04:00
|
|
|
ok(composer.get('categoryId') === undefined, "Uncategorized by default. Must choose a category.");
|
2013-10-17 12:19:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('showPreview', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const newComposer = function() {
|
2013-10-17 12:19:09 -04:00
|
|
|
return Discourse.Composer.open({action: 'createTopic', draftKey: 'asfd', draftSequence: 1});
|
|
|
|
};
|
|
|
|
|
|
|
|
Discourse.Mobile.mobileView = true;
|
2015-04-01 14:18:46 -04:00
|
|
|
equal(newComposer().get('showPreview'), false, "Don't show preview in mobile view");
|
2013-10-17 12:19:09 -04:00
|
|
|
|
|
|
|
Discourse.KeyValueStore.set({ key: 'composer.showPreview', value: 'true' });
|
2015-04-01 14:18:46 -04:00
|
|
|
equal(newComposer().get('showPreview'), false, "Don't show preview in mobile view even if KeyValueStore wants to");
|
2013-10-17 12:19:09 -04:00
|
|
|
Discourse.KeyValueStore.remove('composer.showPreview');
|
|
|
|
|
|
|
|
Discourse.Mobile.mobileView = false;
|
2015-04-01 14:18:46 -04:00
|
|
|
equal(newComposer().get('showPreview'), true, "Show preview by default in desktop view");
|
2013-10-17 12:19:09 -04:00
|
|
|
});
|
2013-11-15 13:13:42 -05:00
|
|
|
|
|
|
|
test('open with a quote', function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
const quote = '[quote="neil, post:5, topic:413"]\nSimmer down you two.\n[/quote]';
|
|
|
|
const newComposer = function() {
|
2013-11-15 13:13:42 -05:00
|
|
|
return Discourse.Composer.open({action: Discourse.Composer.REPLY, draftKey: 'asfd', draftSequence: 1, quote: quote});
|
|
|
|
};
|
|
|
|
|
2015-04-01 14:18:46 -04:00
|
|
|
equal(newComposer().get('originalText'), quote, "originalText is the quote" );
|
|
|
|
equal(newComposer().get('replyDirty'), false, "replyDirty is initally false with a quote" );
|
2014-07-28 16:40:06 -04:00
|
|
|
});
|
|
|
|
|
2014-08-01 14:53:35 -04:00
|
|
|
test("Title length for static page topics as admin", function() {
|
2015-04-01 14:18:46 -04:00
|
|
|
Discourse.SiteSettings.min_topic_title_length = 5;
|
|
|
|
Discourse.SiteSettings.max_topic_title_length = 10;
|
|
|
|
const composer = createComposer();
|
2014-07-28 16:40:06 -04:00
|
|
|
|
2015-04-01 14:18:46 -04:00
|
|
|
const post = Discourse.Post.create({id: 123, post_number: 2, static_doc: true});
|
2014-08-01 14:53:35 -04:00
|
|
|
composer.setProperties({post: post, action: Discourse.Composer.EDIT });
|
|
|
|
|
2014-07-28 16:40:06 -04:00
|
|
|
composer.set('title', 'asdf');
|
|
|
|
ok(composer.get('titleLengthValid'), "admins can use short titles");
|
|
|
|
|
|
|
|
composer.set('title', 'this is a long title');
|
|
|
|
ok(composer.get('titleLengthValid'), "admins can use long titles");
|
|
|
|
|
|
|
|
composer.set('title', 'just right');
|
|
|
|
ok(composer.get('titleLengthValid'), "in the range is okay");
|
|
|
|
|
|
|
|
composer.set('title', '');
|
|
|
|
ok(!composer.get('titleLengthValid'), "admins must set title to at least 1 character");
|
2014-07-30 18:56:01 -04:00
|
|
|
});
|