Skip to content

Commit

Permalink
HTML Reporter: Ensure attributes on qunit-fixture are reset.
Browse files Browse the repository at this point in the history
This change leverages `Node.prototype.cloneNode` to deeply clone the
original `qunit-fixture` before any tests have started, then for each
test replaces (using `Node.prototype.replaceChild`) the current
`qunit-fixture` with a clone of the original.
  • Loading branch information
rwjblue committed Jan 10, 2018
1 parent c2a55bd commit f60890e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
10 changes: 8 additions & 2 deletions runner/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { window, document } from "../src/globals";

var fixture = document.getElementById( "qunit-fixture" );
if ( fixture ) {
config.fixture = fixture.innerHTML;
config.fixture = fixture.cloneNode( true );
}
}

Expand All @@ -33,8 +33,14 @@ import { window, document } from "../src/globals";
}

var fixture = document.getElementById( "qunit-fixture" );
if ( fixture ) {
var resetFixtureType = typeof config.fixture;
if ( resetFixtureType === "string" ) {

// support user defined values for `config.fixture`
fixture.innerHTML = config.fixture;
} else {
const clonedFixture = config.fixture.cloneNode( true );
fixture.parentNode.replaceChild( clonedFixture, fixture );
}
}

Expand Down
32 changes: 24 additions & 8 deletions test/main/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ if ( typeof document !== "undefined" ) {
originalValue = QUnit.config.fixture;
values.unshift( originalValue );
values.unshift( originalValue );

var customFixtureNode = document.createElement( "span" );
customFixtureNode.setAttribute( "id", "qunit-fixture" );
customFixtureNode.setAttribute( "data-baz", "huzzah!" );
values.push( customFixtureNode );
} );

hooks.beforeEach( function( assert ) {
Expand Down Expand Up @@ -74,7 +79,7 @@ if ( typeof document !== "undefined" ) {
} );

QUnit.test( "setup", function( assert ) {
assert.equal( values.length, 4, "proper sequence" );
assert.equal( values.length, 5, "proper sequence" );

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
Expand All @@ -84,9 +89,9 @@ if ( typeof document !== "undefined" ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: originalValue
content: originalValue.innerHTML
} );
assert.equal( values.length, 3, "proper sequence" );
assert.equal( values.length, 4, "proper sequence" );

// setup for next test
document.getElementById( "qunit-fixture" ).setAttribute( "data-foo", "blah" );
Expand All @@ -96,29 +101,40 @@ if ( typeof document !== "undefined" ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: originalValue
content: originalValue.innerHTML
} );
assert.equal( values.length, 2, "proper sequence" );
assert.equal( values.length, 3, "proper sequence" );
} );

QUnit.test( "user-specified", function( assert ) {
QUnit.test( "user-specified string", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "<b>ar</b>"
} );
assert.equal( values.length, 1, "proper sequence" );
assert.equal( values.length, 2, "proper sequence" );

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

QUnit.test( "disabled", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "baz"
} );
assert.equal( values.length, 1, "proper sequence" );
} );

QUnit.test( "user-specified DOM node", function( assert ) {
assert.fixtureEquals( {
tagName: "span",
attributes: {
id: "qunit-fixture",
"data-baz": "huzzah!"
},
content: ""
} );
assert.equal( values.length, 0, "proper sequence" );
} );
} );
Expand Down

0 comments on commit f60890e

Please sign in to comment.