Parcourir la source

Merge branch t/ckeditor5/1144

Tests: Properly use sinon sandbox in tests t/ckeditor5/1144.
Oskar Wróbel il y a 7 ans
Parent
commit
b2b16b24b2

+ 2 - 2
packages/ckeditor5-core/tests/_utils-tests/classictesteditor.js

@@ -22,11 +22,11 @@ import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import testUtils from '../../tests/_utils/utils';
 
-testUtils.createSinonSandbox();
-
 describe( 'ClassicTestEditor', () => {
 	let editorElement;
 
+	testUtils.createSinonSandbox();
+
 	beforeEach( () => {
 		editorElement = document.createElement( 'div' );
 		document.body.appendChild( editorElement );

+ 2 - 2
packages/ckeditor5-core/tests/_utils-tests/createsinonsandbox.js

@@ -11,9 +11,9 @@ const obj = {
 const origMethod = obj.method;
 let spy;
 
-testUtils.createSinonSandbox();
-
 describe( 'testUtils.createSinonSandbox()', () => {
+	testUtils.createSinonSandbox();
+
 	it( 'creates a sandbox', () => {
 		expect( testUtils.sinon ).to.be.an( 'object' );
 		expect( testUtils.sinon ).to.have.property( 'spy' );

+ 2 - 2
packages/ckeditor5-core/tests/_utils-tests/modeltesteditor.js

@@ -15,9 +15,9 @@ import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model
 
 import testUtils from '../../tests/_utils/utils';
 
-testUtils.createSinonSandbox();
-
 describe( 'ModelTestEditor', () => {
+	testUtils.createSinonSandbox();
+
 	describe( 'constructor()', () => {
 		it( 'creates an instance of editor', () => {
 			const editor = new ModelTestEditor( { foo: 1 } );

+ 2 - 2
packages/ckeditor5-core/tests/_utils-tests/utils.js

@@ -6,9 +6,9 @@
 import testUtils from '../_utils/utils';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 
-testUtils.createSinonSandbox();
-
 describe( 'utils', () => {
+	testUtils.createSinonSandbox();
+
 	describe( 'checkAssertions()', () => {
 		it( 'does not throw an error if at least one assertion passed', () => {
 			const assertionRed = testUtils.sinon.stub().callsFake( () => {

+ 2 - 2
packages/ckeditor5-core/tests/_utils-tests/virtualtesteditor.js

@@ -12,9 +12,9 @@ import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';
 
 import testUtils from '../../tests/_utils/utils';
 
-testUtils.createSinonSandbox();
-
 describe( 'VirtualTestEditor', () => {
+	testUtils.createSinonSandbox();
+
 	describe( 'constructor()', () => {
 		it( 'creates an instance of editor', () => {
 			const editor = new VirtualTestEditor( { foo: 1 } );

+ 14 - 7
packages/ckeditor5-core/tests/_utils/utils.js

@@ -8,20 +8,27 @@
  */
 const utils = {
 	/**
-	 * Creates Sinon sandbox in {@link bender#sinon} and plugs `afterEach()` callback which
+	 * Creates Sinon sandbox in {@link utils#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:
-	 *		testUtils.createSinonSandbox();
+	 *		import testUtils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
 	 *
-	 *		// Then inside tests you can use bender.sinon:
-	 *		it( 'does something', () => {
-	 *			testUtils.sinon.spy( obj, 'method' );
-	 *		} );
+	 *		describe( 'MyClass', () => {
+	 *			// Create Sinon sandbox inside top-level describe block:
+	 *			testUtils.createSinonSandbox();
+	 *
+	 *			// Then inside tests you can use testUtils.sinon:
+	 *			it( 'does something', () => {
+	 *				testUtils.sinon.spy( obj, 'method' );
+	 *			} );
+	 *		}
+	 *
+	 * **Note**: Do not use `testUtils.createSinonSandbox()` outside `describe()` block as it will attach `afterEach()` calls
+	 * to all test - not only those in current file.
 	 */
 	createSinonSandbox() {
 		before( () => {

+ 2 - 2
packages/ckeditor5-core/tests/editor/editorui.js

@@ -12,11 +12,11 @@ import View from '@ckeditor/ckeditor5-ui/src/view';
 
 import testUtils from '../_utils/utils';
 
-testUtils.createSinonSandbox();
-
 describe( 'EditorUI', () => {
 	let editor, view, ui;
 
+	testUtils.createSinonSandbox();
+
 	beforeEach( () => {
 		editor = new Editor();
 		view = new View();

+ 35 - 35
packages/ckeditor5-core/tests/plugincollection.js

@@ -18,42 +18,42 @@ class TestError extends Error {}
 class ChildPlugin extends Plugin {}
 class GrandPlugin extends ChildPlugin {}
 
-testUtils.createSinonSandbox();
-
-before( () => {
-	PluginA = createPlugin( 'A' );
-	PluginB = createPlugin( 'B' );
-	PluginC = createPlugin( 'C' );
-	PluginD = createPlugin( 'D' );
-	PluginE = createPlugin( 'E' );
-	PluginF = createPlugin( 'F' );
-	PluginG = createPlugin( 'G', GrandPlugin );
-	PluginH = createPlugin( 'H' );
-	PluginI = createPlugin( 'I' );
-	PluginJ = createPlugin( 'J' );
-	PluginK = createPlugin( 'K' );
-	PluginX = class extends Plugin {
-		constructor( editor ) {
-			super( editor );
-
-			throw new TestError( 'Some error inside a plugin' );
-		}
-	};
-	PluginFoo = createPlugin( 'Foo' );
-	AnotherPluginFoo = createPlugin( 'Foo' );
-
-	PluginC.requires = [ PluginB ];
-	PluginD.requires = [ PluginA, PluginC ];
-	PluginF.requires = [ PluginE ];
-	PluginE.requires = [ PluginF ];
-	PluginH.requires = [ PluginI ];
-	PluginJ.requires = [ 'K' ];
-	PluginK.requires = [ PluginA ];
-
-	editor = new Editor();
-} );
-
 describe( 'PluginCollection', () => {
+	testUtils.createSinonSandbox();
+
+	before( () => {
+		PluginA = createPlugin( 'A' );
+		PluginB = createPlugin( 'B' );
+		PluginC = createPlugin( 'C' );
+		PluginD = createPlugin( 'D' );
+		PluginE = createPlugin( 'E' );
+		PluginF = createPlugin( 'F' );
+		PluginG = createPlugin( 'G', GrandPlugin );
+		PluginH = createPlugin( 'H' );
+		PluginI = createPlugin( 'I' );
+		PluginJ = createPlugin( 'J' );
+		PluginK = createPlugin( 'K' );
+		PluginX = class extends Plugin {
+			constructor( editor ) {
+				super( editor );
+
+				throw new TestError( 'Some error inside a plugin' );
+			}
+		};
+		PluginFoo = createPlugin( 'Foo' );
+		AnotherPluginFoo = createPlugin( 'Foo' );
+
+		PluginC.requires = [ PluginB ];
+		PluginD.requires = [ PluginA, PluginC ];
+		PluginF.requires = [ PluginE ];
+		PluginE.requires = [ PluginF ];
+		PluginH.requires = [ PluginI ];
+		PluginJ.requires = [ 'K' ];
+		PluginK.requires = [ PluginA ];
+
+		editor = new Editor();
+	} );
+
 	beforeEach( () => {
 		availablePlugins = [
 			PluginA,