Skip to content

[Refactor tooltip test] Add DOM testing utility - #3791

Closed
ajduberstein wants to merge 5 commits into
masterfrom
ajd/refactor-jupyter-widget-dom-tests-1
Closed

ajduberstein wants to merge 5 commits into
masterfrom
ajd/refactor-jupyter-widget-dom-tests-1

Conversation

@ajduberstein

@ajduberstein ajduberstein commented Oct 16, 2019 •

Copy link
Copy Markdown
Collaborator

Supports increasing the test coverage on @deck.gl/jupyter-widget, where
many components of the module assume a global document and a canvas have
already been created.

See #3792 for usage

Change List

  • Create a helper class for document testing

@coveralls

coveralls commented Oct 16, 2019 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+2.6%) to 82.975% when pulling 49739ee on ajd/refactor-jupyter-widget-dom-tests-1 into 429218a on master.

Comment thread test/utils/document.js Outdated
canvasParent.appendChild(canvas);
this.canvasParent = canvasParent;
this.document.body.appendChild(canvasParent);
this.testObject = applyConstructorOrFunction(this.canvasCallback, canvas);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does this do? Just call callback(canvas).

Comment thread test/utils/document.js Outdated

export default class DocumentTest {
// Simplify the testing of deck.gl work that requires a document and canvas
constructor(canvasCallback) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

callback should be supplied to the method where it is used, in this case setup.

Comment thread test/utils/document.js Outdated
return documentClone;
}

setup() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This method implies that setup can be called multiple times while in reality the class only support being setup once. You should call it in the constructor in this case.

Comment thread test/utils/document.js Outdated
this.testObject = applyConstructorOrFunction(this.canvasCallback, canvas);
}

teardown() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

finalize

@ajduberstein
ajduberstein force-pushed the ajd/refactor-jupyter-widget-dom-tests-1 branch from 83f2601 to f4bd47c Compare November 12, 2019 07:35
Comment thread test/utils/document.js Outdated
const dom = new JSDOM(`<!DOCTYPE html>`);
return dom.window.document;
}
const documentClone = window.document.cloneNode(true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do you need to clone the document? It may have heavy content during browser test.

Comment thread test/utils/document.js Outdated
@@ -0,0 +1,41 @@
/* global window */

export default class DocumentTest {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Add JSDoc for this class.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The class name DocumentTest does not communicate what it does, e.g. the creation of a canvas. When you write tests using this class, it will be difficult for other maintainers to understand a) what the callback receives b) what the callback is supposed to return, from the code new DocumentTest(...).

Comment thread test/utils/document.js Outdated
}

finalize() {
this.testObject.remove();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This class has no control over the returned value of the user callback. Assuming testObject is a DOM element here is dangerous. There is no apparent reason why testObject has to be stored in this class. It will be a lot easier to read if you write your test like this

const testCanvas = new TestCanvas();
const myElement = setup(testCanvas.canvas);
// ...
myElement.remove();
testCanvas.finalize();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Just updated the PR, the class has been cut in favor of smaller functions and an API that looks more like the one here.

Comment thread test/utils/canvas-test.js
if (typeof window === undefined || !window.document) {
const {JSDOM} = require('jsdom');
const dom = new JSDOM(`<!DOCTYPE html>`);
documentClone = dom.window.document.cloneNode(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This node is brand new - why clone it?

Comment thread test/utils/canvas-test.js
const body = window.document.createElement('body');
html.appendChild(body);
documentClone.appendChild(html);
documentClone.body = body;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What is the purpose of this blob? Why don't you just use document.body?

Comment thread test/utils/canvas-test.js
const dom = new JSDOM(`<!DOCTYPE html>`);
documentClone = dom.window.document.cloneNode(false);
} else {
documentClone = window.document.cloneNode(false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you cannot attach test elements to the current document?

Comment thread test/utils/canvas-test.js
}

/* Create a clean canvas, its parent, and a document node for testing */
export default function setup() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

setupTestCanvas for clarity

@ajduberstein
ajduberstein force-pushed the ajd/refactor-jupyter-widget-dom-tests-1 branch from dac0ad9 to 49739ee Compare November 14, 2019 19:47
@ajduberstein

Copy link
Copy Markdown
Collaborator Author

Closed in favor of #3792

@ajduberstein
ajduberstein deleted the ajd/refactor-jupyter-widget-dom-tests-1 branch December 9, 2019 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants