/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import ListStylesEditing from '../src/liststylesediting';
describe( 'ListStylesCommand', () => {
let editor, model, bulletedListCommand, numberedListCommand, listStylesCommand;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Paragraph, ListStylesEditing ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
bulletedListCommand = editor.commands.get( 'bulletedList' );
numberedListCommand = editor.commands.get( 'numberedList' );
listStylesCommand = editor.commands.get( 'listStyles' );
} );
} );
afterEach( () => {
return editor.destroy();
} );
describe( '#isEnabled', () => {
it( 'should be true if bulletedList or numberedList is enabled', () => {
bulletedListCommand.isEnabled = true;
numberedListCommand.isEnabled = false;
listStylesCommand.refresh();
expect( listStylesCommand.isEnabled ).to.equal( true );
bulletedListCommand.isEnabled = false;
numberedListCommand.isEnabled = true;
listStylesCommand.refresh();
expect( listStylesCommand.isEnabled ).to.equal( true );
} );
it( 'should be false if bulletedList and numberedList are enabled', () => {
bulletedListCommand.isEnabled = false;
numberedListCommand.isEnabled = false;
listStylesCommand.refresh();
expect( listStylesCommand.isEnabled ).to.equal( false );
} );
} );
describe( '#value', () => {
it( 'should return null if selected a paragraph', () => {
setData( model, 'Foo[]' );
expect( listStylesCommand.value ).to.equal( null );
} );
it( 'should return null if selection starts in a paragraph and ends in a list item', () => {
setData( model,
'Fo[o' +
'Foo]'
);
expect( listStylesCommand.value ).to.equal( null );
} );
it( 'should return the value of `listStyle` attribute if selection is inside a listItem (collapsed selection)', () => {
setData( model, 'Foo[]' );
expect( listStylesCommand.value ).to.equal( 'default' );
} );
it( 'should return the value of `listStyle` attribute if selection is inside a listItem (non-collapsed selection)', () => {
setData( model, '[Foo]' );
expect( listStylesCommand.value ).to.equal( 'default' );
} );
it( 'should return the value of `listStyle` attribute if selected more elements in the same list', () => {
setData( model,
'[1.' +
'2.]' +
'3.'
);
expect( listStylesCommand.value ).to.equal( 'square' );
} );
it( 'should return the value of `listStyle` attribute for the selection inside a nested list', () => {
setData( model,
'1.' +
'1.1.[]' +
'2.'
);
expect( listStylesCommand.value ).to.equal( 'disc' );
} );
it( 'should return the value of `listStyle` attribute from a list where the selection starts (selection over nested list)', () => {
setData( model,
'1.' +
'1.1.[' +
'2.]'
);
expect( listStylesCommand.value ).to.equal( 'disc' );
} );
} );
describe( 'execute()', () => {
it( 'should set the `listStyle` attribute for collapsed selection', () => {
setData( model,
'1.[]'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'1.[]'
);
} );
it( 'should set the `listStyle` attribute for non-collapsed selection', () => {
setData( model,
'[1.]'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'[1.]'
);
} );
it( 'should set the `listStyle` attribute for all the same list items (collapsed selection)', () => {
setData( model,
'1.[]' +
'2.' +
'3.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'1.[]' +
'2.' +
'3.'
);
} );
it( 'should set the `listStyle` attribute for all the same list items and ignores nested lists', () => {
setData( model,
'1.[]' +
'2.' +
'2.1.' +
'2.2.' +
'3.' +
'3.1.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'1.[]' +
'2.' +
'2.1.' +
'2.2.' +
'3.' +
'3.1.'
);
} );
it( 'should set the `listStyle` attribute for all the same list items and ignores nested lists (selection in nested list)', () => {
setData( model,
'1.' +
'2.' +
'2.1.[]' +
'2.2.' +
'3.' +
'3.1.'
);
listStylesCommand.execute( { type: 'disc' } );
expect( getData( model ) ).to.equal(
'1.' +
'2.' +
'2.1.[]' +
'2.2.' +
'3.' +
'3.1.'
);
} );
it( 'should stop searching for the list items when spotted non-listItem element', () => {
setData( model,
'Foo.' +
'1.[]' +
'2.' +
'3.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'Foo.' +
'1.[]' +
'2.' +
'3.'
);
} );
it( 'should stop searching for the list items when spotted listItem with different listType attribute', () => {
setData( model,
'Foo.' +
'1.[]' +
'2.' +
'1.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'Foo.' +
'1.[]' +
'2.' +
'1.'
);
} );
it( 'should stop searching for the list items when spotted listItem with different listStyle attribute', () => {
setData( model,
'Foo.' +
'1.[]' +
'2.' +
'1.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'Foo.' +
'1.[]' +
'2.' +
'1.'
);
} );
it( 'should start searching for the list items from starting position (collapsed selection)', () => {
setData( model,
'1.' +
'2.' +
'[3.' +
'Foo.]'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'1.' +
'2.' +
'[3.' +
'Foo.]'
);
} );
it( 'should start searching for the list items from ending position (collapsed selection)', () => {
setData( model,
'[Foo.' +
'1.]' +
'2.' +
'3.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'[Foo.' +
'1.]' +
'2.' +
'3.'
);
} );
it( 'should use default type if not specified (no options passed)', () => {
setData( model,
'1.[]'
);
listStylesCommand.execute();
expect( getData( model ) ).to.equal(
'1.[]'
);
} );
it( 'should use default type if not specified (passed an empty object)', () => {
setData( model,
'1.[]'
);
listStylesCommand.execute( {} );
expect( getData( model ) ).to.equal(
'1.[]'
);
} );
it( 'should use default type if not specified (passed null as value)', () => {
setData( model,
'1.[]'
);
listStylesCommand.execute( { type: null } );
expect( getData( model ) ).to.equal(
'1.[]'
);
} );
it( 'should not update anything if no listItem found in the selection', () => {
setData( model,
'[Foo.]' +
'1.'
);
listStylesCommand.execute( { type: 'circle' } );
expect( getData( model ) ).to.equal(
'[Foo.]' +
'1.'
);
} );
} );
} );