Skip to content

Commit

Permalink
add tests and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Apr 4, 2016
1 parent ee10ad2 commit 50442e2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
2 changes: 1 addition & 1 deletion core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Editor {

insertText(index, text, formats = {}, source = Emitter.sources.API) {
this.scroll.insertAt(index, text);
this.formatText(index, index + text.length, formats, source);
this.formatText(index, text.length, formats, source);
}

removeFormat(index, length) {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,16 @@ describe('Editor', function() {
});

describe('applyDelta', function() {
it('inserts', function() {
it('insert', function() {
let editor = this.initialize(Editor, '<p></p>');
editor.applyDelta(new Delta().insert('01'));
expect(this.container.innerHTML).toEqualHTML('<p>01</p>');
});

it('attributed insert', function() {
let editor = this.initialize(Editor, '<p></p>');
editor.applyDelta(new Delta().insert('01', { bold: true }));
expect(this.container.innerHTML).toEqualHTML('<p><strong>01</strong></p>');
let editor = this.initialize(Editor, '<p>0123</p>');
editor.applyDelta(new Delta().retain(2).insert('|', { bold: true }));
expect(this.container.innerHTML).toEqualHTML('<p>01<strong>|</strong>23</p>');
});

it('format', function() {
Expand Down
93 changes: 82 additions & 11 deletions test/unit/core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,104 @@ describe('Quill', function() {

describe('manipulation', function() {
beforeEach(function() {
this.quill = this.initialize(Quill, '<p>01234567</p>');
this.quill = this.initialize(Quill, '<p>0123<em>45</em>67</p>');
this.oldDelta = this.quill.getContents();
spyOn(this.quill.emitter, 'emit').and.callThrough();
});

it('deleteText()', function() {
this.quill.deleteText(1, 2);
expect(this.quill.root).toEqualHTML('<p>034567</p>');
let change = new Delta().retain(1).delete(2);
this.quill.deleteText(3, 2);
let change = new Delta().retain(3).delete(2);
expect(this.quill.root).toEqualHTML('<p>012<em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
});

it('format', function() {
this.quill.setSelection(2, 4);
this.quill.setSelection(3, 2);
this.quill.format('bold', true);
let change = new Delta().retain(2).retain(4, { bold: true });
expect(this.quill.root).toEqualHTML('<p>01<strong>2345</strong>67</p>');
let change = new Delta().retain(3).retain(2, { bold: true });
expect(this.quill.root).toEqualHTML('<p>012<strong>3<em>4</em></strong><em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
expect(this.quill.getSelection()).toEqual(new Range(3, 2));
});

it('formatLine()', function() {
this.quill.formatLine(1, 1, 'header', 2);
let change = new Delta().retain(8).retain(1, { header: 2});
expect(this.quill.root).toEqualHTML('<h2>0123<em>45</em>67</h2>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
});

it('formatText()', function() {
this.quill.formatText(3, 2, 'bold', true);
let change = new Delta().retain(3).retain(2, { bold: true });
expect(this.quill.root).toEqualHTML('<p>012<strong>3<em>4</em></strong><em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
expect(this.quill.getSelection()).toEqual(new Range(2, 4));
});

it('insertEmbed()', function() {
this.quill.insertEmbed(0, 'image', '/assets/favicon.png');
let change = new Delta().insert({ image: '/assets/favicon.png'});
expect(this.quill.root).toEqualHTML('<p><img src="/assets/favicon.png">01234567</p>');
this.quill.insertEmbed(5, 'image', '/assets/favicon.png');
let change = new Delta().retain(5).insert({ image: '/assets/favicon.png'}, { italic: true });
expect(this.quill.root).toEqualHTML('<p>0123<em>4<img src="/assets/favicon.png">5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
});

it('insertText()', function() {
this.quill.insertText(5, '|', 'bold', true);
let change = new Delta().retain(5).insert('|', { bold: true, italic: true });
expect(this.quill.root).toEqualHTML('<p>0123<em>4</em><strong><em>|</em></strong><em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
});

it('enable/disable', function() {
this.quill.disable();
expect(this.quill.root.getAttribute('contenteditable')).toEqual('false');
this.quill.enable();
expect(this.quill.root.getAttribute('contenteditable')).toBeTruthy();
});

it('getBounds() index', function() {
let bounds = this.quill.selection.getBounds(1);
expect(this.quill.getBounds(1)).toEqual(bounds);
});

it('getBounds() range', function() {
let bounds = this.quill.selection.getBounds(3, 4);
expect(this.quill.getBounds(new Range(3, 4))).toEqual(bounds);
});

it('getFormat()', function() {
let formats = this.quill.getFormat(5);
expect(formats).toEqual({ italic: true });
});

it('getSelection()', function() {
expect(this.quill.getSelection()).toEqual(null);
let range = new Range(1, 2);
this.quill.setSelection(range);
expect(this.quill.getSelection()).toEqual(range);
});

it('removeFormat()', function() {
this.quill.removeFormat(5, 1);
let change = new Delta().retain(5).retain(1, { italic: null });
expect(this.quill.root).toEqualHTML('<p>0123<em>4</em>567</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, change, this.oldDelta, Emitter.sources.API);
});

it('updateContents() delta', function() {
let delta = new Delta().retain(5).insert('|');
this.quill.updateContents(delta);
expect(this.quill.root).toEqualHTML('<p>0123<em>4</em>|<em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, this.oldDelta, Emitter.sources.API);
});

it('updateContents() ops array', function() {
let delta = new Delta().retain(5).insert('|');
this.quill.updateContents(delta.ops);
expect(this.quill.root).toEqualHTML('<p>0123<em>4</em>|<em>5</em>67</p>');
expect(this.quill.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, this.oldDelta, Emitter.sources.API);
});
});

describe('setContents()', function() {
Expand Down

0 comments on commit 50442e2

Please sign in to comment.