Procházet zdrojové kódy

Added basic tests for new feature.

Kamil Piechaczek před 7 roky
rodič
revize
38edd3fc7c

+ 1 - 1
packages/ckeditor5-media-embed/src/mediaembedediting.js

@@ -11,8 +11,8 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
 import { modelToViewUrlAttributeConverter } from './converters';
 import MediaEmbedCommand from './mediaembedcommand';
+import MediaRegistry from './mediaregistry';
 import { toMediaWidget, createMediaFigureElement } from './utils';
-import { MediaRegistry } from './mediaregistry';
 import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
 import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
 

+ 1 - 1
packages/ckeditor5-media-embed/src/mediaregistry.js

@@ -22,7 +22,7 @@ const mediaPlaceholderIconViewBox = '0 0 64 42';
  *
  * Mostly used by the {@link module:media-embed/mediaembedediting~MediaEmbedEditing} plugin.
  */
-export class MediaRegistry {
+export default class MediaRegistry {
 	/**
 	 * Creates an instance of the {@link module:media-embed/mediaregistry~MediaRegistry} class.
 	 *

+ 126 - 0
packages/ckeditor5-media-embed/tests/mediaregistry.js

@@ -0,0 +1,126 @@
+/**
+ * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import MediaRegistry from '../src/mediaregistry';
+import log from '@ckeditor/ckeditor5-utils/src/log';
+
+describe( 'MediaRegistry', () => {
+	describe( 'constructor()', () => {
+		it( 'filters out providers that should be removed', () => {
+			const providers = [
+				{ name: 'dailymotion', url: [] },
+				{ name: 'spotify', url: [] },
+				{ name: 'youtube', url: [] },
+				{ name: 'vimeo', url: [] },
+			];
+			const removeProviders = [ 'spotify' ];
+
+			const mediaRegistry = new MediaRegistry( {}, { providers, removeProviders } );
+			const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
+
+			expect( availableProviders ).to.deep.equal( [ 'dailymotion', 'youtube', 'vimeo' ] );
+		} );
+
+		it( 'allows extending providers using `extraProviders` option', () => {
+			const providers = [
+				{ name: 'dailymotion', url: [] },
+				{ name: 'youtube', url: [] },
+				{ name: 'vimeo', url: [] },
+			];
+			const extraProviders = [
+				{ name: 'spotify', url: [] },
+			];
+
+			const mediaRegistry = new MediaRegistry( {}, { providers, extraProviders } );
+			const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
+
+			expect( availableProviders ).to.deep.equal( [ 'dailymotion', 'youtube', 'vimeo', 'spotify' ] );
+		} );
+
+		it( 'logs a warning when provider\'s name is not defined', () => {
+			const logStub = sinon.stub( log, 'warn' );
+
+			const providers = [
+				{ url: [ /dailymotion\.com/ ] },
+				{ name: 'spotify', url: [] },
+				{ name: 'youtube', url: [] },
+				{ name: 'vimeo', url: [] },
+			];
+
+			const mediaRegistry = new MediaRegistry( {}, { providers } );
+			const availableProviders = mediaRegistry.providerDefinitions.map( provider => provider.name );
+
+			expect( availableProviders ).to.deep.equal( [ 'spotify', 'youtube', 'vimeo' ] );
+			expect( logStub.calledOnce ).to.equal( true );
+			expect( logStub.firstCall.args[ 0 ] ).to.equal(
+				'media-embed-no-provider-name: The configured media provider has no name and cannot be used.'
+			);
+			expect( logStub.firstCall.args[ 1 ] ).to.deep.equal( { provider: { url: [ /dailymotion\.com/ ] } } );
+		} );
+	} );
+
+	describe( '_getMedia()', () => {
+		let mediaRegistry, htmlSpy;
+
+		beforeEach( () => {
+			htmlSpy = sinon.spy();
+
+			mediaRegistry = new MediaRegistry( {}, {
+				providers: [
+					{
+						name: 'youtube',
+						url: [
+							/^youtube\.com\/watch\?v=([\w-]+)/,
+							/^youtube\.com\/v\/([\w-]+)/,
+							/^youtube\.com\/embed\/([\w-]+)/,
+							/^youtu\.be\/([\w-]+)/
+						],
+						html: htmlSpy
+					}
+				]
+			} );
+		} );
+
+		it( 'works fine for url with sub-domain and the protocol', () => {
+			const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
+
+			expect( media ).is.not.null;
+			expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
+		} );
+
+		it( 'works fine for url with defined protocol', () => {
+			const media = mediaRegistry._getMedia( 'https://youtube.com/watch?v=euqbMkM-QQk' );
+
+			expect( media ).is.not.null;
+			expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
+		} );
+
+		it( 'works fine for url with sub-domain without protocol', () => {
+			const media = mediaRegistry._getMedia( 'www.youtube.com/watch?v=euqbMkM-QQk' );
+
+			expect( media ).is.not.null;
+			expect( media.url ).to.equal( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
+		} );
+
+		it( 'works fine for url without protocol', () => {
+			const media = mediaRegistry._getMedia( 'youtube.com/watch?v=euqbMkM-QQk' );
+
+			expect( media ).is.not.null;
+			expect( media.url ).to.equal( 'https://youtube.com/watch?v=euqbMkM-QQk' );
+		} );
+
+		it( 'passes the entire match array to render function', () => {
+			const media = mediaRegistry._getMedia( 'https://www.youtube.com/watch?v=euqbMkM-QQk' );
+
+			media._getContentHtml();
+
+			expect( htmlSpy.calledOnce ).to.equal( true );
+			expect( htmlSpy.firstCall.args[ 0 ] ).to.deep.equal( [
+				'youtube.com/watch?v=euqbMkM-QQk',
+				'euqbMkM-QQk'
+			] );
+		} );
+	} );
+} );