htmlembedupdatecommand.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  8. import HTMLEmbedEditing from '../src/htmlembedediting';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. describe( 'HTMLEmbedUpdateCommand', () => {
  12. let editor, model, editorElement, command;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. editorElement = document.createElement( 'div' );
  16. document.body.appendChild( editorElement );
  17. return ClassicTestEditor
  18. .create( editorElement, {
  19. plugins: [ HTMLEmbedEditing, Paragraph ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. command = editor.commands.get( 'htmlEmbedUpdate' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy()
  29. .then( () => {
  30. editorElement.remove();
  31. } );
  32. } );
  33. describe( 'isEnabled', () => {
  34. it( 'should be true when the selection contains single the `rawHtml` element', () => {
  35. setModelData( model, '[<rawHtml></rawHtml>]' );
  36. command.refresh();
  37. expect( command.isEnabled ).to.equal( true );
  38. } );
  39. it( 'should be false when the selection contains more than single `rawHtml` element', () => {
  40. setModelData( model, '[<rawHtml></rawHtml><rawHtml></rawHtml>]' );
  41. command.refresh();
  42. expect( command.isEnabled ).to.equal( false );
  43. } );
  44. it( 'should be false when the selection contains other element than the `rawHtml` element', () => {
  45. setModelData( model, '[<paragraph></paragraph>]' );
  46. command.refresh();
  47. expect( command.isEnabled ).to.equal( false );
  48. } );
  49. } );
  50. describe( 'execute()', () => {
  51. it( 'should create a single batch', () => {
  52. setModelData( model, '[<rawHtml></rawHtml>]' );
  53. const spy = sinon.spy();
  54. model.document.on( 'change', spy );
  55. command.execute( '<b>Foo.</b>' );
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. it( 'should update the `value` attribute of selected the `rawHtml` element', () => {
  59. setModelData( model, '[<rawHtml></rawHtml>]' );
  60. command.execute( '<b>Foo.</b>' );
  61. const rawHtml = model.document.getRoot().getChild( 0 );
  62. expect( rawHtml.getAttribute( 'value' ) ).to.equal( '<b>Foo.</b>' );
  63. } );
  64. it( 'should update nothing if selected other element than the `rawHtml` element', () => {
  65. setModelData( model, '[<paragraph></paragraph>]' );
  66. command.execute( '<b>Foo.</b>' );
  67. expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  68. } );
  69. } );
  70. } );