8
0
Pārlūkot izejas kodu

Added a new test util which executes specified assertions.

Kamil Piechaczek 8 gadi atpakaļ
vecāks
revīzija
5a868417a1

+ 48 - 0
packages/ckeditor5-core/tests/_utils-tests/utils.js

@@ -0,0 +1,48 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+import testUtils from '../_utils/utils';
+
+testUtils.createSinonSandbox();
+
+describe( 'utils', () => {
+	describe( 'checkAssertions', () => {
+		it( 'does not throw an error if at least one assertion passed', () => {
+			const assertionRed = testUtils.sinon.stub().callsFake( () => {
+				expect( 1 ).to.equal( 2 );
+			} );
+			const assertionGreen = testUtils.sinon.stub().callsFake( () => {
+				expect( 1 ).to.equal( 1 );
+			} );
+
+			expect( () => {
+				testUtils.checkAssertions( assertionRed, assertionGreen );
+			} ).to.not.throw();
+		} );
+
+		it( 'throws all errors if any assertion did not pass', () => {
+			const assertionRed = testUtils.sinon.stub().callsFake( () => {
+				expect( 1 ).to.equal( 2 );
+			} );
+			const assertionGreen = testUtils.sinon.stub().callsFake( () => {
+				expect( 2 ).to.equal( 1 );
+			} );
+
+			expect( () => {
+				testUtils.checkAssertions( assertionRed, assertionGreen );
+			} ).to.throw( Error, 'expected 1 to equal 2\n\nexpected 2 to equal 1' );
+		} );
+
+		it( 'does not execute all assertions if the first one passed', () => {
+			const assertionRed = testUtils.sinon.stub().callsFake( () => {
+				expect( 1 ).to.equal( 1 );
+			} );
+			const assertionGreen = testUtils.sinon.stub().callsFake();
+
+			testUtils.checkAssertions( assertionRed, assertionGreen );
+			expect( assertionGreen.called ).to.equal( false );
+		} );
+	} );
+} );

+ 42 - 0
packages/ckeditor5-core/tests/_utils/utils.js

@@ -31,6 +31,48 @@ const utils = {
 		afterEach( () => {
 			utils.sinon.restore();
 		} );
+	},
+
+	/**
+	 * Executes specified assertions. It expects that at least one function will not throw an error.
+	 *
+	 * Some of the tests fail because different browsers renders selection differently when it comes to element boundaries.
+	 * Using this method we can check few scenarios.
+	 *
+	 * See https://github.com/ckeditor/ckeditor5-core/issues/107.
+	 *
+	 * Usage:
+	 *
+	 *      it( 'test', () => {
+	 *          // Test bootstrapping...
+	 *
+	 *          const assertEdge = () => {
+	 *              // expect();
+	 *          };
+	 *
+	 *          const assertAll = () => {
+	 *              // expect();
+	 *          };
+	 *
+	 *          testUtils.checkAssertions( assertEdge, assertAll );
+	 *      } );
+	 *
+	 * @param {Array.<Function>} assertions Functions that will be executed.
+	 */
+	checkAssertions( ...assertions ) {
+		const errors = [];
+
+		for ( const assertFn of assertions ) {
+			try {
+				assertFn();
+
+				return;
+			} catch ( err ) {
+				errors.push( err.message );
+			}
+		}
+
+		throw new Error( errors.join( '\n\n' ) );
 	}
 };