Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Reporter: Ensure attributes on qunit-fixture are reset. #1250

Merged
merged 3 commits into from
Jan 17, 2018
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
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
125 changes: 110 additions & 15 deletions test/main/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,66 @@ if ( typeof document !== "undefined" ) {

QUnit.module( "fixture", function( hooks ) {
var failure = false,
values = [ /* initial value (see unshift below), */ "<b>ar</b>", undefined ],
values = [

/* initial value (see unshift below), */
/* initial value (see unshift below), */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused as to why this comment is duplicated.

Copy link
Contributor Author

@rwjblue rwjblue Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments represent items in the values array here, and we now push the original value twice (the two values.unshift calls below).

Basically, there are now two tests that want to confirm that the default fixture is re-applied (where before we only had one)...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, okay, now I understand. (Kind of a weird setup for these tests, though...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INDEED! It was massively confusing for a bit, I tried to leave them a little easier to grok for next time but I definitely agree it is tricky as heck...

"<b>ar</b>",
"<p>bc</p>",
undefined
],
originalValue;

hooks.before( function() {
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 ) {
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 +80,76 @@ if ( typeof document !== "undefined" ) {
} );

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

// setup for next test
document.getElementById( "qunit-fixture" ).innerHTML = "foo";
assert.equal( values.length, 3, "proper sequence" );
failure = failure || values.length !== 3;
} );

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

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

QUnit.test( "automatically reset after attribute value mutation", function( assert ) {
assert.fixtureEquals( {
tagName: "div",
attributes: { id: "qunit-fixture" },
content: originalValue.innerHTML
} );
assert.equal( values.length, 4, "proper sequence" );
} );

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

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

QUnit.test( "user-specified", function( assert ) {
var contents = document.getElementById( "qunit-fixture" ).innerHTML;
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";
assert.equal( contents, "<b>ar</b>" );
assert.equal( values.length, 1, "proper sequence" );
failure = failure || values.length !== 1 || contents !== "<b>ar</b>";
} );

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, 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" );
failure = failure || values.length !== 0 || contents !== "baz";
} );
} );

Expand Down