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

Testing morphdom with jsdom is tricky. #256

Open
ioquatix opened this issue May 3, 2024 · 2 comments
Open

Testing morphdom with jsdom is tricky. #256

ioquatix opened this issue May 3, 2024 · 2 comments

Comments

@ioquatix
Copy link

ioquatix commented May 3, 2024

Hi, thanks for creating morphdom.

It looks like testing morphdom with jsdom is quite tricky, e.g. https://runkit.com/autosponge/morphdom-jsdom-example

Having to set up a global document before invoking morphdom is tricky when there are lots of tests which might use different doms.

Might I propose adding an option, e.g. morphdom(..., ..., document: dom.window.document) or something similar that allows us to inject this into the individual calls? Alternatively, maybe we can extract it from the first argument?

I see some additional discussion here: #98

Another idea is to introduce a class which contains the state.

e.g.

morphdom = new Morphdom(document);
morphdom.morph(...)

In my own code, I have something like this:

  constructor(window) {
    this.window = window;
    this.document = window.document;

This makes testing using jsdom extremely easy.

@ioquatix
Copy link
Author

ioquatix commented May 3, 2024

I found one way to work around this is pre-constructing the fragment, rather than providing HTML:

	createDocumentFragment(html) {
		return this.document.createRange().createContextualFragment(html);
	}
	
	update(id, html, options) {
		let element = this.document.getElementById(id);
		let fragment = this.createDocumentFragment(html);
		
		morphdom(element, fragment);

This avoids morphdom having to interact with doc internally. Not sure if there are any performance problems with this approach?

@ioquatix
Copy link
Author

ioquatix commented May 4, 2024

Unfortunately that approach doesn't always work, sometimes createElementNS is invoked regardless.

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

No branches or pull requests

1 participant