| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Model from '../../src/model/model';
- import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
- describe( 'Model', () => {
- let model;
- let changes = '';
- beforeEach( () => {
- model = new Model();
- changes = '';
- } );
- describe( 'change & enqueueChange', () => {
- it( 'should execute changes immediately', () => {
- model.change( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'A' );
- } );
- it( 'should pass returned value', () => {
- const ret = model.change( () => {
- changes += 'A';
- return 'B';
- } );
- changes += ret;
- expect( changes ).to.equal( 'AB' );
- } );
- it( 'should not mixed the order when nested change is called', () => {
- const ret = model.change( () => {
- changes += 'A';
- nested();
- return 'D';
- } );
- changes += ret;
- expect( changes ).to.equal( 'ABCD' );
- function nested() {
- const ret = model.change( () => {
- changes += 'B';
- return 'C';
- } );
- changes += ret;
- }
- } );
- it( 'should execute enqueueChange immediately if its the first block', () => {
- model.enqueueChange( () => {
- changes += 'A';
- nested();
- } );
- expect( changes ).to.equal( 'ABC' );
- function nested() {
- const ret = model.change( () => {
- changes += 'B';
- return 'C';
- } );
- changes += ret;
- }
- } );
- it( 'should be possible to enqueueChange immediately if its the first block', () => {
- model.enqueueChange( () => {
- changes += 'A';
- nested();
- } );
- expect( changes ).to.equal( 'AB' );
- function nested() {
- model.change( () => {
- changes += 'B';
- } );
- }
- } );
- it( 'should be possible to nest change in enqueueChange', () => {
- model.enqueueChange( () => {
- changes += 'A';
- nested();
- changes += 'D';
- } );
- expect( changes ).to.equal( 'ABCD' );
- function nested() {
- const ret = model.change( () => {
- changes += 'B';
- return 'C';
- } );
- changes += ret;
- }
- } );
- it( 'should be possible to nest enqueueChange in enqueueChange', () => {
- model.enqueueChange( () => {
- changes += 'A';
- nestedEnqueue();
- changes += 'B';
- } );
- expect( changes ).to.equal( 'ABC' );
- function nestedEnqueue() {
- model.enqueueChange( () => {
- changes += 'C';
- } );
- }
- } );
- it( 'should be possible to nest enqueueChange in changes', () => {
- const ret = model.change( () => {
- changes += 'A';
- nestedEnqueue();
- changes += 'B';
- return 'D';
- } );
- changes += ret;
- expect( changes ).to.equal( 'ABCD' );
- function nestedEnqueue() {
- model.enqueueChange( () => {
- changes += 'C';
- } );
- }
- } );
- it( 'should be possible to nest enqueueChange in enqueueChange event', () => {
- model.once( 'change', () => {
- model.enqueueChange( () => {
- changes += 'C';
- } );
- changes += 'B';
- } );
- model.on( 'changesDone', () => {
- changes += 'D';
- } );
- model.enqueueChange( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'ABCD' );
- } );
- it( 'should be possible to nest enqueueChange in changes event', () => {
- model.once( 'change', () => {
- model.enqueueChange( () => {
- changes += 'C';
- } );
- changes += 'B';
- } );
- model.on( 'changesDone', () => {
- changes += 'D';
- } );
- model.change( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'ABCD' );
- } );
- it( 'should be possible to nest changes in enqueueChange event', () => {
- model.once( 'change', () => {
- model.change( () => {
- changes += 'B';
- } );
- changes += 'C';
- } );
- model.on( 'changesDone', () => {
- changes += 'D';
- } );
- model.enqueueChange( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'ABCD' );
- } );
- it( 'should be possible to nest changes in changes event', () => {
- model.once( 'change', () => {
- model.change( () => {
- changes += 'B';
- } );
- changes += 'C';
- } );
- model.on( 'changesDone', () => {
- changes += 'D';
- } );
- model.change( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'ABCD' );
- } );
- it( 'should let mix blocks', () => {
- model.once( 'change', () => {
- model.change( () => {
- changes += 'B';
- nestedEnqueue();
- } );
- model.change( () => {
- changes += 'C';
- } );
- changes += 'D';
- } );
- model.on( 'changesDone', () => {
- changes += 'F';
- } );
- model.change( () => {
- changes += 'A';
- } );
- expect( changes ).to.equal( 'ABCDEF' );
- function nestedEnqueue() {
- model.enqueueChange( () => {
- changes += 'E';
- } );
- }
- } );
- it( 'should use the same writer in all change blocks (change & change)', () => {
- model.change( outerWriter => {
- model.change( innerWriter => {
- expect( innerWriter ).to.equal( outerWriter );
- } );
- } );
- } );
- it( 'should create new writer in enqueue block', () => {
- model.change( outerWriter => {
- model.enqueueChange( innerWriter => {
- expect( innerWriter ).to.not.equal( outerWriter );
- expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
- } );
- } );
- } );
- it( 'should let you pass batch', () => {
- let outerBatch;
- model.change( outerWriter => {
- outerBatch = outerWriter.batch;
- model.enqueueChange( outerBatch, innerWriter => {
- expect( innerWriter.batch ).to.equal( outerBatch );
- } );
- } );
- } );
- it( 'should let you create transparent batch', () => {
- model.enqueueChange( 'transparent', writer => {
- expect( writer.batch.type ).to.equal( 'transparent' );
- } );
- } );
- } );
- describe( 'applyOperation', () => {
- it( 'should execute provided operation end return the result of operation', () => {
- const returnValue = { foo: 'bar' };
- const operation = {
- _execute: sinon.stub().returns( returnValue )
- };
- model.applyOperation( operation );
- sinon.assert.calledOnce( operation._execute );
- expect( model.applyOperation( operation ) ).to.equal( returnValue );
- } );
- } );
- describe( 'destroy()', () => {
- it( 'should destroy document', () => {
- sinon.spy( model.document, 'destroy' );
- model.destroy();
- sinon.assert.calledOnce( model.document.destroy );
- } );
- it( 'should stop listening', () => {
- const emitter = Object.create( EmitterMixin );
- const spy = sinon.spy();
- model.listenTo( emitter, 'event', spy );
- model.destroy();
- emitter.fire( 'event' );
- sinon.assert.notCalled( spy );
- } );
- } );
- } );
|