From d0cd5592a2a975a643b47f5c961f8e46a361416a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Fri, 21 Sep 2018 14:11:01 +0200 Subject: [PATCH] Cleaned up the tests. Removed confusing boundary cases with setData() and added explicit test for setting selection's attrs. Cleared schema rules. --- tests/model/schema.js | 57 +++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/tests/model/schema.js b/tests/model/schema.js index c6d470399..b79a16de7 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -1035,6 +1035,9 @@ describe( 'Schema', () => { allowIn: '$root', allowAttributes: [ 'name', 'title' ] } ); + schema.extend( '$text', { + allowAttributes: [ 'italic' ] + } ); schema.addAttributeCheck( ( ctx, attributeName ) => { // Allow 'bold' on p>$text. @@ -1046,24 +1049,12 @@ describe( 'Schema', () => { if ( ctx.endsWith( '$root p' ) && attributeName == 'bold' ) { return true; } - } ); - schema.addAttributeCheck( ( ctx, attributeName ) => { - // Disallow 'italic' on $text that has attribute 'bold'. + // Disallow 'italic' on $text that has 'bold' already. if ( inTextWithBold( ctx ) && attributeName == 'italic' ) { return false; } - // Allow 'italic' on p>$text. - if ( ctx.endsWith( 'p $text' ) && attributeName == 'italic' ) { - return true; - } - - // Allow 'italic' on $root>p. - if ( ctx.endsWith( '$root p' ) && attributeName == 'italic' ) { - return true; - } - function inTextWithBold( context ) { return context.endsWith( '$text' ) && context.last.getAttribute( 'bold' ); } @@ -1084,22 +1075,19 @@ describe( 'Schema', () => { expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false; } ); - it( 'should check attributes of the selection (selection at the beginning of the text)', () => { - setData( model, '

<$text bold="true">[]foo

' ); - expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false; - } ); - - it( 'should check attributes of the selection (selection inside the text)', () => { + it( 'should check attributes of the selection (selection inside the $text[bold])', () => { setData( model, '

<$text bold="true">f[]oo

' ); - expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false; - } ); - it( 'should check attributes of the selection (selection at the end of the text)', () => { - setData( model, '

<$text bold="true">foo[]

' ); expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false; + + model.change( writer => { + writer.removeSelectionAttribute( 'bold' ); + } ); + + expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.true; } ); - it( 'should check attributes of the selection (an attribute sets manually)', () => { + it( 'should check attributes of the selection (attribute set manually on selection)', () => { setData( model, '

foo[]bar

' ); expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.true; @@ -1110,6 +1098,27 @@ describe( 'Schema', () => { expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false; } ); + + it( 'should pass all selection\'s attributes to checkAttribute()', done => { + schema.on( 'checkAttribute', ( evt, args ) => { + const context = args[ 0 ]; + const attributeName = args[ 1 ]; + + expect( attributeName ).to.equal( 'italic' ); + expect( Array.from( context.last.getAttributeKeys() ) ).to.deep.equal( [ 'bold', 'underline' ] ); + + done(); + }, { priority: 'highest' } ); + + setData( model, '

foo[]bar

' ); + + model.change( writer => { + writer.setSelectionAttribute( 'bold', true ); + writer.setSelectionAttribute( 'underline', true ); + } ); + + expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false; + } ); } ); describe( 'when selection is not collapsed', () => {