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

T/107: A test util for checking multi assertions for different browsers #108

Merged
merged 6 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 assertions if the first one passed', () => {
const assertionRed = testUtils.sinon.stub().callsFake( () => {
expect( 1 ).to.equal( 1 );
} );
const assertionGreen = testUtils.sinon.stub();

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 {...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' ) );
}
};

Expand Down