Skip to content

Handle non-unique elements in Mutation Observer (jest only?) #186

@JRJurman

Description

@JRJurman

Summary

While writing the tests for #185, it was found that sometimes the mutation observer will get a duplicate of an element (usually when it is now catching up to multiple events that happened in the app).

I believe this is something that only happens in tests, and possible a side-effect of how jest / jsdom / testing-library work.

If that isn't the case, we should update the logic here:

new MutationObserver((mutationList) => {
// cleanup orphaned nodes that are no longer on the DOM
const removedNodesInMutation = (mutation: MutationRecord) => [...mutation.removedNodes];
const removedNodes = mutationList.flatMap(removedNodesInMutation).flatMap(childrenComponents);
removedNodes.forEach(clearNode);
// call new effects on any new nodes
const addedNodesInMutation = (mutation: MutationRecord) => [...mutation.addedNodes];
const newNodes = mutationList.flatMap(addedNodesInMutation).flatMap(childrenComponents);
newNodes.forEach(processEffects);
});

We should see this happening in apps after #185 and we see useStores hooks not cleaning up when they have been removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions