Skip to content

Commit

Permalink
basic test for pressing a letter in the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 6, 2015
1 parent e59eaf7 commit f1372c5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/js/editor/editor.js
Expand Up @@ -117,13 +117,14 @@ function bindLiveUpdate(editor) {
throw new Error('There is not section element for the previous edit');
}

var previousSectionElement;
var previousSectionElement, previousSection;
if (sectionElement && sectionElement.previousSibling) {
previousSectionElement = sectionElement.previousSibling;
previousSection = previousSectionElement.dataset.section;
}

var newSection = editor.compiler.parseSection(
previousSectionElement.dataset.section,
previousSection,
sectionElement.firstChild
);

Expand Down
45 changes: 45 additions & 0 deletions tests/acceptance/basic-editor-test.js
@@ -0,0 +1,45 @@
/* global QUnit */

import { Editor } from 'content-kit-editor';
import { moveCursorTo } from '../test-helpers';

const { test, module } = QUnit;

let fixture, editor, editorElement;

module('acceptance: basic editor', {
beforeEach() {
fixture = document.getElementById('qunit-fixture');
editorElement = document.createElement('div');
editorElement.setAttribute('id', 'editor');
fixture.appendChild(editorElement);
},
afterEach() {
}
});

test('sets element as contenteditable', (assert) => {
let innerHTML = `<p>Hello</p>`;
editorElement.innerHTML = innerHTML;
editor = new Editor('#editor');

assert.equal(editorElement.getAttribute('contenteditable'),
'true',
'element is contenteditable');
assert.equal(editorElement.firstChild.tagName, 'SECTION',
`editor element has a section as its first child`);
});

test('editing element changes editor post model', (assert) => {
let innerHTML = `<p>Hello</p>`;
editorElement.innerHTML = innerHTML;
editor = new Editor('#editor');

let p = editorElement.querySelector('p');
let textElement = p.firstChild;

moveCursorTo(textElement, 0);

document.execCommand('insertText', false, 'A');
assert.equal(p.textContent, 'AHello');
});
9 changes: 9 additions & 0 deletions tests/test-helpers.js
@@ -0,0 +1,9 @@
export function moveCursorTo(element, offset) {
let range = document.createRange();
range.setStart(element, offset);
range.setEnd(element, offset);

let selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
File renamed without changes.

0 comments on commit f1372c5

Please sign in to comment.