mediaregistry.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import MediaRegistry from '../src/mediaregistry';
  6. import log from '@ckeditor/ckeditor5-utils/src/log';
  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 logStub = sinon.stub( log, '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( logStub.calledOnce ).to.equal( true );
  46. expect( logStub.firstCall.args[ 0 ] ).to.equal(
  47. 'media-embed-no-provider-name: The configured media provider has no name and cannot be used.'
  48. );
  49. expect( logStub.firstCall.args[ 1 ] ).to.deep.equal( { provider: { url: [ /dailymotion\.com/ ] } } );
  50. } );
  51. } );
  52. describe( '_getMedia()', () => {
  53. let mediaRegistry, htmlSpy;
  54. beforeEach( () => {
  55. htmlSpy = sinon.spy();
  56. mediaRegistry = new MediaRegistry( {}, {
  57. providers: [
  58. {
  59. name: 'youtube',
  60. url: [
  61. /^youtube\.com\/watch\?v=([\w-]+)/,
  62. /^youtube\.com\/v\/([\w-]+)/,
  63. /^youtube\.com\/embed\/([\w-]+)/,
  64. /^youtu\.be\/([\w-]+)/
  65. ],
  66. html: htmlSpy
  67. }
  68. ]
  69. } );
  70. } );
  71. it( 'works fine for url with sub-domain and the protocol', () => {
  72. const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  73. expect( media ).is.not.null;
  74. expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  75. } );
  76. it( 'works fine for url with defined protocol', () => {
  77. const media = mediaRegistry._getMedia( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  78. expect( media ).is.not.null;
  79. expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  80. } );
  81. it( 'works fine for url with sub-domain without protocol', () => {
  82. const media = mediaRegistry._getMedia( 'www.youtube.com/watch?v=euqbMkM-QQk' );
  83. expect( media ).is.not.null;
  84. expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  85. } );
  86. it( 'works fine for url without protocol', () => {
  87. const media = mediaRegistry._getMedia( 'youtube.com/watch?v=euqbMkM-QQk' );
  88. expect( media ).is.not.null;
  89. expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
  90. } );
  91. it( 'passes the entire match array to render function', () => {
  92. const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
  93. media._getContentHtml();
  94. expect( htmlSpy.calledOnce ).to.equal( true );
  95. expect( htmlSpy.firstCall.args[ 0 ] ).to.deep.equal( [
  96. 'youtube.com/watch?v=euqbMkM-QQk',
  97. 'euqbMkM-QQk'
  98. ] );
  99. } );
  100. } );
  101. } );