Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Added a new test util which executes specified assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Nov 14, 2017
1 parent 68c5152 commit 6d33f78
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/_utils-tests/utils.js
Original file line number Diff line number Diff line change
@@ -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 assertion 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 changes: 42 additions & 0 deletions tests/_utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Function 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' ) );
}
};

Expand Down

0 comments on commit 6d33f78

Please sign in to comment.