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 11, 2018
1 parent c2a55bd commit aec00d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
15 changes: 12 additions & 3 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,17 @@ import { window, document } from "../src/globals";
}

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

// support user defined values for `config.fixture`
var newFixture = document.createElement( "div" );
newFixture.setAttribute( "id", "qunit-fixture" );
newFixture.innerHTML = config.fixture;
fixture.parentNode.replaceChild( newFixture, fixture );
} else {
const clonedFixture = config.fixture.cloneNode( true );
fixture.parentNode.replaceChild( clonedFixture, fixture );
}
}

Expand Down
44 changes: 37 additions & 7 deletions test/main/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if ( typeof document !== "undefined" ) {
/* initial value (see unshift below), */
/* initial value (see unshift below), */
"<b>ar</b>",
"<p>bc</p>",
undefined
],
originalValue;
Expand All @@ -24,6 +25,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 +80,7 @@ if ( typeof document !== "undefined" ) {
} );

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

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
Expand All @@ -84,9 +90,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, 5, "proper sequence" );

// setup for next test
document.getElementById( "qunit-fixture" ).setAttribute( "data-foo", "blah" );
Expand All @@ -96,18 +102,30 @@ 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, 4, "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, 3, "proper sequence" );

// setup for next test
document.getElementById( "qunit-fixture" ).setAttribute( "data-foo", "blah" );
} );

QUnit.test( "user-specified string automatically resets attribute value mutation", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: "<p>bc</p>"
} );
assert.equal( values.length, 2, "proper sequence" );

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "baz";
Expand All @@ -119,6 +137,18 @@ if ( typeof document !== "undefined" ) {
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 aec00d5

Please sign in to comment.