| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import testUtils from '/tests/core/_utils/utils.js';
- import utilsTestUtils from '/tests/utils/_utils/utils.js';
- import ObservableMixin from '/ckeditor5/utils/observablemixin.js';
- import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
- import EventInfo from '/ckeditor5/utils/eventinfo.js';
- import mix from '/ckeditor5/utils/mix.js';
- const assertBinding = utilsTestUtils.assertBinding;
- testUtils.createSinonSandbox();
- describe( 'ObservableMixin', () => {
- it( 'exists', () => {
- expect( ObservableMixin ).to.be.an( 'object' );
- } );
- it( 'mixes in EmitterMixin', () => {
- expect( ObservableMixin ).to.have.property( 'on', EmitterMixin.on );
- } );
- it( 'implements set, bind, and unbind methods', () => {
- expect( ObservableMixin ).to.contain.keys( 'set', 'bind', 'unbind' );
- } );
- } );
- describe( 'Observable', () => {
- class Observable {
- constructor( attrs ) {
- if ( attrs ) {
- this.set( attrs );
- }
- }
- }
- mix( Observable, ObservableMixin );
- let Car, car;
- beforeEach( () => {
- Car = class extends Observable {};
- car = new Car( {
- color: 'red',
- year: 2015
- } );
- } );
- it( 'should set attributes on creation', () => {
- expect( car ).to.have.property( 'color', 'red' );
- expect( car ).to.have.property( 'year', 2015 );
- } );
- it( 'should get correctly after set', () => {
- car.color = 'blue';
- expect( car.color ).to.equal( 'blue' );
- } );
- describe( 'set', () => {
- it( 'should work when passing an object', () => {
- car.set( {
- color: 'blue', // Override
- wheels: 4,
- seats: 5
- } );
- expect( car ).to.deep.equal( {
- color: 'blue',
- year: 2015,
- wheels: 4,
- seats: 5
- } );
- } );
- it( 'should work when passing a key/value pair', () => {
- car.set( 'color', 'blue' );
- car.set( 'wheels', 4 );
- expect( car ).to.deep.equal( {
- color: 'blue',
- year: 2015,
- wheels: 4
- } );
- } );
- it( 'should fire the "change" event', () => {
- let spy = sinon.spy();
- let spyColor = sinon.spy();
- let spyYear = sinon.spy();
- let spyWheels = sinon.spy();
- car.on( 'change', spy );
- car.on( 'change:color', spyColor );
- car.on( 'change:year', spyYear );
- car.on( 'change:wheels', spyWheels );
- // Set property in all possible ways.
- car.color = 'blue';
- car.set( { year: 2003 } );
- car.set( 'wheels', 4 );
- // Check number of calls.
- sinon.assert.calledThrice( spy );
- sinon.assert.calledOnce( spyColor );
- sinon.assert.calledOnce( spyYear );
- sinon.assert.calledOnce( spyWheels );
- // Check context.
- sinon.assert.alwaysCalledOn( spy, car );
- sinon.assert.calledOn( spyColor, car );
- sinon.assert.calledOn( spyYear, car );
- sinon.assert.calledOn( spyWheels, car );
- // Check params.
- sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'color', 'blue', 'red' );
- sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'year', 2003, 2015 );
- sinon.assert.calledWithExactly( spy, sinon.match.instanceOf( EventInfo ), 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
- sinon.assert.calledWithExactly( spyColor, sinon.match.instanceOf( EventInfo ), 'color', 'blue', 'red' );
- sinon.assert.calledWithExactly( spyYear, sinon.match.instanceOf( EventInfo ), 'year', 2003, 2015 );
- sinon.assert.calledWithExactly( spyWheels, sinon.match.instanceOf( EventInfo ), 'wheels', 4, sinon.match.typeOf( 'undefined' ) );
- } );
- it( 'should not fire the "change" event for the same attribute value', () => {
- let spy = sinon.spy();
- let spyColor = sinon.spy();
- car.on( 'change', spy );
- car.on( 'change:color', spyColor );
- // Set the "color" property in all possible ways.
- car.color = 'red';
- car.set( 'color', 'red' );
- car.set( { color: 'red' } );
- sinon.assert.notCalled( spy );
- sinon.assert.notCalled( spyColor );
- } );
- it( 'should throw when overriding already existing property', () => {
- car.normalProperty = 1;
- expect( () => {
- car.set( 'normalProperty', 2 );
- } ).to.throwCKEditorError( /^observable-set-cannot-override/ );
- expect( car ).to.have.property( 'normalProperty', 1 );
- } );
- it( 'should throw when overriding already existing property (in the prototype)', () => {
- class Car extends Observable {
- method() {}
- }
- car = new Car();
- expect( () => {
- car.set( 'method', 2 );
- } ).to.throwCKEditorError( /^observable-set-cannot-override/ );
- expect( car.method ).to.be.a( 'function' );
- } );
- it( 'should allow setting attributes with undefined value', () => {
- let spy = sinon.spy();
- car.on( 'change', spy );
- car.set( 'seats', undefined );
- sinon.assert.calledOnce( spy );
- expect( car ).to.contain.keys( 'seats' );
- expect( car.seats ).to.be.undefined;
- car.set( 'seats', 5 );
- sinon.assert.calledTwice( spy );
- expect( car ).to.have.property( 'seats', 5 );
- } );
- } );
- describe( 'bind', () => {
- it( 'should chain for a single attribute', () => {
- expect( car.bind( 'color' ) ).to.contain.keys( 'to' );
- } );
- it( 'should chain for multiple attributes', () => {
- expect( car.bind( 'color', 'year' ) ).to.contain.keys( 'to' );
- } );
- it( 'should chain for nonexistent attributes', () => {
- expect( car.bind( 'nonexistent' ) ).to.contain.keys( 'to' );
- } );
- it( 'should throw when attributes are not strings', () => {
- expect( () => {
- car.bind();
- } ).to.throwCKEditorError( /observable-bind-wrong-attrs/ );
- expect( () => {
- car.bind( new Date() );
- } ).to.throwCKEditorError( /observable-bind-wrong-attrs/ );
- expect( () => {
- car.bind( 'color', new Date() );
- } ).to.throwCKEditorError( /observable-bind-wrong-attrs/ );
- } );
- it( 'should throw when the same attribute is used than once', () => {
- expect( () => {
- car.bind( 'color', 'color' );
- } ).to.throwCKEditorError( /observable-bind-duplicate-attrs/ );
- } );
- it( 'should throw when binding the same attribute more than once', () => {
- expect( () => {
- car.bind( 'color' );
- car.bind( 'color' );
- } ).to.throwCKEditorError( /observable-bind-rebind/ );
- } );
- describe( 'to', () => {
- it( 'should not chain', () => {
- expect(
- car.bind( 'color' ).to( new Observable( { color: 'red' } ) )
- ).to.be.undefined;
- } );
- it( 'should throw when arguments are of invalid type - empty', () => {
- expect( () => {
- car = new Car();
- car.bind( 'color' ).to();
- } ).to.throwCKEditorError( /observable-bind-to-parse-error/ );
- } );
- it( 'should throw when binding multiple attributes to multiple observables', () => {
- let vehicle = new Car();
- const car1 = new Car( { color: 'red', year: 1943 } );
- const car2 = new Car( { color: 'yellow', year: 1932 } );
- expect( () => {
- vehicle.bind( 'color', 'year' ).to( car1, 'color', car2, 'year' );
- } ).to.throwCKEditorError( /observable-bind-to-no-callback/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car1, car2 );
- } ).to.throwCKEditorError( /observable-bind-to-no-callback/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car1, car2, 'year' );
- } ).to.throwCKEditorError( /observable-bind-to-no-callback/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car1, 'color', car2 );
- } ).to.throwCKEditorError( /observable-bind-to-no-callback/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year', 'custom' ).to( car, car );
- } ).to.throwCKEditorError( /observable-bind-to-no-callback/ );
- } );
- it( 'should throw when binding multiple attributes but passed a callback', () => {
- let vehicle = new Car();
- expect( () => {
- vehicle.bind( 'color', 'year' ).to( car, () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-extra-callback/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car, car, () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-extra-callback/ );
- } );
- it( 'should throw when binding a single attribute but multiple callbacks', () => {
- let vehicle = new Car();
- expect( () => {
- vehicle.bind( 'color' ).to( car, () => {}, () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-parse-error/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color' ).to( car, car, () => {}, () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-parse-error/ );
- } );
- it( 'should throw when a number of attributes does not match', () => {
- let vehicle = new Car();
- expect( () => {
- vehicle.bind( 'color' ).to( car, 'color', 'year' );
- } ).to.throwCKEditorError( /observable-bind-to-attrs-length/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car, 'color' );
- } ).to.throwCKEditorError( /observable-bind-to-attrs-length/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'color', 'year', () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-attrs-length/ );
- expect( () => {
- vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'color', car, 'color', 'year', () => {} );
- } ).to.throwCKEditorError( /observable-bind-to-attrs-length/ );
- } );
- it( 'should work when attributes don\'t exist in to() observable #1', () => {
- const vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'nonexistent in car' );
- assertBinding( vehicle,
- { color: undefined, year: undefined },
- [
- [ car, { 'nonexistent in car': 'foo', year: 1969 } ]
- ],
- { color: 'foo', year: undefined }
- );
- } );
- it( 'should work when attributes don\'t exist in to() observable #2', () => {
- const vehicle = new Car();
- vehicle.bind( 'nonexistent in car' ).to( car );
- assertBinding( vehicle,
- { 'nonexistent in car': undefined, year: undefined },
- [
- [ car, { 'nonexistent in car': 'foo', year: 1969 } ]
- ],
- { 'nonexistent in car': 'foo', year: undefined }
- );
- } );
- it( 'should work when attributes don\'t exist in to() observable #3', () => {
- const vehicle = new Car();
- vehicle.bind( 'year' ).to( car, 'color', car, 'nonexistent in car', ( a, b ) => a + b );
- assertBinding( vehicle,
- { color: undefined, year: car.color + undefined },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: undefined, year: 'blueundefined' }
- );
- } );
- it( 'should set new observable attributes', () => {
- const car = new Car( { color: 'green', year: 2001, type: 'pickup' } );
- const vehicle = new Car( { 'not involved': true } );
- vehicle.bind( 'color', 'year', 'type' ).to( car );
- expect( vehicle ).to.have.property( 'color' );
- expect( vehicle ).to.have.property( 'year' );
- expect( vehicle ).to.have.property( 'type' );
- expect( vehicle ).to.have.property( 'not involved' );
- } );
- it( 'should work when no attribute specified #1', () => {
- const vehicle = new Car();
- vehicle.bind( 'color' ).to( car );
- assertBinding( vehicle,
- { color: car.color, year: undefined },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 'blue', year: undefined }
- );
- } );
- it( 'should work for a single attribute', () => {
- const vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'color' );
- assertBinding( vehicle,
- { color: car.color, year: undefined },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 'blue', year: undefined }
- );
- } );
- it( 'should work for multiple attributes', () => {
- const vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
- assertBinding( vehicle,
- { color: car.color, year: car.year },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 'blue', year: 1969 }
- );
- } );
- it( 'should work for attributes that don\'t exist in the observable', () => {
- const vehicle = new Car();
- vehicle.bind( 'nonexistent in vehicle' ).to( car, 'color' );
- assertBinding( vehicle,
- { 'nonexistent in vehicle': car.color, color: undefined },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { 'nonexistent in vehicle': 'blue', color: undefined }
- );
- } );
- it( 'should work when using the same attribute name more than once', () => {
- const vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car, 'year', 'year' );
- assertBinding( vehicle,
- { color: car.year, year: car.year },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 1969, year: 1969 }
- );
- } );
- it( 'should work when binding more that once', () => {
- const vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'color' );
- vehicle.bind( 'year' ).to( car, 'year' );
- assertBinding( vehicle,
- { color: car.color, year: car.year },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 'blue', year: 1969 }
- );
- } );
- it( 'should work with callback – set a new observable attribute', () => {
- const vehicle = new Car();
- const car1 = new Car( { type: 'pickup' } );
- const car2 = new Car( { type: 'truck' } );
- vehicle.bind( 'type' )
- .to( car1, car2, ( ...args ) => args.join( '' ) );
- expect( vehicle ).to.have.property( 'type' );
- } );
- it( 'should work with callback #1', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'black' } );
- const car2 = new Car( { color: 'brown' } );
- vehicle.bind( 'color' )
- .to( car1, car2, ( ...args ) => args.join( '' ) );
- assertBinding( vehicle,
- { color: car1.color + car2.color, year: undefined },
- [
- [ car1, { color: 'black', year: 1930 } ],
- [ car2, { color: 'green', year: 1950 } ]
- ],
- { color: 'blackgreen', year: undefined }
- );
- } );
- it( 'should work with callback #2', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'black' } );
- const car2 = new Car( { color: 'brown' } );
- vehicle.bind( 'color' )
- .to( car1, 'color', car2, 'color', ( ...args ) => args.join( '' ) );
- assertBinding( vehicle,
- { color: car1.color + car2.color, year: undefined },
- [
- [ car1, { color: 'black', year: 1930 } ],
- [ car2, { color: 'green', year: 1950 } ]
- ],
- { color: 'blackgreen', year: undefined }
- );
- } );
- it( 'should work with callback #3', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'black' } );
- const car2 = new Car( { color: 'brown' } );
- const car3 = new Car( { color: 'yellow' } );
- vehicle.bind( 'color' )
- .to( car1, car2, car3, ( ...args ) => args.join( '' ) );
- assertBinding( vehicle,
- { color: car1.color + car2.color + car3.color, year: undefined },
- [
- [ car1, { color: 'black', year: 1930 } ],
- [ car2, { color: 'green', year: 1950 } ]
- ],
- { color: 'blackgreenyellow', year: undefined }
- );
- } );
- it( 'should work with callback #4', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'black' } );
- const car2 = new Car( { lightness: 'bright' } );
- const car3 = new Car( { color: 'yellow' } );
- vehicle.bind( 'color' )
- .to( car1, car2, 'lightness', car3, ( ...args ) => args.join( '' ) );
- assertBinding( vehicle,
- { color: car1.color + car2.lightness + car3.color, year: undefined },
- [
- [ car1, { color: 'black', year: 1930 } ],
- [ car2, { color: 'green', year: 1950 } ]
- ],
- { color: 'blackbrightyellow', year: undefined }
- );
- } );
- it( 'should work with callback #5', () => {
- const vehicle = new Car();
- const car1 = new Car( { hue: 'reds' } );
- const car2 = new Car( { lightness: 'bright' } );
- vehicle.bind( 'color' )
- .to( car1, 'hue', car2, 'lightness', ( ...args ) => args.join( '' ) );
- assertBinding( vehicle,
- { color: car1.hue + car2.lightness, year: undefined },
- [
- [ car1, { hue: 'greens', year: 1930 } ],
- [ car2, { lightness: 'dark', year: 1950 } ]
- ],
- { color: 'greensdark', year: undefined }
- );
- } );
- it( 'should work with callback #6', () => {
- const vehicle = new Car();
- const car1 = new Car( { hue: 'reds' } );
- vehicle.bind( 'color' )
- .to( car1, 'hue', ( h ) => h.toUpperCase() );
- assertBinding( vehicle,
- { color: car1.hue.toUpperCase(), year: undefined },
- [
- [ car1, { hue: 'greens', year: 1930 } ]
- ],
- { color: 'GREENS', year: undefined }
- );
- } );
- it( 'should work with callback #7', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'red', year: 1943 } );
- const car2 = new Car( { color: 'yellow', year: 1932 } );
- vehicle.bind( 'custom' )
- .to( car1, 'color', car2, 'year', ( ...args ) => args.join( '/' ) );
- assertBinding( vehicle,
- { color: undefined, year: undefined, 'custom': car1.color + '/' + car2.year },
- [
- [ car1, { color: 'blue', year: 2100 } ],
- [ car2, { color: 'violet', year: 1969 } ]
- ],
- { color: undefined, year: undefined, 'custom': 'blue/1969' }
- );
- } );
- it( 'should work with callback #8', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'red', year: 1943 } );
- const car2 = new Car( { color: 'yellow', year: 1932 } );
- const car3 = new Car( { hue: 'reds' } );
- vehicle.bind( 'custom' )
- .to( car1, 'color', car2, 'year', car3, 'hue', ( ...args ) => args.join( '/' ) );
- assertBinding( vehicle,
- { color: undefined, year: undefined, hue: undefined, 'custom': car1.color + '/' + car2.year + '/' + car3.hue },
- [
- [ car1, { color: 'blue', year: 2100 } ],
- [ car2, { color: 'violet', year: 1969 } ]
- ],
- { color: undefined, year: undefined, hue: undefined, 'custom': 'blue/1969/reds' }
- );
- } );
- it( 'should work with callback – binding more that once #1', () => {
- const vehicle = new Car();
- const car1 = new Car( { hue: 'reds', produced: 1920 } );
- const car2 = new Car( { lightness: 'bright', sold: 1921 } );
- vehicle.bind( 'color' )
- .to( car1, 'hue', car2, 'lightness', ( ...args ) => args.join( '' ) );
- vehicle.bind( 'year' )
- .to( car1, 'produced', car2, 'sold', ( ...args ) => args.join( '/' ) );
- assertBinding( vehicle,
- { color: car1.hue + car2.lightness, year: car1.produced + '/' + car2.sold },
- [
- [ car1, { hue: 'greens', produced: 1930 } ],
- [ car2, { lightness: 'dark', sold: 2000 } ]
- ],
- { color: 'greensdark', year: '1930/2000' }
- );
- } );
- it( 'should work with callback – binding more that once #2', () => {
- const vehicle = new Car();
- const car1 = new Car( { hue: 'reds', produced: 1920 } );
- const car2 = new Car( { lightness: 'bright', sold: 1921 } );
- vehicle.bind( 'color' )
- .to( car1, 'hue', car2, 'lightness', ( ...args ) => args.join( '' ) );
- vehicle.bind( 'year' )
- .to( car1, 'produced', car2, 'sold', ( ...args ) => args.join( '/' ) );
- vehicle.bind( 'mix' )
- .to( car1, 'hue', car2, 'sold', ( ...args ) => args.join( '+' ) );
- assertBinding( vehicle,
- {
- color: car1.hue + car2.lightness,
- year: car1.produced + '/' + car2.sold,
- mix: car1.hue + '+' + car2.sold
- },
- [
- [ car1, { hue: 'greens', produced: 1930 } ],
- [ car2, { lightness: 'dark', sold: 2000 } ]
- ],
- {
- color: 'greensdark',
- year: '1930/2000',
- mix: 'greens+2000'
- }
- );
- } );
- it( 'should work with callback – binding more that once #3', () => {
- const vehicle = new Car();
- const car1 = new Car( { hue: 'reds', produced: 1920 } );
- const car2 = new Car( { lightness: 'bright', sold: 1921 } );
- vehicle.bind( 'color' )
- .to( car1, 'hue', car2, 'lightness', ( ...args ) => args.join( '' ) );
- vehicle.bind( 'custom1' ).to( car1, 'hue' );
- vehicle.bind( 'year' )
- .to( car1, 'produced', car2, 'sold', ( ...args ) => args.join( '/' ) );
- vehicle.bind( 'custom2', 'custom3' ).to( car1, 'produced', 'hue' );
- assertBinding( vehicle,
- {
- color: car1.hue + car2.lightness,
- year: car1.produced + '/' + car2.sold,
- custom1: car1.hue,
- custom2: car1.produced,
- custom3: car1.hue
- },
- [
- [ car1, { hue: 'greens', produced: 1930 } ],
- [ car2, { lightness: 'dark', sold: 2000 } ]
- ],
- {
- color: 'greensdark',
- year: '1930/2000',
- custom1: 'greens',
- custom2: 1930,
- custom3: 'greens'
- }
- );
- } );
- it( 'should fire a single change event per bound attribute', () => {
- const vehicle = new Car();
- const car = new Car( { color: 'red', year: 1943 } );
- const spy = sinon.spy();
- vehicle.on( 'change', spy );
- vehicle.bind( 'color', 'year' ).to( car );
- car.color = 'violet';
- car.custom = 'foo';
- car.year = 2001;
- expect( spy.args.map( args => args[ 1 ] ) )
- .to.have.members( [ 'color', 'year', 'color', 'year' ] );
- } );
- } );
- } );
- describe( 'unbind', () => {
- it( 'should not fail when unbinding a fresh observable', () => {
- const observable = new Observable();
- observable.unbind();
- } );
- it( 'should throw when non-string attribute is passed', () => {
- expect( () => {
- car.unbind( new Date() );
- } ).to.throwCKEditorError( /observable-unbind-wrong-attrs/ );
- } );
- it( 'should remove all bindings', () => {
- const vehicle = new Car();
- vehicle.bind( 'color', 'year' ).to( car, 'color', 'year' );
- vehicle.unbind();
- assertBinding( vehicle,
- { color: 'red', year: 2015 },
- [
- [ car, { color: 'blue', year: 1969 } ]
- ],
- { color: 'red', year: 2015 }
- );
- } );
- it( 'should remove bindings of certain attributes', () => {
- const vehicle = new Car();
- const car = new Car( { color: 'red', year: 2000, torque: 160 } );
- vehicle.bind( 'color', 'year', 'torque' ).to( car );
- vehicle.unbind( 'year', 'torque' );
- assertBinding( vehicle,
- { color: 'red', year: 2000, torque: 160 },
- [
- [ car, { color: 'blue', year: 1969, torque: 220 } ]
- ],
- { color: 'blue', year: 2000, torque: 160 }
- );
- } );
- it( 'should remove bindings of certain attributes, callback', () => {
- const vehicle = new Car();
- const car1 = new Car( { color: 'red' } );
- const car2 = new Car( { color: 'blue' } );
- vehicle.bind( 'color' ).to( car1, car2, ( c1, c2 ) => c1 + c2 );
- vehicle.unbind( 'color' );
- assertBinding( vehicle,
- { color: 'redblue' },
- [
- [ car1, { color: 'green' } ],
- [ car2, { color: 'violet' } ]
- ],
- { color: 'redblue' }
- );
- } );
- it( 'should be able to unbind two attributes from a single source observable attribute', () => {
- const vehicle = new Car();
- vehicle.bind( 'color' ).to( car, 'color' );
- vehicle.bind( 'interiorColor' ).to( car, 'color' );
- vehicle.unbind( 'color' );
- vehicle.unbind( 'interiorColor' );
- assertBinding( vehicle,
- { color: 'red', interiorColor: 'red' },
- [
- [ car, { color: 'blue' } ]
- ],
- { color: 'red', interiorColor: 'red' }
- );
- } );
- } );
- } );
|