-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #733 from ckeditor/ck/9668
Feature (tests): Created the `equalMarkup` and `attribute` chai assertions. They are loaded automatically when running tests. Closes ckeditor/ckeditor5#9668.
- Loading branch information
Showing
8 changed files
with
616 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/ckeditor5-dev-tests/lib/utils/automated-tests/assertions/attribute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* Factory function that registers the `attribute` assertion. | ||
* | ||
* @param {Chai} chai | ||
*/ | ||
module.exports = chai => { | ||
/** | ||
* Asserts that the target has an attribute with the given key name. | ||
* | ||
* expect( selection ).to.have.attribute( 'linkHref' ); | ||
* | ||
* When `value` is provided, `.attribute` also asserts that the attribute's value is equal to the given `value`. | ||
* | ||
* expect( selection ).to.have.attribute( 'linkHref', 'example.com' ); | ||
* | ||
* Negations works as well. | ||
* | ||
* @param {String} key Key of attribute to assert. | ||
* @param {String} [value] Attribute value to assert. | ||
*/ | ||
chai.Assertion.addMethod( 'attribute', function attributeAssertion( key, value ) { | ||
const obj = this._obj; | ||
|
||
if ( arguments.length === 1 ) { | ||
// Check if it has the method at all. | ||
new chai.Assertion( obj ).to.respondTo( 'hasAttribute' ); | ||
|
||
// Check if it has the attribute. | ||
const hasAttribute = obj.hasAttribute( key ); | ||
this.assert( | ||
hasAttribute === true, | ||
`expected #{this} to have attribute '${ key }'`, | ||
`expected #{this} to not have attribute '${ key }'`, | ||
!chai.util.flag( this, 'negate' ), | ||
hasAttribute | ||
); | ||
} | ||
|
||
// If a value was given. | ||
if ( arguments.length >= 2 ) { | ||
// Check if it has the method at all. | ||
new chai.Assertion( obj ).to.respondTo( 'getAttribute' ); | ||
|
||
const attributeValue = obj.getAttribute( key ); | ||
this.assert( | ||
attributeValue === value, | ||
`expected #{this} to have attribute '${ key }' of #{exp}, but got #{act}`, | ||
`expected #{this} to not have attribute '${ key }' of #{exp}`, | ||
value, | ||
attributeValue | ||
); | ||
} | ||
} ); | ||
}; |
57 changes: 57 additions & 0 deletions
57
packages/ckeditor5-dev-tests/lib/utils/automated-tests/assertions/equal-markup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
const AssertionError = require( 'assertion-error' ); | ||
const { html_beautify: beautify } = require( 'js-beautify/js/lib/beautify-html' ); | ||
|
||
/** | ||
* Factory function that registers the `equalMarkup` assertion. | ||
* | ||
* @param {Chai} chai | ||
*/ | ||
module.exports = chai => { | ||
/** | ||
* Custom assertion that tests whether two given strings containing markup language are equal. | ||
* Unlike `expect().to.equal()` form Chai assertion library, this assertion formats the markup before showing a diff. | ||
* | ||
* This assertion can be used to test HTML strings and string containing serialized model. | ||
* | ||
* // Will throw an assertion error. | ||
* expect( | ||
* '<paragraph>foo bXXX[]r baz</paragraph>' | ||
* ).to.equalMarkup( | ||
* '<paragraph>foo bYYY[]r baz</paragraph>' | ||
* ); | ||
* | ||
* @param {String} expected Markup to compare. | ||
*/ | ||
chai.Assertion.addMethod( 'equalMarkup', function( expected ) { | ||
const actual = this._obj; | ||
const message = 'Expected markup strings to be equal'; | ||
|
||
if ( actual !== expected ) { | ||
throw new AssertionError( message, { | ||
actual: formatMarkup( actual ), | ||
expected: formatMarkup( expected ), | ||
showDiff: true | ||
} ); | ||
} | ||
} ); | ||
}; | ||
|
||
// Renames the $text occurrences as it is not properly formatted by the beautifier - it is tread as a block. | ||
const TEXT_TAG_PLACEHOLDER = 'span data-cke="true"'; | ||
const TEXT_TAG_PLACEHOLDER_REGEXP = new RegExp( TEXT_TAG_PLACEHOLDER, 'g' ); | ||
|
||
function formatMarkup( string ) { | ||
const htmlSafeString = string.replace( /\$text/g, TEXT_TAG_PLACEHOLDER ); | ||
|
||
const beautifiedMarkup = beautify( htmlSafeString, { | ||
indent_size: 2, | ||
space_in_empty_paren: true | ||
} ); | ||
|
||
return beautifiedMarkup.replace( TEXT_TAG_PLACEHOLDER_REGEXP, '$text' ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.