Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Changed: model.Document#event:change should have a batch as a param.
Browse files Browse the repository at this point in the history
Tests: Added missing `model.Document#event:change` tests.
  • Loading branch information
scofalik committed Jan 10, 2018
1 parent e9908d8 commit 0e83c83
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/model/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default class Document {
if ( !this.differ.isEmpty || hasSelectionChanged ) {
this._callPostFixers( writer );

this.fire( 'change' );
this.fire( 'change', writer.batch );

this.differ.reset();
hasSelectionChanged = false;
Expand Down
67 changes: 67 additions & 0 deletions tests/model/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Model from '../../src/model/model';
import Document from '../../src/model/document';
import RootElement from '../../src/model/rootelement';
import Text from '../../src/model/text';
import Batch from '../../src/model/batch';
import Delta from '../../src/model/delta/delta';
import Range from '../../src/model/range';
Expand Down Expand Up @@ -518,6 +519,72 @@ describe( 'Document', () => {
} );
} );

describe( 'event change', () => {
it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
doc.createRoot();
const spy = sinon.spy();

doc.on( 'change', ( evt, batch ) => {
spy();
expect( batch ).to.be.instanceof( Batch );
} );

model.change( writer => {
writer.insertText( 'foo', doc.getRoot(), 0 );
} );

expect( spy.calledOnce ).to.be.true;
} );

it( 'should be fired if there was a change in a document tree in a change block and have a batch as param', () => {
doc.createRoot();
const spy = sinon.spy();

doc.on( 'change', ( evt, batch ) => {
spy();
expect( batch ).to.be.instanceof( Batch );
} );

model.enqueueChange( writer => {
writer.insertText( 'foo', doc.getRoot(), 0 );
} );

expect( spy.calledOnce ).to.be.true;
} );

it( 'should be fired if there was a selection change in an (enqueue)change block', () => {
doc.createRoot();
const spy = sinon.spy();

const root = doc.getRoot();
root.appendChildren( new Text( 'foo' ) );

doc.on( 'change', spy );

model.change( () => {
doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 2, root, 2 ) ] );
} );

expect( spy.calledOnce ).to.be.true;
} );

it( 'should not be fired if writer was used on non-document tree', () => {
const spy = sinon.spy();

doc.on( 'change', ( evt, batch ) => {
spy();
expect( batch ).to.be.instanceof( Batch );
} );

model.change( writer => {
const docFrag = writer.createDocumentFragment();
writer.insertText( 'foo', docFrag, 0 );
} );

expect( spy.calledOnce ).to.be.false;
} );
} );

it( 'should be correctly converted to json', () => {
const serialized = jsonParseStringify( doc );

Expand Down

0 comments on commit 0e83c83

Please sign in to comment.