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

Commit

Permalink
Provide unit test for new methods related to updateing switch button …
Browse files Browse the repository at this point in the history
…status.
  • Loading branch information
Mateusz Samsel committed Jun 24, 2019
1 parent a6951ec commit 3bfd2c5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 43 deletions.
41 changes: 41 additions & 0 deletions tests/linkcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, {} );
}
113 changes: 70 additions & 43 deletions tests/linkui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
} );
} );
} );
} );
Expand Down
1 change: 1 addition & 0 deletions tests/ui/linkformview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit 3bfd2c5

Please sign in to comment.