Explorar el Código

Introduced bender.tools.

Piotrek Koszuliński hace 10 años
padre
commit
58f3af61c0

+ 2 - 1
dev/bender/plugins/ckeditor5/lib/index.js

@@ -9,7 +9,8 @@
 
 var path = require( 'path' );
 var files = [
-	path.join( __dirname, '../static/extensions.js' )
+	path.join( __dirname, '../static/extensions.js' ),
+	path.join( __dirname, '../static/tools.js' )
 ];
 
 module.exports = {

+ 51 - 0
dev/bender/plugins/ckeditor5/static/tools.js

@@ -0,0 +1,51 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals bender, before, afterEach, sinon */
+
+'use strict';
+
+( function() {
+	/**
+	 * Test tools for CKEditor.
+	 *
+	 * This is a main namespace for the test tools.
+	 *
+	 * * General tools go directly to `bender.tools.*`.
+	 * * Core tools (introduced by `ckeditor5-core`) go to `bender.tools.core.*.
+	 * * Plugin tools (introduced by plugins) go to `bender.tools.plugin.<plugin-name>.*`.
+	 *
+	 * Tools for specific plugins or the core should be kept in `tests/_tools/tools.js` file
+	 * of the respective repository. They can be loaded using Bender's `bender-include` directive.
+	 * Their tests should be kept in `tests/bender/*` directory.
+	 */
+	bender.tools = {
+		/**
+		 * Creates Sinon sandbox in {@link bender#sinon} and plugs `afterEach()` callback which
+		 * restores all spies and stubs created in this sandbox.
+		 *
+		 * See https://github.com/ckeditor/ckeditor5-design/issues/72 and http://sinonjs.org/docs/#sinon-sandbox
+		 *
+		 * Usage:
+		 *
+		 *		// Directly in the test file:
+		 *		bender.tools.createSinonSandbox();
+		 *
+		 *		// Then inside tests you can use bender.sinon:
+		 *		it( 'does something', function() {
+		 *			bender.sinon.spy( obj, 'method' );
+		 *		} );
+		 */
+		createSinonSandbox() {
+			before( function() {
+				bender.sinon = sinon.sandbox.create();
+			} );
+
+			afterEach( function() {
+				bender.sinon.restore();
+			} );
+		}
+	};
+} )();

+ 35 - 0
tests/bender/createsinonsandbox.js

@@ -0,0 +1,35 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+var obj = {
+	method() {}
+};
+var spy;
+var origMethod = obj.method;
+
+bender.tools.createSinonSandbox();
+
+describe( 'bender.tools.createSinonSandbox()', function() {
+	it( 'creates a sandbox', function() {
+		expect( bender.sinon ).to.be.an( 'object' );
+		expect( bender.sinon ).to.have.property( 'spy' );
+	} );
+
+	// This test is needed for the following one.
+	it( 'really works', function() {
+		spy = bender.sinon.spy( obj, 'method' );
+
+		expect( obj ).to.have.property( 'method', spy );
+	} );
+
+	it( 'restores spies after each test', function() {
+		obj.method();
+
+		sinon.assert.notCalled( spy );
+		expect( obj ).to.have.property( 'method', origMethod );
+	} );
+} );