Skip to content

Commit

Permalink
Merge pull request #19979 from code-dot-org/unit-test-cleanup
Browse files Browse the repository at this point in the history
Tests: Sandbox document.body.innerHTML in ExporterTest
  • Loading branch information
islemaster committed Jan 12, 2018
2 parents bc044b9 + bd2236c commit 5cb74a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 5cb74a6

Please sign in to comment.