Parcourir la source

Provide unit test for new methods related to updateing switch button status.

Mateusz Samsel il y a 6 ans
Parent
commit
f6dad0eb56

+ 41 - 0
packages/ckeditor5-link/tests/linkcommand.js

@@ -353,5 +353,46 @@ describe( 'LinkCommand', () => {
 				expect( getData( model ) ).to.equal( 'foo[<$text linkHref="url">url</$text>]bar' );
 			} );
 		} );
+
+		describe( 'restoreManualDecoratorStates()', () => {
+			it( 'synchronize values with current model state', () => {
+				setData( model, 'foo<$text linkHref="url" linkManualDecorator0="true" linkManualDecorator1="true" >u[]rl</$text>bar' );
+
+				expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
+					linkManualDecorator0: true,
+					linkManualDecorator1: true
+				} );
+
+				command.manualDecorators.first.value = false;
+
+				expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
+					linkManualDecorator0: false,
+					linkManualDecorator1: true,
+				} );
+
+				command.restoreManualDecoratorStates();
+
+				expect( decoratorStates( command.manualDecorators ) ).to.deep.equal( {
+					linkManualDecorator0: true,
+					linkManualDecorator1: true,
+				} );
+			} );
+		} );
+
+		describe( '_getDecoratorStateFromModel', () => {
+			it( 'obtain current values from the model', () => {
+				setData( model, 'foo[<$text linkHref="url" linkManualDecorator1="true" >url</$text>]bar' );
+
+				expect( command._getDecoratorStateFromModel( 'linkManualDecorator0' ) ).to.be.false;
+				expect( command._getDecoratorStateFromModel( 'linkManualDecorator1' ) ).to.be.true;
+			} );
+		} );
 	} );
 } );
+
+function decoratorStates( manualDecorators ) {
+	return Array.from( manualDecorators ).reduce( ( accumulator, currentValue ) => {
+		accumulator[ currentValue.id ] = currentValue.value;
+		return accumulator;
+	}, {} );
+}

+ 70 - 43
packages/ckeditor5-link/tests/linkui.js

@@ -959,58 +959,85 @@ describe( 'LinkUI', () => {
 				expect( focusSpy.calledBefore( removeSpy ) ).to.equal( true );
 			} );
 
-			it( 'should gather information about manual decorators', () => {
-				let editor, model, formView;
-				const editorElement = document.createElement( 'div' );
-				document.body.appendChild( editorElement );
-
-				return ClassicTestEditor
-					.create( editorElement, {
-						plugins: [ LinkEditing, LinkUI, Paragraph ],
-						link: {
-							decorators: [
-								{
-									mode: 'manual',
-									label: 'Foo',
-									attributes: {
-										foo: 'bar'
+			describe( 'support manual decorators', () => {
+				let editorElement, editor, model, formView, linkUIFeature;
+
+				beforeEach( () => {
+					editorElement = document.createElement( 'div' );
+					document.body.appendChild( editorElement );
+					return ClassicTestEditor
+						.create( editorElement, {
+							plugins: [ LinkEditing, LinkUI, Paragraph ],
+							link: {
+								decorators: [
+									{
+										mode: 'manual',
+										label: 'Foo',
+										attributes: {
+											foo: 'bar'
+										}
 									}
-								}
-							]
-						}
-					} )
-					.then( newEditor => {
-						editor = newEditor;
-						model = editor.model;
-
-						model.schema.extend( '$text', {
-							allowIn: '$root',
-							allowAttributes: 'linkHref'
+								]
+							}
+						} )
+						.then( newEditor => {
+							editor = newEditor;
+							model = editor.model;
+
+							model.schema.extend( '$text', {
+								allowIn: '$root',
+								allowAttributes: 'linkHref'
+							} );
+
+							linkUIFeature = editor.plugins.get( LinkUI );
+
+							const balloon = editor.plugins.get( ContextualBalloon );
+
+							formView = linkUIFeature.formView;
+
+							// There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
+							testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
+							testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
+
+							formView.render();
 						} );
+				} );
 
-						const linkUIFeature = editor.plugins.get( LinkUI );
-						const balloon = editor.plugins.get( ContextualBalloon );
+				afterEach( () => {
+					editorElement.remove();
+				} );
+
+				it( 'should gather information about manual decorators', () => {
+					const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+
+					setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
+					expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
+					expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkManualDecorator0: true } );
 
-						formView = linkUIFeature.formView;
+					formView.fire( 'submit' );
 
-						// There is no point to execute BalloonPanelView attachTo and pin methods so lets override it.
-						testUtils.sinon.stub( balloon.view, 'attachTo' ).returns( {} );
-						testUtils.sinon.stub( balloon.view, 'pin' ).returns( {} );
+					expect( executeSpy.calledOnce ).to.be.true;
+					expect( executeSpy.calledWithExactly( 'link', 'url', { linkManualDecorator0: true } ) ).to.be.true;
+				} );
+
+				it( 'should reset switch state when form view is closed', () => {
+					setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
 
-						formView.render();
-					} )
-					.then( () => {
-						const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+					const manualDecorators = editor.commands.get( 'link' ).manualDecorators;
+					const firstDecoratorModel = manualDecorators.first;
+					const firstDecoratorSwitch = formView._manualDecoratorSwitches.first;
 
-						setModelData( model, 'f[<$text linkHref="url" linkManualDecorator0="true">ooba</$text>]r' );
-						expect( formView.urlInputView.inputView.element.value ).to.equal( 'url' );
-						expect( formView.getDecoratorSwitchesState() ).to.deep.equal( { linkManualDecorator0: true } );
+					expect( firstDecoratorModel.value, 'Initial value should be read from the model (true)' ).to.be.true;
+					expect( firstDecoratorSwitch.isOn, 'Initial value should be read from the model (true)' ).to.be.true;
 
-						formView.fire( 'submit' );
+					firstDecoratorSwitch.fire( 'execute' );
+					expect( firstDecoratorModel.value, 'Pressing button toggles value' ).to.be.false;
+					expect( firstDecoratorSwitch.isOn, 'Pressing button toggles value' ).to.be.false;
 
-						expect( executeSpy.calledOnce ).to.be.true;
-						expect( executeSpy.calledWithExactly( 'link', 'url', { linkManualDecorator0: true } ) ).to.be.true;
-					} );
+					linkUIFeature._closeFormView();
+					expect( firstDecoratorModel.value, 'Close form view without submit resets value to initial state' ).to.be.true;
+					expect( firstDecoratorSwitch.isOn, 'Close form view without submit resets value to initial state' ).to.be.true;
+				} );
 			} );
 		} );
 	} );

+ 1 - 0
packages/ckeditor5-link/tests/ui/linkformview.js

@@ -228,6 +228,7 @@ describe( 'LinkFormView', () => {
 			view.destroy();
 			collection.clear();
 		} );
+
 		it( 'switch buttons reflects state of manual decorators', () => {
 			expect( view._manualDecoratorSwitches.length ).to.equal( 3 );