Skip to content

Commit

Permalink
refactor(tests): Refactor deletion tests to be terser (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic authored Aug 25, 2016
1 parent 919600c commit eeb9e19
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 299 deletions.
11 changes: 6 additions & 5 deletions tests/helpers/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import PostAbstractHelpers from './post-abstract';
import Editor from 'mobiledoc-kit/editor/editor';
import MobiledocRenderer from 'mobiledoc-kit/renderers/mobiledoc/0-3';

function retargetPosition(position, fromPost, toPost) {
function retargetPosition(position, toPost) {
let fromPost = position.section.post;
let sectionIndex;
let retargetedPosition;
fromPost.walkAllLeafSections((section,index) => {
Expand All @@ -25,9 +26,8 @@ function retargetPosition(position, fromPost, toPost) {
}

function retargetRange(range, toPost) {
let fromPost = range.head.section.post;
let newHead = retargetPosition(range.head, fromPost, toPost);
let newTail = retargetPosition(range.tail, fromPost, toPost);
let newHead = retargetPosition(range.head, toPost);
let newTail = retargetPosition(range.tail, toPost);

return newHead.toRange(newTail);
}
Expand All @@ -50,5 +50,6 @@ function buildFromText(texts, editorOptions={}) {

export {
buildFromText,
retargetRange
retargetRange,
retargetPosition
};
165 changes: 42 additions & 123 deletions tests/unit/editor/post-delete-at-position-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const { module, test } = Helpers;

let { postEditor: { run } } = Helpers;
let { postAbstract: { buildFromText } } = Helpers;
const { editor: { retargetPosition } } = Helpers;

module('Unit: PostEditor: #deleteAtPosition');

test('single markup section (backward)', (assert) => {
let examples = [
let expectationGroups = [{
name: 'single markup section',
direction: BACKWARD,
expectations: [
['abc|def', 'ab|def', 'middle'],
['abcdef|', 'abcde|', 'end'],
['|abcdef', '|abcdef', 'start'],
Expand All @@ -21,23 +24,11 @@ test('single markup section (backward)', (assert) => {

['ab @| ef', 'ab | ef', 'atom (right side)'],
['ab |@ ef', 'ab|@ ef', 'atom (left side)']
];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, BACKWARD));

expectedPosition = post.sections.head.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});

test('single markup section (forward)', (assert) => {
let examples = [
]
}, {
name: 'single markup section',
direction: FORWARD,
expectations: [
['abc|def', 'abc|ef', 'middle'],
['abcdef|', 'abcdef|', 'end'],
['|abcdef', '|bcdef', 'start'],
Expand All @@ -47,129 +38,57 @@ test('single markup section (forward)', (assert) => {

['ab @| ef', 'ab @|ef', 'atom (right side)'],
['ab |@ ef', 'ab | ef', 'atom (left side)']
];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, FORWARD));

expectedPosition = post.sections.head.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});

test('across section boundary (backward)', (assert) => {
let examples = [
]
}, {
name: 'across section boundary',
direction: BACKWARD,
expectations: [
[['abc','|def'], 'abc|def', 'markup sections'],
[['*abc*','|def'], '*abc*|def', 'markup sections with markup'],
[['[abc]','|def'], ['[abc]|','def'], 'prev section is card'],
[['abc','|[def]'], ['abc|','[def]'], 'cur section is card'],
[['', '|abc'], ['|abc'], 'prev section is blank']
];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, BACKWARD));

expectedPosition = post.sections.head.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});

test('across section boundary (forward)', (assert) => {
let examples = [
]
}, {
name: 'across section boundary',
direction: FORWARD,
expectations: [
[['abc|','def'], 'abc|def', 'markup sections'],
[['*abc*|','def'], '*abc*|def', 'markup sections with markup'],
[['[abc]|','def'], ['[abc]|','def'], 'cur section is card'],
[['abc|','[def]'], ['abc|','[def]'], 'next section is card'],
[['abc|', ''], ['abc|'], 'next section is blank']
];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, FORWARD));

expectedPosition = post.sections.head.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});

test('across list item boundary (backward)', (assert) => {
let examples = [
]
}, {
name: 'across list item boundary',
direction: BACKWARD,
expectations: [
[['* abc','* |def'], ['* abc', '|def'], 'start of list item'],
[['* abc','|def'], ['* abc|def'], 'into list item'],
[['', '* |abc'], ['', '|abc'], 'prev blank section'],
];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, BACKWARD));

// FIXME need to figure out how to say which section to expect the range to include
let sectionIndex;
let index = 0;
expectedPost.walkAllLeafSections(section => {
if (section === expectedPosition.section) { sectionIndex = index; }
index++;
});

let section;
index = 0;
post.walkAllLeafSections(_section => {
if (index === sectionIndex) { section = _section; }
index++;
});
expectedPosition = section.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});

test('across list item boundary (forward)', (assert) => {
let examples = [
]
}, {
name: 'across list item boundary',
direction: FORWARD,
expectations: [
[['* abc|','* def'], ['* abc|def'], 'item into item'],
[['* abc|','def'], ['* abc|def'], 'item into markup'],
[['abc|','* def'], ['abc|def'], 'markup into markup'],
];
]
}];

examples.forEach(([before, after, msg]) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);
expectationGroups.forEach(({name, expectations, direction}) => {
expectations.forEach(([before, after, msg]) => {
let dir = direction === FORWARD ? 'forward' : 'backward';
test(`${dir}: ${name}, "${before}" -> "${after}" (${msg})`, (assert) => {
let { post, range: { head: position } } = buildFromText(before);
let { post: expectedPost, range: { head: expectedPosition } } = buildFromText(after);

position = run(post, postEditor => postEditor.deleteAtPosition(position, FORWARD));
position = run(post, postEditor => postEditor.deleteAtPosition(position, direction));
expectedPosition = retargetPosition(expectedPosition, post);

// FIXME need to figure out how to say which section to expect the range to include
let sectionIndex;
let index = 0;
expectedPost.walkAllLeafSections(section => {
if (section === expectedPosition.section) { sectionIndex = index; }
index++;
assert.postIsSimilar(post, expectedPost);
assert.positionIsEqual(position, expectedPosition);
});

let section;
index = 0;
post.walkAllLeafSections(_section => {
if (index === sectionIndex) { section = _section; }
index++;
});
expectedPosition = section.toPosition(expectedPosition.offset);

assert.postIsSimilar(post, expectedPost, `post ${msg}`);
assert.positionIsEqual(position, expectedPosition, `position ${msg}`);
});
});
Loading

0 comments on commit eeb9e19

Please sign in to comment.