From 52579179bde3c747b5126e270f5d971074dde7c0 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 6 Nov 2015 16:44:38 -0500 Subject: [PATCH] FIX: Broken tests in phantom, not in browser --- .../discourse/components/d-editor.js.es6 | 4 ++-- .../discourse/lib/emoji/emoji-toolbar.js.es6 | 2 +- test/javascripts/components/d-editor-test.js.es6 | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 71fcb6c5710..cccc12c15ae 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -276,8 +276,8 @@ export default Ember.Component.extend({ if (!this.get('ready')) { return; } const textarea = this.$('textarea.d-editor-input')[0]; - let start = textarea.selectionStart; - let end = textarea.selectionEnd; + const start = textarea.selectionStart; + const end = textarea.selectionEnd; const value = textarea.value.substring(start, end); const pre = textarea.value.slice(0, start); diff --git a/app/assets/javascripts/discourse/lib/emoji/emoji-toolbar.js.es6 b/app/assets/javascripts/discourse/lib/emoji/emoji-toolbar.js.es6 index 5219e39faa1..7df7dd75cf2 100644 --- a/app/assets/javascripts/discourse/lib/emoji/emoji-toolbar.js.es6 +++ b/app/assets/javascripts/discourse/lib/emoji/emoji-toolbar.js.es6 @@ -39,7 +39,7 @@ function initializeUngroupedIcons() { } function trackEmojiUsage(title) { - const recent = keyValueStore.getObject(EMOJI_USAGE); + const recent = keyValueStore.getObject(EMOJI_USAGE) || {}; if (!recent[title]) { recent[title] = { title: title, usage: 0 }; } recent[title]["usage"]++; diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6 index 8891f9fb55f..c0ab0172382 100644 --- a/test/javascripts/components/d-editor-test.js.es6 +++ b/test/javascripts/components/d-editor-test.js.es6 @@ -43,6 +43,12 @@ componentTest('updating the value refreshes the preview', { } }); +function jumpEnd(textarea) { + textarea.selectionStart = textarea.value.length; + textarea.selectionEnd = textarea.value.length; + return textarea; +} + function testCase(title, testFunc) { componentTest(title, { template: '{{d-editor value=value}}', @@ -50,13 +56,14 @@ function testCase(title, testFunc) { this.set('value', 'hello world.'); }, test(assert) { - const textarea = this.$('textarea.d-editor-input')[0]; + const textarea = jumpEnd(this.$('textarea.d-editor-input')[0]); testFunc.call(this, assert, textarea); } }); } testCase(`bold button with no selection`, function(assert, textarea) { + console.log(textarea.selectionStart); click(`button.bold`); andThen(() => { const example = I18n.t(`composer.bold_text`); @@ -203,7 +210,7 @@ componentTest('code button', { }, test(assert) { - const textarea = this.$('textarea.d-editor-input')[0]; + const textarea = jumpEnd(this.$('textarea.d-editor-input')[0]); click('button.code'); andThen(() => { @@ -403,6 +410,8 @@ testCase(`heading button with no selection`, function(assert, textarea) { assert.equal(textarea.selectionEnd, 17 + example.length); }); + textarea.selectionStart = 30; + textarea.selectionEnd = 30; click(`button.heading`); andThen(() => { assert.equal(this.get('value'), `hello world.\n\n${example}`); @@ -468,6 +477,7 @@ componentTest('emoji', { test(assert) { assert.equal($('.emoji-modal').length, 0); + jumpEnd(this.$('textarea.d-editor-input')[0]); click('button.emoji'); andThen(() => { assert.equal($('.emoji-modal').length, 1);