8
0

cloudservices.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import CloudServices from '../src/cloudservices';
  7. import Context from '@ckeditor/ckeditor5-core/src/context';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import TokenMock from './_utils/tokenmock';
  10. const Token = CloudServices.Token;
  11. describe( 'CloudServices', () => {
  12. let element;
  13. beforeEach( () => {
  14. CloudServices.Token = TokenMock;
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. } );
  18. afterEach( () => {
  19. CloudServices.Token = Token;
  20. document.body.removeChild( element );
  21. } );
  22. describe( 'init()', () => {
  23. it( 'should expose its properties based on config', () => {
  24. return Context
  25. .create( {
  26. plugins: [ CloudServices ],
  27. cloudServices: {
  28. tokenUrl: 'http://token-endpoint',
  29. additionalOption: 'some-value'
  30. }
  31. } )
  32. .then( editor => {
  33. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  34. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  35. expect( cloudServicesPlugin.tokenUrl ).to.equal( 'http://token-endpoint' );
  36. expect( cloudServicesPlugin.additionalOption ).to.equal( 'some-value' );
  37. return editor.destroy();
  38. } );
  39. } );
  40. it( 'should work as an editor plugin', () => {
  41. return ClassicTestEditor
  42. .create( element, {
  43. plugins: [ CloudServices ],
  44. cloudServices: {
  45. tokenUrl: 'http://token-endpoint',
  46. additionalOption: 'some-value'
  47. }
  48. } )
  49. .then( editor => {
  50. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  51. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  52. expect( cloudServicesPlugin.tokenUrl ).to.equal( 'http://token-endpoint' );
  53. expect( cloudServicesPlugin.additionalOption ).to.equal( 'some-value' );
  54. return editor.destroy();
  55. } );
  56. } );
  57. it( 'should be able to get by its plugin name', () => {
  58. return Context.create( { plugins: [ CloudServices ] } ).then( editor => {
  59. const cloudServicesPlugin = editor.plugins.get( 'CloudServices' );
  60. expect( cloudServicesPlugin ).to.be.instanceOf( CloudServices );
  61. } );
  62. } );
  63. it( 'should not throw an error when no config is provided', () => {
  64. return Context.create( { plugins: [ CloudServices ] } );
  65. } );
  66. it( 'should not expose any default uploadUrl', () => {
  67. return Context.create( { plugins: [ CloudServices ] } ).then( editor => {
  68. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  69. expect( cloudServicesPlugin.uploadUrl ).to.be.undefined;
  70. } );
  71. } );
  72. it( 'should use provided uploadUrl', () => {
  73. return Context
  74. .create( {
  75. plugins: [ CloudServices ],
  76. cloudServices: {
  77. uploadUrl: 'https://some-upload-url/'
  78. }
  79. } )
  80. .then( editor => {
  81. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  82. expect( cloudServicesPlugin.uploadUrl ).to.equal( 'https://some-upload-url/' );
  83. } );
  84. } );
  85. it( 'should provide token if tokenUrl is provided', () => {
  86. CloudServices.Token.initialToken = 'initial-token';
  87. return Context
  88. .create( {
  89. plugins: [ CloudServices ],
  90. cloudServices: {
  91. tokenUrl: 'http://token-endpoint',
  92. }
  93. } )
  94. .then( editor => {
  95. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  96. expect( cloudServicesPlugin.token.value ).to.equal( 'initial-token' );
  97. return editor.destroy();
  98. } );
  99. } );
  100. it( 'should not provide token if tokenUrl is not provided', () => {
  101. CloudServices.Token.initialToken = 'initial-token';
  102. return Context.create( { plugins: [ CloudServices ] } ).then( editor => {
  103. const cloudServicesPlugin = editor.plugins.get( CloudServices );
  104. expect( cloudServicesPlugin.token ).to.equal( null );
  105. } );
  106. } );
  107. } );
  108. } );