浏览代码

Add tests.

Rémy HUBSCHER 8 年之前
父节点
当前提交
27e8d6a7cc

+ 2 - 1
packages/ckeditor5-basic-styles/docs/api/basic-styles.md

@@ -6,7 +6,7 @@ category: api-reference
 
 [![npm version](https://badge.fury.io/js/%40ckeditor%2Fckeditor5-basic-styles.svg)](https://www.npmjs.com/package/@ckeditor/ckeditor5-basic-styles)
 
-This package contains features allowing to apply basic text formatting such as bold, italic, underline and code in CKEditor 5.
+This package contains features allowing to apply basic text formatting such as bold, italic, underline, strike and code in CKEditor 5.
 
 ## Documentation
 
@@ -14,6 +14,7 @@ Check out the following plugins:
 
 * {@link module:basic-styles/bold~Bold}
 * {@link module:basic-styles/italic~Italic}
+* {@link module:basic-styles/strike~Strike}
 * {@link module:basic-styles/underline~Underline}
 * {@link module:basic-styles/code~Code}
 

+ 3 - 2
packages/ckeditor5-basic-styles/tests/manual/basic-styles.js

@@ -10,13 +10,14 @@ import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Bold from '../../src/bold';
 import Italic from '../../src/italic';
+import Strike from '../../src/strike';
 import Underline from '../../src/underline';
 import Code from '../../src/code';
 
 ClassicEditor
 	.create( document.querySelector( '#editor' ), {
-		plugins: [ Essentials, Paragraph, Bold, Italic, Underline, Code ],
-		toolbar: [ 'bold', 'italic', 'underline', 'code', 'undo', 'redo' ]
+		plugins: [ Essentials, Paragraph, Bold, Italic, Strike, Underline, Code ],
+		toolbar: [ 'bold', 'italic', 'strike', 'underline', 'code', 'undo', 'redo' ]
 	} )
 	.then( editor => {
 		window.editor = editor;

+ 5 - 4
packages/ckeditor5-basic-styles/tests/manual/basic-styles.md

@@ -1,8 +1,9 @@
 ## Basic styles
 
 1. The data should be loaded with:
-  * italic "This",
-  * bold "editor",
+  * italic _"This"_,
+  * bold **"editor"**,
   * underline "instance",
-  * code "is an".
-2. Test the bold, italic, underline and code features live.
+  * strike ~~"is"~~,
+  * code `"an"`.
+2. Test the bold, italic, strike, underline and code features live.

+ 97 - 0
packages/ckeditor5-basic-styles/tests/strike.js

@@ -0,0 +1,97 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Rémy Hubscher. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals document */
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import Strike from '../src/strike';
+import StrikeEngine from '../src/strikeengine';
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+
+testUtils.createSinonSandbox();
+
+describe( 'Strike', () => {
+	let editor, strikeView;
+
+	beforeEach( () => {
+		const editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
+
+		return ClassicTestEditor
+			.create( editorElement, {
+				plugins: [ Strike ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				strikeView = editor.ui.componentFactory.create( 'strike' );
+			} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy();
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( Strike ) ).to.be.instanceOf( Strike );
+	} );
+
+	it( 'should load StrikeEngine', () => {
+		expect( editor.plugins.get( StrikeEngine ) ).to.be.instanceOf( StrikeEngine );
+	} );
+
+	it( 'should register strike feature component', () => {
+		expect( strikeView ).to.be.instanceOf( ButtonView );
+		expect( strikeView.isOn ).to.be.false;
+		expect( strikeView.label ).to.equal( 'Strike' );
+		expect( strikeView.icon ).to.match( /<svg / );
+		expect( strikeView.keystroke ).to.equal( 'CTRL+E' );
+	} );
+
+	it( 'should execute strike command on model execute event', () => {
+		const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+
+		strikeView.fire( 'execute' );
+
+		sinon.assert.calledOnce( executeSpy );
+		sinon.assert.calledWithExactly( executeSpy, 'strike' );
+	} );
+
+	it( 'should bind model to strike command', () => {
+		const command = editor.commands.get( 'strike' );
+
+		expect( strikeView.isOn ).to.be.false;
+
+		expect( strikeView.isEnabled ).to.be.false;
+
+		command.value = true;
+		expect( strikeView.isOn ).to.be.true;
+
+		command.isEnabled = true;
+		expect( strikeView.isEnabled ).to.be.true;
+	} );
+
+	it( 'should set keystroke in the model', () => {
+		expect( strikeView.keystroke ).to.equal( 'CTRL+E' );
+	} );
+
+	it( 'should set editor keystroke', () => {
+		const spy = sinon.spy( editor, 'execute' );
+		const keyEventData = {
+			keyCode: keyCodes.e,
+			ctrlKey: true,
+			preventDefault: sinon.spy(),
+			stopPropagation: sinon.spy()
+		};
+
+		const wasHandled = editor.keystrokes.press( keyEventData );
+
+		expect( wasHandled ).to.be.true;
+		expect( spy.calledOnce ).to.be.true;
+		expect( keyEventData.preventDefault.calledOnce ).to.be.true;
+	} );
+} );

+ 108 - 0
packages/ckeditor5-basic-styles/tests/strikeengine.js

@@ -0,0 +1,108 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Rémy Hubscher. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import StrikeEngine from '../src/strikeengine';
+
+import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import AttributeCommand from '../src/attributecommand';
+
+import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
+import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
+
+describe( 'StrikeEngine', () => {
+	let editor, doc;
+
+	beforeEach( () => {
+		return VirtualTestEditor
+			.create( {
+				plugins: [ Paragraph, StrikeEngine ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				doc = editor.document;
+			} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy();
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( StrikeEngine ) ).to.be.instanceOf( StrikeEngine );
+	} );
+
+	it( 'should set proper schema rules', () => {
+		expect( doc.schema.check( { name: '$inline', attributes: 'strike', inside: '$root' } ) ).to.be.false;
+		expect( doc.schema.check( { name: '$inline', attributes: 'strike', inside: '$block' } ) ).to.be.true;
+		expect( doc.schema.check( { name: '$inline', attributes: 'strike', inside: '$clipboardHolder' } ) ).to.be.true;
+	} );
+
+	describe( 'command', () => {
+		it( 'should register strike command', () => {
+			const command = editor.commands.get( 'strike' );
+
+			expect( command ).to.be.instanceOf( AttributeCommand );
+			expect( command ).to.have.property( 'attributeKey', 'strike' );
+		} );
+	} );
+
+	describe( 'data pipeline conversions', () => {
+		it( 'should convert <strike> to strike attribute', () => {
+			editor.setData( '<p><strike>foo</strike>bar</p>' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph><$text strike="true">foo</$text>bar</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p><s>foo</s>bar</p>' );
+		} );
+		it( 'should convert <del> to strike attribute', () => {
+			editor.setData( '<p><del>foo</del>bar</p>' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph><$text strike="true">foo</$text>bar</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p><s>foo</s>bar</p>' );
+		} );
+
+		it( 'should convert <s> to strike attribute', () => {
+			editor.setData( '<p><s>foo</s>bar</p>' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph><$text strike="true">foo</$text>bar</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p><s>foo</s>bar</p>' );
+		} );
+
+		it( 'should convert text-decoration:line-through to strike attribute', () => {
+			editor.setData( '<p><span style="text-decoration: line-through;">foo</span>bar</p>' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) )
+				.to.equal( '<paragraph><$text strike="true">foo</$text>bar</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p><s>foo</s>bar</p>' );
+		} );
+
+		it( 'should be integrated with autoparagraphing', () => {
+			// Incorrect results because autoparagraphing works incorrectly (issue in paragraph).
+			// https://github.com/ckeditor/ckeditor5-paragraph/issues/10
+
+			editor.setData( '<s>foo</s>bar' );
+
+			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foobar</paragraph>' );
+
+			expect( editor.getData() ).to.equal( '<p>foobar</p>' );
+		} );
+	} );
+
+	describe( 'editing pipeline conversion', () => {
+		it( 'should convert attribute', () => {
+			setModelData( doc, '<paragraph><$text strike="true">foo</$text>bar</paragraph>' );
+
+			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><s>foo</s>bar</p>' );
+		} );
+	} );
+} );