| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* global document, HTMLElement */
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import View from '../src/view';
- import Template from '../src/template';
- import Collection from '@ckeditor/ckeditor5-utils/src/collection';
- import ViewCollection from '../src/viewcollection';
- import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml';
- import { assertCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- let TestView, view, childA, childB;
- describe( 'View', () => {
- testUtils.createSinonSandbox();
- afterEach( () => {
- if ( view.element ) {
- view.element.remove();
- }
- view.destroy();
- } );
- describe( 'constructor()', () => {
- beforeEach( () => {
- setTestViewClass();
- setTestViewInstance();
- } );
- it( 'defines basic view properties', () => {
- view = new View();
- expect( view.t ).to.be.undefined;
- expect( view.locale ).to.be.undefined;
- expect( view.isRendered ).to.be.false;
- expect( view.template ).to.be.undefined;
- expect( view._viewCollections ).to.be.instanceOf( Collection );
- expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
- } );
- it( 'defines the locale property and the "t" function', () => {
- const locale = { t() {} };
- view = new View( locale );
- expect( view.locale ).to.equal( locale );
- expect( view.t ).to.equal( locale.t );
- } );
- describe( '_viewCollections', () => {
- it( 'manages #locale property', () => {
- const locale = {
- t() {}
- };
- const view = new View( locale );
- const collection = new ViewCollection();
- expect( view.locale ).to.equal( locale );
- expect( collection.locale ).to.be.undefined;
- view._viewCollections.add( collection );
- expect( collection.locale ).to.equal( view.locale );
- } );
- } );
- } );
- describe( 'createCollection()', () => {
- beforeEach( () => {
- setTestViewClass();
- setTestViewInstance();
- } );
- it( 'returns an instance of view collection', () => {
- expect( view.createCollection() ).to.be.instanceOf( ViewCollection );
- } );
- it( 'adds a new collection to the #_viewCollections', () => {
- expect( view._viewCollections ).to.have.length( 1 );
- const collection = view.createCollection();
- expect( view._viewCollections ).to.have.length( 2 );
- expect( view._viewCollections.get( 1 ) ).to.equal( collection );
- } );
- it( 'accepts initial views', () => {
- const viewA = new View();
- const viewB = new View();
- const collection = view.createCollection( [ viewA, viewB ] );
- expect( collection ).to.have.length( 2 );
- expect( collection.get( 0 ) ).to.equal( viewA );
- expect( collection.get( 1 ) ).to.equal( viewB );
- } );
- } );
- describe( 'registerChild()', () => {
- beforeEach( () => {
- setTestViewClass();
- setTestViewInstance();
- } );
- it( 'should add a single view to #_unboundChildren', () => {
- expect( view._unboundChildren ).to.have.length( 0 );
- const child = new View();
- view.registerChild( child );
- expect( view._unboundChildren ).to.have.length( 1 );
- expect( view._unboundChildren.get( 0 ) ).to.equal( child );
- } );
- it( 'should support iterables', () => {
- expect( view._unboundChildren ).to.have.length( 0 );
- view.registerChild( [ new View(), new View(), new View() ] );
- expect( view._unboundChildren ).to.have.length( 3 );
- } );
- } );
- describe( 'deregisterChild()', () => {
- beforeEach( () => {
- setTestViewClass();
- setTestViewInstance();
- } );
- it( 'should remove a single view from #_unboundChildren', () => {
- const child1 = new View();
- const child2 = new View();
- view.registerChild( child1 );
- view.registerChild( child2 );
- expect( view._unboundChildren ).to.have.length( 2 );
- view.deregisterChild( child2 );
- expect( view._unboundChildren ).to.have.length( 1 );
- expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
- } );
- it( 'should support iterables', () => {
- const child1 = new View();
- const child2 = new View();
- const child3 = new View();
- view.registerChild( [ child1, child2, child3 ] );
- expect( view._unboundChildren ).to.have.length( 3 );
- view.deregisterChild( [ child2, child3 ] );
- expect( view._unboundChildren ).to.have.length( 1 );
- expect( view._unboundChildren.get( 0 ) ).to.equal( child1 );
- } );
- } );
- describe( 'setTemplate()', () => {
- it( 'sets the template', () => {
- const view = new View();
- const bind = view.bindTemplate;
- view.set( 'foo', 'bar' );
- view.setTemplate( {
- tag: 'div',
- attributes: {
- class: [
- bind.to( 'foo' )
- ]
- }
- } );
- view.render();
- expect( normalizeHtml( view.element.outerHTML ) ).to.equal( '<div class="bar"></div>' );
- } );
- } );
- describe( 'extendTemplate()', () => {
- it( 'extends the template', () => {
- const view = new View();
- const bind = view.bindTemplate;
- view.set( 'foo', 'bar' );
- view.setTemplate( {
- tag: 'div'
- } );
- view.extendTemplate( {
- attributes: {
- class: [
- bind.to( 'foo' )
- ]
- }
- } );
- view.render();
- expect( normalizeHtml( view.element.outerHTML ) ).to.equal( '<div class="bar"></div>' );
- } );
- } );
- describe( 'render()', () => {
- it( 'is decorated', done => {
- const view = new View();
- view.on( 'render', () => {
- expect( view.isRendered ).to.be.true;
- done();
- } );
- view.render();
- } );
- it( 'should throw if already rendered', () => {
- const view = new View();
- view.render();
- try {
- view.render();
- throw new Error( 'This should not be executed.' );
- } catch ( err ) {
- // TODO
- assertCKEditorError( err, /^ui-view-render-already-rendered:/, view );
- }
- } );
- it( 'should set view#isRendered', () => {
- const view = new View();
- view.setTemplate( {
- tag: 'div'
- } );
- expect( view.isRendered ).to.be.false;
- view.render();
- expect( view.isRendered ).to.be.true;
- } );
- } );
- describe( 'bind', () => {
- beforeEach( () => {
- setTestViewClass();
- setTestViewInstance();
- } );
- it( 'returns a shorthand for Template binding', () => {
- expect( view.bindTemplate.to ).to.be.a( 'function' );
- expect( view.bindTemplate.if ).to.be.a( 'function' );
- const binding = view.bindTemplate.to( 'a' );
- expect( binding.observable ).to.equal( view );
- expect( binding.emitter ).to.equal( view );
- } );
- } );
- describe( 'element', () => {
- beforeEach( createViewInstanceWithTemplate );
- it( 'invokes out of #template', () => {
- expect( view.element ).to.be.an.instanceof( HTMLElement );
- expect( view.element.nodeName ).to.equal( 'A' );
- } );
- it( 'can be explicitly declared', () => {
- class CustomView extends View {
- constructor() {
- super();
- this.element = document.createElement( 'span' );
- }
- }
- const view = new CustomView();
- expect( view.element ).to.be.an.instanceof( HTMLElement );
- } );
- it( 'is null when there is no template', () => {
- const view = new View();
- view.render();
- expect( view.element ).to.be.null;
- } );
- it( 'registers child views found in the template', () => {
- const view = new View();
- const viewA = new View();
- const viewB = new View();
- const viewC = new View();
- viewA.setTemplate( { tag: 'a' } );
- viewB.setTemplate( { tag: 'b' } );
- viewC.setTemplate( { tag: 'c' } );
- view.setTemplate( {
- tag: 'div',
- children: [
- viewA,
- viewB,
- {
- tag: 'p',
- children: [
- viewC
- ]
- },
- {
- text: 'foo'
- }
- ]
- } );
- expect( view._unboundChildren ).to.have.length( 0 );
- view.render();
- expect( view._unboundChildren ).to.have.length( 3 );
- expect( view._unboundChildren.get( 0 ) ).to.equal( viewA );
- expect( view._unboundChildren.get( 1 ) ).to.equal( viewB );
- expect( view._unboundChildren.get( 2 ) ).to.equal( viewC );
- } );
- } );
- describe( 'destroy()', () => {
- beforeEach( createViewWithChildren );
- it( 'can be called multiple times', () => {
- expect( () => {
- view.destroy();
- view.destroy();
- view.destroy();
- } ).to.not.throw();
- } );
- it( 'should not touch the basic properties', () => {
- view.destroy();
- expect( view.element ).to.be.an.instanceof( HTMLElement );
- expect( view.template ).to.be.an.instanceof( Template );
- expect( view.locale ).to.be.an( 'object' );
- expect( view.locale.t ).to.be.a( 'function' );
- expect( view._viewCollections ).to.be.instanceOf( Collection );
- expect( view._unboundChildren ).to.be.instanceOf( ViewCollection );
- } );
- it( 'should not clear the #_unboundChildren', () => {
- const cached = view._unboundChildren;
- view.registerChild( [ new View(), new View() ] );
- expect( cached ).to.have.length( 4 );
- view.destroy();
- expect( cached ).to.have.length( 4 );
- } );
- it( 'should not clear the #_viewCollections', () => {
- const cached = view._viewCollections;
- expect( cached ).to.have.length( 1 );
- view.destroy();
- expect( cached ).to.have.length( 1 );
- } );
- it( 'leaves the #element in DOM', () => {
- const elRef = view.element;
- const parentEl = document.createElement( 'div' );
- parentEl.appendChild( view.element );
- view.destroy();
- expect( elRef.parentNode ).to.equal( parentEl );
- } );
- it( 'calls destroy() on all view#_viewCollections', () => {
- const collectionA = view.createCollection();
- const collectionB = view.createCollection();
- const spyA = testUtils.sinon.spy( collectionA, 'destroy' );
- const spyB = testUtils.sinon.spy( collectionB, 'destroy' );
- view.destroy();
- sinon.assert.calledOnce( spyA );
- sinon.assert.calledOnce( spyB );
- sinon.assert.callOrder( spyA, spyB );
- } );
- it( 'destroy a template–less view', () => {
- const view = new View();
- expect( () => {
- view.destroy();
- } ).to.not.throw();
- } );
- } );
- } );
- function createViewInstanceWithTemplate() {
- setTestViewClass( { tag: 'a' } );
- setTestViewInstance();
- }
- function setTestViewClass( templateDef ) {
- TestView = class V extends View {
- constructor() {
- super();
- this.locale = { t() {} };
- if ( templateDef ) {
- this.setTemplate( templateDef );
- }
- }
- };
- }
- function setTestViewInstance() {
- view = new TestView();
- if ( view.template ) {
- view.render();
- document.body.appendChild( view.element );
- }
- }
- function createViewWithChildren() {
- class ChildView extends View {
- constructor() {
- super();
- this.setTemplate( {
- tag: 'span'
- } );
- }
- }
- childA = new ChildView();
- childB = new ChildView();
- setTestViewClass( {
- tag: 'p',
- children: [ childA, childB ]
- } );
- setTestViewInstance();
- }
|