mediaregistry.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 console */
  6. import MediaRegistry from '../src/mediaregistry';
  7. describe( 'MediaRegistry', () => {
  8. describe( 'constructor()', () => {
  9. it( 'filters out providers that should be removed', () => {
  10. const providers = [
  11. { name: 'dailymotion', url: [] },
  12. { name: 'spotify', url: [] },
  13. { name: 'youtube', url: [] },
  14. { name: 'vimeo', url: [] }
  15. ];
  16. const removeProviders = [ 'spotify' ];
  17. const mediaRegistry = new MediaRegistry( {}, { providers, removeProviders } );
  18. const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
  19. expect( availableProviders ).to.deep.equal( [ 'dailymotion', 'youtube', 'vimeo' ] );
  20. } );
  21. it( 'allows extending providers using `extraProviders` option', () => {
  22. const providers = [
  23. { name: 'dailymotion', url: [] },
  24. { name: 'youtube', url: [] },
  25. { name: 'vimeo', url: [] }
  26. ];
  27. const extraProviders = [
  28. { name: 'spotify', url: [] }
  29. ];
  30. const mediaRegistry = new MediaRegistry( {}, { providers, extraProviders } );
  31. const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
  32. expect( availableProviders ).to.deep.equal( [ 'dailymotion', 'youtube', 'vimeo', 'spotify' ] );
  33. } );
  34. it( 'logs a warning when provider\'s name is not defined', () => {
  35. const consoleWarnStub = sinon.stub( console, 'warn' );
  36. const providers = [
  37. { url: [ /dailymotion\.com/ ] },
  38. { name: 'spotify', url: [] },
  39. { name: 'youtube', url: [] },
  40. { name: 'vimeo', url: [] }
  41. ];
  42. const mediaRegistry = new MediaRegistry( {}, { providers } );
  43. const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
  44. expect( availableProviders ).to.deep.equal( [ 'spotify', 'youtube', 'vimeo' ] );
  45. expect( consoleWarnStub.calledOnce ).to.equal( true );
  46. expect( consoleWarnStub.firstCall.args[ 0 ] ).to.match( /^media-embed-no-provider-name/ );
  47. expect( consoleWarnStub.firstCall.args[ 1 ] ).to.deep.equal( { provider: { url: [ /dailymotion\.com/ ] } } );
  48. } );
  49. } );
  50. describe( '_getMedia()', () => {
  51. let mediaRegistry, htmlSpy;
  52. beforeEach( () => {
  53. htmlSpy = sinon.spy();
  54. mediaRegistry = new MediaRegistry( {}, {
  55. providers: [
  56. {
  57. name: 'youtube',
  58. url: [
  59. /^youtube\.com\/watch\?v=([\w-]+)/,
  60. /^youtube\.com\/v\/([\w-]+)/,
  61. /^youtube\.com\/embed\/([\w-]+)/,
  62. /^youtu\.be\/([\w-]+)/
  63. ],
  64. html: htmlSpy
  65. }
  66. ]
  67. } );
  68. } );
  69. it( 'works fine for url with sub-domain and the protocol', () => {
  70. const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  71. expect( media ).is.not.null;
  72. expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  73. } );
  74. it( 'works fine for url with defined protocol', () => {
  75. const media = mediaRegistry._getMedia( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  76. expect( media ).is.not.null;
  77. expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  78. } );
  79. it( 'works fine for url with sub-domain without protocol', () => {
  80. const media = mediaRegistry._getMedia( 'www.youtube.com/watch?v=euqbMkM-QQk' );
  81. expect( media ).is.not.null;
  82. expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  83. } );
  84. it( 'works fine for url without protocol', () => {
  85. const media = mediaRegistry._getMedia( 'youtube.com/watch?v=euqbMkM-QQk' );
  86. expect( media ).is.not.null;
  87. expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  88. } );
  89. it( 'passes the entire match array to render function', () => {
  90. const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  91. media._getPreviewHtml();
  92. expect( htmlSpy.calledOnce ).to.equal( true );
  93. expect( htmlSpy.firstCall.args[ 0 ] ).to.deep.equal( [
  94. 'youtube.com/watch?v=euqbMkM-QQk',
  95. 'euqbMkM-QQk'
  96. ] );
  97. } );
  98. } );
  99. } );