Skip to content

Commit

Permalink
move and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed May 12, 2016
1 parent 0c6e6a8 commit 7d5fa21
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion blots/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Scroll extends Parchment.Scroll {
}

leaf(index) {
return this.path(index).pop();
return this.path(index).pop() || [null, -1];
}

line(index) {
Expand Down
41 changes: 41 additions & 0 deletions test/unit/blots/scroll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Parchment from 'parchment';
import Emitter from '../../../core/emitter';
import Selection, { Range } from '../../../core/selection';
import Cursor from '../../../blots/cursor';
import Scroll from '../../../blots/scroll';


Expand Down Expand Up @@ -34,4 +36,43 @@ describe('Scroll', function() {
scroll.formatAt(6, 5, 'italic', true);
expect(scroll.domNode.firstChild).toEqualHTML('<strong>Hello</strong> World!');
});

describe('leaf()', function() {
it('text', function() {
let scroll = this.initialize(Scroll, '<p>Tests</p>');
let [leaf, offset] = scroll.leaf(2);
expect(leaf.value()).toEqual('Tests');
expect(offset).toEqual(2);
});

it('precise', function() {
let scroll = this.initialize(Scroll, '<p><u>0</u><s>1</s><u>2</u><s>3</s><u>4</u></p>');
let [leaf, offset] = scroll.leaf(3);
expect(leaf.value()).toEqual('2');
expect(offset).toEqual(1);
});

it('newline', function() {
let scroll = this.initialize(Scroll, '<p>0123</p><p>5678</p>');
let [leaf, offset] = scroll.leaf(4);
expect(leaf.value()).toEqual('0123');
expect(offset).toEqual(4);
});

it('cursor', function() {
let selection = this.initialize(Selection, '<p><u>0</u>1<u>2</u></p>');
selection.setRange(new Range(2));
selection.format('strike', true);
let [leaf, offset] = selection.scroll.leaf(2);
expect(leaf instanceof Cursor).toBe(true);
expect(offset).toEqual(0);
});

it('beyond document', function() {
let scroll = this.initialize(Scroll, '<p>Test</p>');
let [leaf, offset] = scroll.leaf(10);
expect(leaf).toEqual(null);
expect(offset).toEqual(-1);
});
});
});
40 changes: 1 addition & 39 deletions test/unit/core/selection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Delta from 'rich-text/lib/delta';
import Selection, { Range, findLeaf } from '../../../core/selection';
import Selection, { Range } from '../../../core/selection';
import Cursor from '../../../blots/cursor';
import Scroll from '../../../blots/scroll';

Expand All @@ -12,44 +12,6 @@ describe('Selection', function() {
};
});

describe('findLeaf', function() {
it('text', function() {
let scroll = this.initialize(Scroll, '<p>Tests</p>');
let [leaf, offset] = findLeaf(scroll, 2);
expect(leaf.value()).toEqual('Tests');
expect(offset).toEqual(2);
});

it('precise', function() {
let scroll = this.initialize(Scroll, '<p><u>0</u><s>1</s><u>2</u><s>3</s><u>4</u></p>');
let [leaf, offset] = findLeaf(scroll, 3);
expect(leaf.value()).toEqual('2');
expect(offset).toEqual(1);
});

it('newline', function() {
let scroll = this.initialize(Scroll, '<p>0123</p><p>5678</p>');
let [leaf, offset] = findLeaf(scroll, 4);
expect(leaf.value()).toEqual('0123');
expect(offset).toEqual(4);
});

it('cursor', function() {
this.setup('<p><u>0</u>1<u>2</u></p>', 2);
this.selection.format('strike', true);
let [leaf, offset] = findLeaf(this.selection.scroll, 2);
expect(leaf instanceof Cursor).toBe(true);
expect(offset).toEqual(0);
});

it('beyond document', function() {
let scroll = this.initialize(Scroll, '<p>Test</p>');
let [leaf, offset] = findLeaf(scroll, 10);
expect(leaf).toEqual(null);
expect(offset).toEqual(-1);
});
});

describe('focus()', function() {
beforeEach(function() {
this.initialize(HTMLElement, '<textarea>Test</textarea><div></div>');
Expand Down

0 comments on commit 7d5fa21

Please sign in to comment.