Browse Source

Added test to getData/setData model test utilities.

Szymon Kupś 9 years ago
parent
commit
f008cbd61d

+ 56 - 3
packages/ckeditor5-engine/tests/_utils-tests/model.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-import { stringify, parse } from '/tests/engine/_utils/model.js';
+import { stringify, parse, getData, setData } from '/tests/engine/_utils/model.js';
 import Document from '/ckeditor5/engine/treemodel/document.js';
 import DocumentFragment from '/ckeditor5/engine/treemodel/documentfragment.js';
 import Element from '/ckeditor5/engine/treemodel/element.js';
@@ -14,16 +14,69 @@ import Range from '/ckeditor5/engine/treemodel/range.js';
 import Position from '/ckeditor5/engine/treemodel/position.js';
 
 describe( 'model test utils', () => {
-	let document, root, selection;
+	let document, root, selection, sandbox;
 
 	beforeEach( () => {
 		document = new Document();
 		root = document.createRoot( 'main', '$root' );
 		selection = document.selection;
-
+		sandbox = sinon.sandbox.create();
 		selection.removeAllRanges();
 	} );
 
+	afterEach( () => {
+		sandbox.restore();
+	} );
+
+	describe( 'getData', () => {
+		it( 'should use stringify method', () => {
+			const stringifySpy = sandbox.spy( getData, '_stringify' );
+			root.appendChildren( new Element( 'b', null, [ 'btext' ] ) );
+
+			expect( getData( document ) ).to.equal( '<b>btext</b>' );
+			sinon.assert.calledOnce( stringifySpy );
+			sinon.assert.calledWithExactly( stringifySpy, root );
+		} );
+
+		it( 'should use stringify method with selection', () => {
+			const stringifySpy = sandbox.spy( getData, '_stringify' );
+			root.appendChildren( new Element( 'b', null, [ 'btext' ] ) );
+			document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
+
+			expect( getData( document, { withSelection: true } ) ).to.equal( '<selection><b>btext</b></selection>' );
+			sinon.assert.calledOnce( stringifySpy );
+			sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
+		} );
+	} );
+
+	describe( 'setData', () => {
+		it( 'should use parse method', () => {
+			const parseSpy = sandbox.spy( setData, '_parse' );
+			const options = {};
+			const data = '<b>btext</b>text';
+
+			setData( document, data, options );
+
+			expect( getData( document ) ).to.equal( data );
+			sinon.assert.calledOnce( parseSpy );
+			const args = parseSpy.firstCall.args;
+			expect( args[ 0 ] ).to.equal( data );
+		} );
+
+		it( 'should use parse method with selection', () => {
+			const parseSpy = sandbox.spy( setData, '_parse' );
+			const options = {};
+			const data = '[<b>btext</b>]';
+
+			setData( document, data, options );
+
+			expect( getData( document ) ).to.equal( data );
+			sinon.assert.calledOnce( parseSpy );
+			const args = parseSpy.firstCall.args;
+			expect( args[ 0 ] ).to.equal( data );
+		} );
+	} );
+
 	describe( 'stringify', () => {
 		it( 'should stringify text', () => {
 			const text = new Text( 'text', { underline: true, bold: true } );

+ 10 - 4
packages/ckeditor5-engine/tests/_utils/model.js

@@ -30,25 +30,31 @@ export function getData( document, options = {} ) {
 	const rootName = options.rootName || 'main';
 	const root = document.getRoot( rootName );
 
-	return withSelection ? stringify( root, document.selection ) : stringify( root );
+	return withSelection ? getData._stringify( root, document.selection ) : getData._stringify( root );
 }
 
+// Set stringify as getData private method - needed for testing/spying.
+getData._stringify = stringify;
+
 /**
  * Sets the contents of the {@link engine.treeModel.Document Document} provided as HTML-like string.
  *
  * @param {engine.treeModel.Document} document
  * @param {String} data HTML-like string to write into Document.
  * @param {Object} options
- * @param {String} [rootName] Root name where parsed data will be stored. If not provided, default `main` name will be
+ * @param {String} [options.rootName] Root name where parsed data will be stored. If not provided, default `main` name will be
  * used.
  */
 export function setData( document, data, options = {} ) {
-	parse( data, {
+	setData._parse( data, {
 		document: document,
 		rootName: options.rootName
 	} );
 }
 
+// Set parse as setData private method - needed for testing/spying.
+setData._parse = parse;
+
 /**
  * Converts model nodes to HTML-like string representation.
  *
@@ -113,7 +119,7 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
  *
  * @param {String} data HTML-like string to be parsed.
  * @param {Object} options
- * @param {engine.treeModel.Document} [options.document] Document to be used to create root element and selection. If
+ * @param {engine.treeModel.Document} [options.document] Document from which root element and selection will be used. If
  * not provided new {engine.treeModel.Document document} instance will be created.
  * @param {String} [options.rootName='main'] When `document` option is provided this root name will be used to create
  * {engine.treeModel.RootElement RootElement} instance.