/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import Model from '../../src/model/model'; import Batch from '../../src/model/batch'; 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 enqueueChanges immediately if its the first block', () => { model.enqueueChange( new Batch(), () => { 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 enqueueChanges immediately if its the first block', () => { model.enqueueChange( new Batch(), () => { changes += 'A'; nested(); } ); expect( changes ).to.equal( 'AB' ); function nested() { const ret = model.change( () => { changes += 'B'; } ); } } ); it( 'should be possible to nest change in enqueueChanges', () => { model.enqueueChange( new Batch(), () => { 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 enqueueChanges in enqueueChanges', () => { model.enqueueChange( new Batch(), () => { changes += 'A'; nestedEnqueue(); changes += 'B'; } ); expect( changes ).to.equal( 'ABC' ); function nestedEnqueue() { model.enqueueChange( new Batch(), () => { changes += 'C'; } ); } } ); it( 'should be possible to nest enqueueChanges in changes', () => { const ret = model.change( () => { changes += 'A'; nestedEnqueue(); changes += 'B'; return 'D'; } ); changes += ret; expect( changes ).to.equal( 'ABCD' ); function nestedEnqueue() { model.enqueueChange( new Batch(), () => { changes += 'C'; } ); } } ); } ); } );