Skip to content

Commit

Permalink
Ensure assertion template children are immutable (#587)
Browse files Browse the repository at this point in the history
* Failing unit test for mutable children

* package-lock

* Ensure children are not able to be mutated when using the children and sibling apis
  • Loading branch information
agubler committed Nov 14, 2019
1 parent 4c2fff4 commit a777de5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions src/testing/assertionTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,19 @@ export function assertionTemplate(renderFunc: () => DNode | DNode[]) {
const render = renderFunc();
const node = findOne(render, selector);
node.children = node.children || [];
if (typeof children === 'function') {
children = children();
let childrenResult = children;
if (typeof childrenResult === 'function') {
childrenResult = childrenResult();
}
switch (type) {
case 'prepend':
node.children = [...children, ...node.children];
node.children = [...childrenResult, ...node.children];
break;
case 'append':
node.children = [...node.children, ...children];
node.children = [...node.children, ...childrenResult];
break;
case 'replace':
node.children = [...children];
node.children = [...childrenResult];
break;
}
return render;
Expand All @@ -156,7 +157,7 @@ export function assertionTemplate(renderFunc: () => DNode | DNode[]) {
}
return assertionTemplate(() => {
const render = renderFunc();
const insertedChildren = typeof children === 'function' ? (children = children()) : children;
const insertedChildren = typeof children === 'function' ? children() : children;
return replaceChildren(selector, render, (index, children) => {
if (type === 'after') {
children.splice(index + 1, 0, ...insertedChildren);
Expand Down
34 changes: 33 additions & 1 deletion tests/testing/unit/assertionTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { stub } from 'sinon';

import { harness } from '../../../src/testing/harness';
import { WidgetBase } from '../../../src/core/WidgetBase';
import { v, w, tsx } from '../../../src/core/vdom';
import { v, w, tsx, create } from '../../../src/core/vdom';
import assertionTemplate, { Ignore } from '../../../src/testing/assertionTemplate';

class MyWidget extends WidgetBase<{
Expand Down Expand Up @@ -190,6 +190,38 @@ describe('assertionTemplate', () => {
h.expect(childAssertion);
});

it('children set should be immutable', () => {
const factory = create();
const Widget = factory(function Widget() {
return (
<div key="parent" classes={['root']}>
<div key="child">hello</div>
</div>
);
});

const WidgetWithProps = factory(function Widget() {
return (
<div key="parent" classes={['root']}>
<div disabled={true} key="child">
hello
</div>
</div>
);
});

const baseAssertion = assertionTemplate(() => v('div', { key: 'parent', classes: ['root'] }, []));

const childAssertion = baseAssertion.setChildren('@parent', () => [<div key="child">hello</div>]);

const h = harness(() => w(Widget, {}));
const h1 = harness(() => w(WidgetWithProps, {}));
h.expect(childAssertion);
const propertyAssertion = childAssertion.setProperty('@child', 'disabled', true);
h1.expect(propertyAssertion);
h.expect(childAssertion);
});

it('can set a child with replace', () => {
const h = harness(() => w(MyWidget, { replaceChild: true }));
const childAssertion = baseAssertion.replaceChildren('~header', ['replace']);
Expand Down

0 comments on commit a777de5

Please sign in to comment.