Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Manual tweaks in tests to make them work with the new Schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Dec 29, 2017
1 parent 54f9a3d commit 1a34b3d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
30 changes: 25 additions & 5 deletions tests/conversion/buildviewconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ describe( 'View converter builder', () => {
inheritAllFrom: '$text'
} );
schema.extend( '$text', {
allowAttributes: textAttributes
allowAttributes: textAttributes,
allowIn: [ '$root', 'span', 'abcd', 'MEGATRON' ]
} );

dispatcher = new ViewConversionDispatcher( model, { schema } );
Expand Down Expand Up @@ -493,8 +494,17 @@ describe( 'View converter builder', () => {
buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );

// TODO this requires a callback
// schema.xdisallow( { name: 'div', inside: '$root' } );
// Disallow $root>div.
schema.on( 'checkChild', ( evt, args ) => {
const context = args[ 0 ];
const child = args[ 1 ];
const childRule = schema.getRule( child );

if ( childRule.name == 'div' && context[ context.length - 1 ].name == '$root' ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );

dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );

Expand All @@ -513,8 +523,18 @@ describe( 'View converter builder', () => {
buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );

// TODO this requires a callback
// schema.xdisallow( { name: '$text', attributes: 'bold', inside: 'paragraph' } );
// Disallow bold in paragraph>$text.
schema.on( 'checkAttribute', ( evt, args ) => {
const context = args[ 0 ];
const ctxItem = context[ context.length - 1 ];
const ctxParent = context[ context.length - 2 ];
const attributeName = args[ 1 ];

if ( ctxItem.name == '$text' && ctxParent.name == 'paragraph' && attributeName == 'bold' ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );

dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );

Expand Down
3 changes: 1 addition & 2 deletions tests/conversion/definition-based-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
} from '../../src/conversion/definition-based-converters';

import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
import ModelSchema from '../../src/model/schema';
import ModelWalker from '../../src/model/treewalker';
import ModelTextProxy from '../../src/model/textproxy';
import Model from '../../src/model/model';
Expand Down Expand Up @@ -104,7 +103,7 @@ describe( 'definition-based-converters', () => {

function setupViewToModelTests() {
additionalData = { context: [ '$root' ] };
schema = new ModelSchema();
schema = model.schema;
dispatcher = new ViewConversionDispatcher( model, { schema } );
}

Expand Down
23 changes: 16 additions & 7 deletions tests/conversion/view-to-model-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ import ViewDocumentFragment from '../../src/view/documentfragment';
import ViewText from '../../src/view/text';

import Model from '../../src/model/model';
import ModelSchema from '../../src/model/schema';
import ModelDocumentFragment from '../../src/model/documentfragment';
import ModelElement from '../../src/model/element';
import ModelText from '../../src/model/text';

import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';

describe( 'view-to-model-converters', () => {
let dispatcher, schema, additionalData;

const model = new Model();
let dispatcher, schema, additionalData, model;

beforeEach( () => {
schema = new ModelSchema();
model = new Model();
schema = model.schema;

schema.register( 'paragraph', { inheritAllFrom: '$block' } );
schema.extend( '$text', { allowIn: '$root' } );

additionalData = { context: [ '$root' ] };

dispatcher = new ViewConversionDispatcher( model, { schema } );
} );

Expand Down Expand Up @@ -62,8 +63,16 @@ describe( 'view-to-model-converters', () => {
} );

it( 'should not convert text if it is wrong with schema', () => {
// TODO this requires a callback
// schema.xdisallow( { name: '$text', inside: '$root' } );
schema.on( 'checkChild', ( evt, args ) => {
const context = args[ 0 ];
const child = args[ 1 ];
const childRule = schema.getRule( child );

if ( childRule.name == '$text' && context[ context.length - 1 ].name == '$root' ) {
evt.stop();
evt.return = false;
}
}, { priority: 'high' } );

const viewText = new ViewText( 'foobar' );
dispatcher.on( 'text', convertText() );
Expand Down
4 changes: 2 additions & 2 deletions tests/dev-utils/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,15 @@ describe( 'model test utils', () => {
it( 'throws when try to set element not registered in schema', () => {
expect( () => {
parse( '<xyz></xyz>', model.schema );
} ).to.throw( Error, 'Element \'xyz\' not allowed in context ["$root"].' );
} ).to.throw( Error, 'Element \'xyz\' was not allowed in context ["$root"].' );
} );

it( 'throws when try to set text directly to $root without registering it', () => {
const model = new Model();

expect( () => {
parse( 'text', model.schema );
} ).to.throw( Error, 'Element \'$text\' not allowed in context ["$root"].' );
} ).to.throw( Error, 'Text was not allowed in context ["$root"].' );
} );

it( 'converts data in the specified context', () => {
Expand Down
12 changes: 7 additions & 5 deletions tests/model/document/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,18 @@ describe( 'Document', () => {
beforeEach( () => {
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );

model.schema.register( 'emptyBlock' );
model.schema.extend( 'emptyBlock', { allowIn: '$root' } );
model.schema.register( 'emptyBlock', { allowIn: '$root' } );

model.schema.register( 'widget', {
allowIn: '$root',
isObject: true
} );
model.schema.extend( 'widget', { allowIn: '$root' } );

model.schema.register( 'blockWidget', { inheritAllFrom: '$block' } );
model.schema.extend( 'blockWidget', { allowIn: '$root' } );
model.schema.register( 'blockWidget', {
allowIn: '$root',
allowContentOf: '$block',
isObject: true
} );

doc.createRoot();
selection = doc.selection;
Expand Down
10 changes: 6 additions & 4 deletions tests/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ describe( 'Selection', () => {

beforeEach( () => {
schema = new Schema();
schema.register( 'p', { inheritAllFrom: '$block' } );
schema.register( '$root' );
schema.register( 'p', { allowIn: '$root' } );
schema.register( '$text', { allowIn: 'p' } );
} );

it( 'should return selected element', () => {
Expand Down Expand Up @@ -933,9 +935,9 @@ describe( 'Selection', () => {
model.schema.extend( 'blockquote', { allowIn: '$root' } );
model.schema.extend( '$block', { allowIn: 'blockquote' } );

model.schema.register( 'image' );
model.schema.extend( 'image', { allowIn: '$root' } );
model.schema.extend( 'image', { allowIn: '$block' } );
model.schema.register( 'image', {
allowIn: [ '$root', '$block' ]
} );
model.schema.extend( '$text', { allowIn: 'image' } );

// Special block which can contain another blocks.
Expand Down

0 comments on commit 1a34b3d

Please sign in to comment.