imagetextalternativecommand.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ImageTextAlternativeCommand from '../../src/imagetextalternative/imagetextalternativecommand';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'ImageTextAlternativeCommand', () => {
  9. let model, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. model = newEditor.model;
  14. command = new ImageTextAlternativeCommand( newEditor );
  15. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  16. model.schema.register( 'image', {
  17. allowWhere: '$block',
  18. isObject: true,
  19. isBlock: true,
  20. alllowAttributes: [ 'alt', 'src' ]
  21. } );
  22. } );
  23. } );
  24. it( 'should have false value if no image is selected', () => {
  25. setData( model, '[]<p></p>' );
  26. expect( command.value ).to.be.false;
  27. } );
  28. it( 'should have false value if image without alt is selected', () => {
  29. setData( model, '[<image src="image.png"></image>]' );
  30. expect( command.value ).to.be.false;
  31. } );
  32. it( 'should be disabled if not on image element', () => {
  33. setData( model, '[]<p></p>' );
  34. expect( command.isEnabled ).to.be.false;
  35. } );
  36. it( 'should be enabled on image element without alt attribute', () => {
  37. setData( model, '[<image src="image.png"></image>]' );
  38. expect( command.isEnabled ).to.be.true;
  39. } );
  40. it( 'should have proper value if on image element with alt attribute', () => {
  41. setData( model, '[<image src="image.png" alt="foo bar baz"></image>]' );
  42. expect( command.value ).to.equal( 'foo bar baz' );
  43. } );
  44. it( 'should set proper alt if executed on image without alt attribute', () => {
  45. setData( model, '[<image src="image.png"></image>]' );
  46. command.execute( { newValue: 'fiz buz' } );
  47. expect( getData( model ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
  48. } );
  49. it( 'should change alt if executed on image with alt attribute', () => {
  50. setData( model, '[<image alt="foo bar" src="image.png"></image>]' );
  51. command.execute( { newValue: 'fiz buz' } );
  52. expect( getData( model ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
  53. } );
  54. it( 'should use parent batch', () => {
  55. setData( model, '[<image src="image.png"></image>]' );
  56. model.change( writer => {
  57. expect( writer.batch.operations ).to.length( 0 );
  58. command.execute( { newValue: 'foo bar' } );
  59. expect( writer.batch.operations ).to.length.above( 0 );
  60. } );
  61. } );
  62. } );