Skip to content

Assertion templates are not as immutable as expected #333

@rorticus

Description

@rorticus

It appears that any assertion template functions that take an array of nodes can be mutated by calling setProperty. For example, setChildren takes an array of DNode children. setProperty modifies the node directly, and since the nodes from setChildren do not get recreated (because they are in an array and not some kind of function that returns them) the property changes persist.

Package Version: 5.0.5
Code

Unfortunately, codesandbox is erroring w/ this code so I can't just post a link, but this should reproduce the issue.

import { tsx } from "@dojo/framework/widget-core/tsx";
import assertionTemplate from "@dojo/framework/testing/assertionTemplate";
import harness from "@dojo/framework/testing/harness";

const baseAssertionTemplate = assertionTemplate(() => <div key="root" />);

const childAssertionTemplate = baseAssertionTemplate.setChildren("@root", [
  <div key="child" title="original title" />
]);

const h = harness(() => (
  <div key="root">
    <div key="child" title="original title" />
  </div>
));
h.expect(childAssertionTemplate);

h.expect(childAssertionTemplate.setProperty('@child', 'title', 'something else'));

h.expect(childAssertionTemplate);

Expected behavior:

I would expect the first expect to succeed, the second to fail, the third to succeed.

Actual behavior:

The third expect fails because node in childAssertionTemplate has been mutated.

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