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

Tests: Sandbox document.body.innerHTML in ExporterTest #19979

Merged
merged 3 commits into from
Jan 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/test/unit/applab/ExporterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ describe('The Exporter,', function () {
}

describe("Regression tests", () => {
testUtils.sandboxDocumentBody();

it("should allow screens to be switched programmatically", (done) => {
runExportedApp(
`console.log("before switch"); setScreen("screen2"); console.log("after switch");`,
Expand Down
21 changes: 21 additions & 0 deletions apps/test/util/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,27 @@ export function restoreOnWindow(key) {
delete originalWindowValues[key];
}

/**
* Saves the contents of `document.body.innerHTML` before each test, and
* restores those contents after the test, allowing tests to do (almost)
* whatever they want with elements in the document body without worrying
* about cleanup.
*
* This is a big hammer and should be used sparingly - if you can write
* components that do precise setup and tear-down, you should. In a few cases,
* though, this is the quickest path to test coverage.
*
* Warning: This can cause issues with event handlers. Handlers attached
* to elements within the body will go away because their targets go away,
* but handlers attached to body, document, or window will persist - and may
* depend on DOM that gets removed during cleanup.
*/
export function sandboxDocumentBody() {
let originalDocumentBody;
beforeEach(() => originalDocumentBody = document.body.innerHTML);
afterEach(() => document.body.innerHTML = originalDocumentBody);
}

/**
* Track whenever we create a timeout/interval, and then clear all timeouts/intervals
* upon completion of each test.
Expand Down