8
0
Просмотр исходного кода

Provide small improvements in normalizer. Add unit test covering normalizers functionalities.

Mateusz Samsel 6 лет назад
Родитель
Сommit
ecd0e5fc26

+ 10 - 15
packages/ckeditor5-paste-from-office/src/contentnormalizer.js

@@ -13,28 +13,23 @@ export default class ContentNormalizer {
 
 		this.activationTrigger = activationTrigger;
 
-		this._active = false;
+		this.isActive = false;
 		this._filters = new Set();
 		this._fullContentFilters = new Set();
 	}
 
-	get isActive() {
-		return this._active;
-	}
-
 	setInputData( data ) {
 		const html = data.dataTransfer.getData( 'text/html' );
+		const dataReadFirstTime = data.isTransformedWithPasteFromOffice === undefined;
+		const hasHtmlData = !!html;
 
-		if ( html && this.activationTrigger( html ) ) {
+		if ( hasHtmlData && dataReadFirstTime && this.activationTrigger( html ) ) {
 			this.data = data;
-			this._active = true;
-
-			if ( this.data.isTransformedWithPasteFromOffice === undefined ) {
-				this.data.isTransformedWithPasteFromOffice = false;
-			}
+			this.isActive = true;
+			this.data.isTransformedWithPasteFromOffice = false;
 		} else {
-			this.data = undefined;
-			this._active = false;
+			this.data = null;
+			this.isActive = false;
 		}
 
 		return this;
@@ -51,10 +46,10 @@ export default class ContentNormalizer {
 	exec() {
 		if ( this.data && !this.data.isTransformedWithPasteFromOffice ) {
 			this._applyFullContentFilters();
-
-			this.data.isTransformedWithPasteFromOffice = true;
 		}
 
+		this.data.isTransformedWithPasteFromOffice = true;
+
 		return this;
 	}
 

+ 224 - 0
packages/ckeditor5-paste-from-office/tests/contentnormalizer.js

@@ -0,0 +1,224 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import ContentNormalizer from '../src/contentnormalizer';
+
+describe( 'ContentNormalizer', () => {
+	let normalizer, sinonTrigger;
+	const templateData = {
+		dataTransfer: {
+			getData: () => 'test data'
+		}
+	};
+
+	testUtils.createSinonSandbox();
+
+	beforeEach( () => {
+		sinonTrigger = sinon.fake.returns( true );
+
+		normalizer = new ContentNormalizer( {
+			activationTrigger: sinonTrigger
+		} );
+	} );
+
+	describe( 'constructor()', () => {
+		it( 'should be not active at start', () => {
+			expect( normalizer.isActive ).to.be.false;
+		} );
+
+		it( 'should not have assigned data', () => {
+			expect( normalizer.data ).to.be.null;
+		} );
+
+		it( 'should have assigned activation trigger', () => {
+			expect( normalizer.activationTrigger ).to.be.a( 'function' );
+			expect( normalizer.activationTrigger ).to.equal( sinonTrigger );
+		} );
+
+		it( 'should initialize sets for filters', () => {
+			expect( normalizer._filters ).to.be.a( 'set' );
+			expect( normalizer._fullContentFilters ).to.be.a( 'set' );
+		} );
+	} );
+
+	describe( 'setInputData()', () => {
+		let data;
+
+		beforeEach( () => {
+			data = Object.assign( {}, templateData );
+		} );
+
+		describe( 'trigger activated', () => {
+			beforeEach( () => {
+				normalizer.setInputData( data );
+			} );
+
+			it( 'should set data', () => {
+				expect( normalizer.data ).to.equal = data;
+			} );
+
+			it( 'should check if activates normalizer', () => {
+				sinon.assert.calledOnce( sinonTrigger );
+				sinon.assert.calledWith( sinonTrigger, 'test data' );
+
+				expect( normalizer.isActive ).to.be.true;
+			} );
+
+			it( 'should add flag to data processed by paste from office plugin', () => {
+				expect( data.isTransformedWithPasteFromOffice ).to.be.false;
+			} );
+		} );
+
+		describe( 'trigger not activated', () => {
+			beforeEach( () => {
+				sinonTrigger = sinon.fake.returns( false );
+
+				normalizer = new ContentNormalizer( {
+					activationTrigger: sinonTrigger
+				} );
+
+				normalizer.setInputData( data );
+			} );
+
+			it( 'should not be active', () => {
+				sinon.assert.calledOnce( sinonTrigger );
+				sinon.assert.calledWith( sinonTrigger, 'test data' );
+
+				expect( normalizer.isActive ).to.be.false;
+			} );
+
+			it( 'should not keep reference to data when is not active', () => {
+				expect( normalizer.data ).to.be.null;
+			} );
+
+			it( 'should not add flag to not processed data', () => {
+				expect( data.isTransformedWithPasteFromOffice ).to.be.undefined;
+			} );
+		} );
+
+		describe( 'already processed data', () => {
+			beforeEach( () => {
+				data.isTransformedWithPasteFromOffice = true;
+
+				normalizer.addFilter( {
+					fullContent: true,
+					// eslint-disable-next-line no-unused-vars
+					exec: d => { d = {}; }
+				} );
+			} );
+
+			it( 'should not change data', () => {
+				normalizer.setInputData( data );
+
+				expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+				expect( data ).to.deep.include( templateData );
+			} );
+		} );
+	} );
+
+	describe( 'addFilter()', () => {
+		let filter;
+		describe( 'fullContentFilters', () => {
+			beforeEach( () => {
+				filter = {
+					fullContent: true,
+					exec: () => {}
+				};
+
+				normalizer.addFilter( filter );
+			} );
+
+			it( 'should add filter to fullContentFilters set', () => {
+				expect( normalizer._fullContentFilters.size ).to.equal( 1 );
+				expect( normalizer._filters.size ).to.equal( 0 );
+
+				const firstFilter = [ ...normalizer._fullContentFilters ][ 0 ];
+				expect( firstFilter ).to.equal( filter );
+			} );
+		} );
+	} );
+
+	describe( 'exec()', () => {
+		let data, filter;
+		beforeEach( () => {
+			data = Object.assign( {}, templateData );
+			filter = {
+				fullContent: true,
+				exec: data => {
+					data.content = 'Foo bar baz.';
+				}
+			};
+
+			normalizer.addFilter( filter );
+		} );
+
+		it( 'should apply filter#exec to data', () => {
+			normalizer.setInputData( data );
+
+			expect( data.content ).to.be.undefined;
+
+			normalizer.exec();
+
+			expect( data.content ).to.equal( 'Foo bar baz.' );
+		} );
+
+		it( 'should mark data as processed with paste from office', () => {
+			normalizer.setInputData( data );
+
+			expect( data.isTransformedWithPasteFromOffice ).to.be.false;
+
+			normalizer.exec();
+
+			expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+		} );
+
+		describe( 'already processed data', () => {
+			let execFake;
+			beforeEach( () => {
+				execFake = sinon.fake();
+				normalizer = new ContentNormalizer( { activationTrigger: () => true } );
+
+				normalizer.addFilter( {
+					fullContent: true,
+					exec: execFake
+				} );
+			} );
+
+			it( 'should not apply filter on already processed data', () => {
+				normalizer.setInputData( data );
+
+				sinon.assert.notCalled( execFake );
+				expect( data.isTransformedWithPasteFromOffice ).to.be.false;
+
+				normalizer.exec();
+				sinon.assert.calledOnce( execFake );
+				sinon.assert.calledWith( execFake, data );
+				expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+
+				normalizer.exec();
+				sinon.assert.calledOnce( execFake );
+			} );
+		} );
+
+		describe( 'normalizer without filter', () => {
+			beforeEach( () => {
+				normalizer = new ContentNormalizer( { activationTrigger: () => true } );
+			} );
+
+			it( 'should do nothing with data', () => {
+				normalizer.setInputData( data );
+
+				expect( data ).to.deep.include( templateData );
+				expect( data.isTransformedWithPasteFromOffice ).to.be.false;
+
+				normalizer.exec();
+
+				expect( data ).to.deep.include( templateData );
+				expect( data.isTransformedWithPasteFromOffice ).to.be.true;
+			} );
+		} );
+	} );
+} );