imagetextalternativecommand.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 document, command;
  10. beforeEach( () => {
  11. return ModelTestEditor.create()
  12. .then( newEditor => {
  13. document = newEditor.document;
  14. command = new ImageTextAlternativeCommand( newEditor );
  15. document.schema.registerItem( 'p', '$block' );
  16. document.schema.registerItem( 'image' );
  17. document.schema.requireAttributes( 'image', [ 'src' ] );
  18. document.schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  19. document.schema.objects.add( 'image' );
  20. } );
  21. } );
  22. it( 'should have false value if no image is selected', () => {
  23. setData( document, '[]<p></p>' );
  24. expect( command.value ).to.be.false;
  25. } );
  26. it( 'should have false value if image without alt is selected', () => {
  27. setData( document, '[<image src="image.png"></image>]' );
  28. expect( command.value ).to.be.false;
  29. } );
  30. it( 'should be disabled if not on image element', () => {
  31. setData( document, '[]<p></p>' );
  32. expect( command.isEnabled ).to.be.false;
  33. } );
  34. it( 'should be enabled on image element without alt attribute', () => {
  35. setData( document, '[<image src="image.png"></image>]' );
  36. expect( command.isEnabled ).to.be.true;
  37. } );
  38. it( 'should have proper value if on image element with alt attribute', () => {
  39. setData( document, '[<image src="image.png" alt="foo bar baz"></image>]' );
  40. expect( command.value ).to.equal( 'foo bar baz' );
  41. } );
  42. it( 'should set proper alt if executed on image without alt attribute', () => {
  43. setData( document, '[<image src="image.png"></image>]' );
  44. command.execute( { newValue: 'fiz buz' } );
  45. expect( getData( document ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
  46. } );
  47. it( 'should change alt if executed on image with alt attribute', () => {
  48. setData( document, '[<image alt="foo bar" src="image.png"></image>]' );
  49. command.execute( { newValue: 'fiz buz' } );
  50. expect( getData( document ) ).to.equal( '[<image alt="fiz buz" src="image.png"></image>]' );
  51. } );
  52. it( 'should allow to provide batch instance', () => {
  53. const batch = document.batch();
  54. const spy = sinon.spy( batch, 'setAttribute' );
  55. setData( document, '[<image src="image.png"></image>]' );
  56. command.execute( { newValue: 'foo bar', batch } );
  57. expect( getData( document ) ).to.equal( '[<image alt="foo bar" src="image.png"></image>]' );
  58. sinon.assert.calledOnce( spy );
  59. } );
  60. } );