Skip to content

Commit

Permalink
Tests: refactor fixture reset tests to be more strict
Browse files Browse the repository at this point in the history
The prior tests were _only_ checking if the `innerHTML` matched our
expectations, and would not catch mutations to the element itself (e.g.
it being replaced with an element with a different tagName or its
attributes modified).
  • Loading branch information
rwjblue authored and trentmwillis committed Jan 17, 2018
1 parent 324632a commit 8dfc4ad
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions test/main/test.js
Expand Up @@ -19,8 +19,45 @@ if ( typeof document !== "undefined" ) {
values.unshift( originalValue );
} );

hooks.beforeEach( function( assert ) {
assert.fixtureEquals = function fixtureEquals( options ) {
var expectedTagName = options.tagName || "div";
var expectedAttributes = options.attributes || {};
var expectedContent = options.content || "";

var element = document.getElementById( "qunit-fixture" );

this.pushResult( {
result: element.tagName === expectedTagName.toUpperCase(),
actual: element.tagName.toLowerCase(),
expected: expectedTagName,
message: "tagName"
} );

var actualAttributes = {};

for ( var i = 0, l = element.attributes.length; i < l; i++ ) {
actualAttributes[ element.attributes[ i ].name ] = element.attributes[ i ].value;
}

this.deepEqual( actualAttributes, expectedAttributes, "attributes" );
this.strictEqual( element.innerHTML, expectedContent, "contents" );
};

assert.hasFailingAssertions = function() {
for ( var i = 0; i < this.test.assertions.length; i++ ) {
if ( !this.test.assertions[ i ].result ) {
return true;
}
}

return false;
};
} );

// Set QUnit.config.fixture for the next test, propagating failures to recover the sequence
hooks.afterEach( function( assert ) {
failure = failure || assert.hasFailingAssertions();
if ( failure ) {
assert.ok( false, "prior failure" );
QUnit.config.fixture = originalValue;
Expand All @@ -30,31 +67,40 @@ if ( typeof document !== "undefined" ) {
} );

QUnit.test( "setup", function( assert ) {
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
assert.equal( values.length, 3, "proper sequence" );
failure = failure || values.length !== 3;

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
} );

QUnit.test( "automatically reset", function( assert ) {
var contents = document.getElementById( "qunit-fixture" ).innerHTML;
assert.equal( contents, originalValue );
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: originalValue
} );
assert.equal( values.length, 2, "proper sequence" );
failure = failure || values.length !== 2 || contents !== originalValue;
} );

QUnit.test( "user-specified", function( assert ) {
var contents = document.getElementById( "qunit-fixture" ).innerHTML;
document.getElementById( "qunit-fixture" ).innerHTML = "baz";
assert.equal( contents, "<b>ar</b>" );
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "<b>ar</b>"
} );
assert.equal( values.length, 1, "proper sequence" );
failure = failure || values.length !== 1 || contents !== "<b>ar</b>";

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "baz";
} );

QUnit.test( "disabled", function( assert ) {
var contents = document.getElementById( "qunit-fixture" ).innerHTML;
assert.equal( contents, "baz" );
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "baz"
} );
assert.equal( values.length, 0, "proper sequence" );
failure = failure || values.length !== 0 || contents !== "baz";
} );
} );

Expand Down

0 comments on commit 8dfc4ad

Please sign in to comment.