Skip to content

Commit d84c861

Browse files
committed
Explicitly set range in some tests so they pass in Firefox
fixes #388
1 parent 2ab8d42 commit d84c861

File tree

5 files changed

+127
-100
lines changed

5 files changed

+127
-100
lines changed

tests/acceptance/editor-key-commands-test.js

Lines changed: 82 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
import { Editor } from 'mobiledoc-kit';
21
import { MODIFIERS } from 'mobiledoc-kit/utils/key';
32
import Keycodes from 'mobiledoc-kit/utils/keycodes';
43
import Browser from 'mobiledoc-kit/utils/browser';
54
import Helpers from '../test-helpers';
5+
import Range from 'mobiledoc-kit/utils/cursor/range';
66

77
const { module, test } = Helpers;
88

99
let editor, editorElement;
1010

11+
// In Firefox, if the window isn't active (which can happen when running tests
12+
// at SauceLabs), the editor element won't have the selection. This helper method
13+
// ensures that it has a cursor selection.
14+
// See https://github.com/bustlelabs/mobiledoc-kit/issues/388
15+
function renderIntoAndFocusTail(treeFn, options={}) {
16+
let editor = Helpers.mobiledoc.renderInto(editorElement, treeFn, options);
17+
editor.selectRange(new Range(editor.post.tailPosition()));
18+
return editor;
19+
}
20+
1121
module('Acceptance: Editor: Key Commands', {
1222
beforeEach() {
1323
editorElement = $('#editor')[0];
@@ -22,19 +32,17 @@ module('Acceptance: Editor: Key Commands', {
2232

2333
function testStatefulCommand({modifierName, key, command, markupName}) {
2434
test(`${command} applies markup ${markupName} to highlighted text`, (assert) => {
25-
assert.expect(2);
35+
assert.expect(3);
2636
let done = assert.async();
2737

2838
let modifier = MODIFIERS[modifierName];
2939
let modifierKeyCode = Keycodes[modifierName];
3040
let initialText = 'something';
31-
const mobiledoc = Helpers.mobiledoc.build(
32-
({post, markupSection, marker}) => post([
33-
markupSection('p', [marker(initialText)])
34-
]));
41+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
42+
markupSection('p', [marker(initialText)])
43+
]));
3544

36-
editor = new Editor({mobiledoc});
37-
editor.render(editorElement);
45+
assert.ok(editor.hasCursor(), 'precond - editor should have cursor');
3846

3947
assert.hasNoElement(`#editor ${markupName}`, `precond - no ${markupName} text`);
4048
Helpers.dom.selectText(editor ,initialText, editorElement);
@@ -50,18 +58,17 @@ function testStatefulCommand({modifierName, key, command, markupName}) {
5058

5159
test(`${command} toggles ${markupName} for next entered text`, (assert) => {
5260
let done = assert.async();
53-
assert.expect(7);
61+
assert.expect(8);
5462

5563
let modifier = MODIFIERS[modifierName];
5664
let modifierKeyCode = Keycodes[modifierName];
5765
let initialText = 'something';
58-
const mobiledoc = Helpers.mobiledoc.build(
59-
({post, markupSection, marker}) => post([
60-
markupSection('p', [marker(initialText)])
61-
]));
6266

63-
editor = new Editor({mobiledoc});
64-
editor.render(editorElement);
67+
editor =renderIntoAndFocusTail(({post, markupSection, marker}) => post([
68+
markupSection('p', [marker(initialText)])
69+
]));
70+
71+
assert.ok(editor.hasCursor(), 'has cursor');
6572

6673
assert.hasNoElement(`#editor ${markupName}`, `precond - no ${markupName} text`);
6774
Helpers.dom.moveCursorTo(editor,
@@ -143,13 +150,11 @@ testStatefulCommand({
143150

144151
test(`cmd-left goes to the beginning of a line (MacOS only)`, (assert) => {
145152
let initialText = 'something';
146-
const mobiledoc = Helpers.mobiledoc.build(
147-
({post, markupSection, marker}) => post([
148-
markupSection('p', [marker(initialText)])
149-
]));
153+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
154+
markupSection('p', [marker(initialText)])
155+
]));
150156

151-
editor = new Editor({mobiledoc});
152-
editor.render(editorElement);
157+
assert.ok(editor.hasCursor(), 'has cursor');
153158

154159
let textElement = editor.post.sections.head.markers.head.renderNode.element;
155160

@@ -169,13 +174,11 @@ test(`cmd-left goes to the beginning of a line (MacOS only)`, (assert) => {
169174

170175
test(`cmd-right goes to the end of a line (MacOS only)`, (assert) => {
171176
let initialText = 'something';
172-
const mobiledoc = Helpers.mobiledoc.build(
173-
({post, markupSection, marker}) => post([
174-
markupSection('p', [marker(initialText)])
175-
]));
177+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
178+
markupSection('p', [marker(initialText)])
179+
]));
176180

177-
editor = new Editor({mobiledoc});
178-
editor.render(editorElement);
181+
assert.ok(editor.hasCursor(), 'has cursor');
179182

180183
let textElement = editor.post.sections.head.markers.head.renderNode.element;
181184

@@ -195,13 +198,11 @@ test(`cmd-right goes to the end of a line (MacOS only)`, (assert) => {
195198

196199
test(`ctrl-k clears to the end of a line`, (assert) => {
197200
let initialText = 'something';
198-
const mobiledoc = Helpers.mobiledoc.build(
199-
({post, markupSection, marker}) => post([
200-
markupSection('p', [marker(initialText)])
201-
]));
201+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
202+
markupSection('p', [marker(initialText)])
203+
]));
202204

203-
editor = new Editor({mobiledoc});
204-
editor.render(editorElement);
205+
assert.ok(editor.hasCursor(), 'has cursor');
205206

206207
let textElement = editor.post.sections.head.markers.head.renderNode.element;
207208
Helpers.dom.moveCursorTo(editor, textElement, 4);
@@ -222,13 +223,11 @@ test(`ctrl-k clears to the end of a line`, (assert) => {
222223

223224
test(`ctrl-k clears selected text`, (assert) => {
224225
let initialText = 'something';
225-
const mobiledoc = Helpers.mobiledoc.build(
226-
({post, markupSection, marker}) => post([
227-
markupSection('p', [marker(initialText)])
228-
]));
226+
editor = renderIntoAndFocusTail( ({post, markupSection, marker}) => post([
227+
markupSection('p', [marker(initialText)])
228+
]));
229229

230-
editor = new Editor({mobiledoc});
231-
editor.render(editorElement);
230+
assert.ok(editor.hasCursor(), 'has cursor');
232231

233232
let textElement = editor.post.sections.head.markers.head.renderNode.element;
234233
Helpers.dom.moveCursorTo(editor, textElement, 4, textElement, 8);
@@ -248,15 +247,15 @@ test(`ctrl-k clears selected text`, (assert) => {
248247
});
249248

250249
test('cmd-k links selected text', (assert) => {
251-
assert.expect(2);
250+
assert.expect(3);
252251

253252
let url = 'http://bustle.com';
254-
let mobiledoc = Helpers.mobiledoc.build(
255-
({post, markupSection, marker}) => post([
256-
markupSection('p', [marker('something')])
257-
]));
258-
editor = new Editor({mobiledoc});
259-
editor.render(editorElement);
253+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
254+
markupSection('p', [marker('something')])
255+
]));
256+
257+
assert.ok(editor.hasCursor(), 'has cursor');
258+
260259
editor.showPrompt = (prompt, defaultUrl, callback) => {
261260
assert.ok(true, 'calls showPrompt');
262261
callback(url);
@@ -269,18 +268,18 @@ test('cmd-k links selected text', (assert) => {
269268
});
270269

271270
test('cmd-k unlinks selected text if it was already linked', (assert) => {
272-
assert.expect(3);
271+
assert.expect(4);
273272

274273
let url = 'http://bustle.com';
275-
let mobiledoc = Helpers.mobiledoc.build(
276-
({post, markupSection, marker, markup}) => post([
277-
markupSection('p', [marker('something', [markup('a', {href:url})])])
278-
]));
279-
editor = new Editor({mobiledoc});
274+
editor = renderIntoAndFocusTail(({post, markupSection, marker, markup}) => post([
275+
markupSection('p', [marker('something', [markup('a', {href:url})])])
276+
]));
277+
278+
assert.ok(editor.hasCursor(), 'has cursor');
279+
280280
editor.showPrompt = () => {
281281
assert.ok(false, 'should not call showPrompt');
282282
};
283-
editor.render(editorElement);
284283
assert.hasElement(`#editor a[href="${url}"]:contains(something)`,
285284
'precond -- has link');
286285

@@ -293,18 +292,17 @@ test('cmd-k unlinks selected text if it was already linked', (assert) => {
293292
});
294293

295294
test('new key commands can be registered', (assert) => {
296-
const mobiledoc = Helpers.mobiledoc.build(
297-
({post, markupSection, marker}) => post([
298-
markupSection('p', [marker('something')])
299-
]));
295+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
296+
markupSection('p', [marker('something')])
297+
]));
298+
299+
assert.ok(editor.hasCursor(), 'has cursor');
300300

301301
let passedEditor;
302-
editor = new Editor({mobiledoc});
303302
editor.registerKeyCommand({
304303
str: 'ctrl+x',
305304
run(editor) { passedEditor = editor; }
306305
});
307-
editor.render(editorElement);
308306

309307
Helpers.dom.triggerKeyCommand(editor, 'Y', MODIFIERS.CTRL);
310308

@@ -316,18 +314,17 @@ test('new key commands can be registered', (assert) => {
316314
});
317315

318316
test('new key commands can be registered without modifiers', (assert) => {
319-
const mobiledoc = Helpers.mobiledoc.build(
320-
({post, markupSection, marker}) => post([
321-
markupSection('p', [marker('something')])
322-
]));
317+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
318+
markupSection('p', [marker('something')])
319+
]));
320+
321+
assert.ok(editor.hasCursor(), 'has cursor');
323322

324323
let passedEditor;
325-
editor = new Editor({mobiledoc});
326324
editor.registerKeyCommand({
327325
str: 'X',
328326
run(editor) { passedEditor = editor; }
329327
});
330-
editor.render(editorElement);
331328

332329
Helpers.dom.triggerKeyCommand(editor, 'Y', MODIFIERS.CTRL);
333330

@@ -343,13 +340,14 @@ test('new key commands can be registered without modifiers', (assert) => {
343340
});
344341

345342
test('duplicate key commands can be registered with the last registered winning', (assert) => {
346-
const mobiledoc = Helpers.mobiledoc.build(
347-
({post, markupSection, marker}) => post([
348-
markupSection('p', [marker('something')])
349-
]));
343+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
344+
markupSection('p', [marker('something')])
345+
]));
346+
347+
assert.ok(editor.hasCursor(), 'has cursor');
350348

351349
let firstCommandRan, secondCommandRan;
352-
editor = new Editor({mobiledoc});
350+
353351
editor.registerKeyCommand({
354352
str: 'ctrl+x',
355353
run() { firstCommandRan = true; }
@@ -358,7 +356,6 @@ test('duplicate key commands can be registered with the last registered winning'
358356
str: 'ctrl+x',
359357
run() { secondCommandRan = true; }
360358
});
361-
editor.render(editorElement);
362359

363360
Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL);
364361

@@ -367,13 +364,14 @@ test('duplicate key commands can be registered with the last registered winning'
367364
});
368365

369366
test('returning false from key command causes next match to run', (assert) => {
370-
const mobiledoc = Helpers.mobiledoc.build(
371-
({post, markupSection, marker}) => post([
372-
markupSection('p', [marker('something')])
373-
]));
367+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
368+
markupSection('p', [marker('something')])
369+
]));
370+
371+
assert.ok(editor.hasCursor(), 'has cursor');
374372

375373
let firstCommandRan, secondCommandRan;
376-
editor = new Editor({mobiledoc});
374+
377375
editor.registerKeyCommand({
378376
str: 'ctrl+x',
379377
run() { firstCommandRan = true; }
@@ -385,7 +383,6 @@ test('returning false from key command causes next match to run', (assert) => {
385383
return false;
386384
}
387385
});
388-
editor.render(editorElement);
389386

390387
Helpers.dom.triggerKeyCommand(editor, 'X', MODIFIERS.CTRL);
391388

@@ -394,20 +391,18 @@ test('returning false from key command causes next match to run', (assert) => {
394391
});
395392

396393
test('key commands can override built-in functionality', (assert) => {
397-
const mobiledoc = Helpers.mobiledoc.build(
398-
({post, markupSection, marker}) => post([
399-
markupSection('p', [marker('something')])
400-
]));
394+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
395+
markupSection('p', [marker('something')])
396+
]));
401397

402-
editor = new Editor({mobiledoc});
398+
assert.ok(editor.hasCursor(), 'has cursor');
403399

404400
let passedEditor;
405401
editor.registerKeyCommand({
406402
str: 'enter',
407403
run(editor) { passedEditor = editor; }
408404
});
409405

410-
editor.render(editorElement);
411406
assert.equal($('#editor p').length, 1, 'has 1 paragraph to start');
412407

413408
Helpers.dom.moveCursorTo(editor, editorElement.childNodes[0].childNodes[0], 5);
@@ -419,12 +414,11 @@ test('key commands can override built-in functionality', (assert) => {
419414
});
420415

421416
test('returning false from key command still runs built-in functionality', (assert) => {
422-
const mobiledoc = Helpers.mobiledoc.build(
423-
({post, markupSection, marker}) => post([
424-
markupSection('p', [marker('something')])
425-
]));
417+
editor = renderIntoAndFocusTail(({post, markupSection, marker}) => post([
418+
markupSection('p', [marker('something')])
419+
]));
426420

427-
editor = new Editor({mobiledoc});
421+
assert.ok(editor.hasCursor(), 'has cursor');
428422

429423
let passedEditor;
430424
editor.registerKeyCommand({
@@ -435,7 +429,6 @@ test('returning false from key command still runs built-in functionality', (asse
435429
}
436430
});
437431

438-
editor.render(editorElement);
439432
assert.equal($('#editor p').length, 1, 'has 1 paragraph to start');
440433

441434
Helpers.dom.moveCursorTo(editor, editorElement.childNodes[0].childNodes[0], 5);

tests/acceptance/editor-sections-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Editor } from 'mobiledoc-kit';
22
import Helpers from '../test-helpers';
33
import { MOBILEDOC_VERSION } from 'mobiledoc-kit/renderers/mobiledoc/0-2';
44
import { NO_BREAK_SPACE } from 'mobiledoc-kit/renderers/editor-dom';
5+
import Range from 'mobiledoc-kit/utils/cursor/range';
56

67
const { test, module } = Helpers;
78

@@ -551,6 +552,12 @@ test('inserting multiple spaces renders them with nbsps', (assert) => {
551552
editor = new Editor({mobiledoc});
552553
editor.render(editorElement);
553554

555+
// Tests on FF fail if the editor doesn't have a cursor, we must
556+
// render it explicitly
557+
editor.selectRange(new Range(editor.post.tailPosition()));
558+
559+
assert.ok(editor.hasCursor(), 'precond - has cursor');
560+
554561
let sp = ' ', nbsp = NO_BREAK_SPACE;
555562
Helpers.dom.insertText(editor, sp + sp + sp);
556563
assert.equal($('#editor p:eq(0)').text(),

0 commit comments

Comments
 (0)