Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c0d3bf

Browse files
authored
Add insertChildren to assertion template (#244)
* add insert to assertion template * rename to insertChildre, update readme
1 parent a7544d0 commit 1c0d3bf

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

src/testing/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Here we're using the `setChildren()` api on the baseAssertion, and we're using t
320320
Assertion Template has the following api's:
321321
322322
```
323+
insertChildren(selector: string, children: DNode[], type?: 'before' | 'after'): AssertionTemplateResult;
323324
setChildren(selector: string, children: DNode[], type?: 'prepend' | 'replace' | 'append'): AssertionTemplateResult;
324325
setProperty(selector: string, property: string, value: any): AssertionTemplateResult;
325326
getChildren(selector: string): DNode[];

src/testing/assertionTemplate.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { VNode, WNode, DNode } from '../widget-core/interfaces';
44

55
export interface AssertionTemplateResult {
66
(): DNode | DNode[];
7+
insertChildren(selector: string, children: DNode[], type?: 'before' | 'after'): AssertionTemplateResult;
78
setChildren(selector: string, children: DNode[], type?: 'prepend' | 'replace' | 'append'): AssertionTemplateResult;
89
setProperty(selector: string, property: string, value: any): AssertionTemplateResult;
910
getChildren(selector: string): DNode[];
@@ -73,6 +74,28 @@ export function assertionTemplate(renderFunc: () => DNode | DNode[]) {
7374
}
7475
return assertionTemplate(() => render);
7576
};
77+
assertionTemplateResult.insertChildren = (
78+
selector: string,
79+
children: DNode[],
80+
type: 'before' | 'after' = 'after'
81+
) => {
82+
const render = renderFunc();
83+
const node = guard(findOne(render, selector));
84+
const parent = (node as any).parent;
85+
const index = parent.children.indexOf(node);
86+
let newChildren = [...parent.children];
87+
switch (type) {
88+
case 'before':
89+
newChildren.splice(index, 0, ...children);
90+
parent.children = newChildren;
91+
break;
92+
case 'after':
93+
newChildren.splice(index + 1, 0, ...children);
94+
parent.children = newChildren;
95+
break;
96+
}
97+
return assertionTemplate(() => render);
98+
};
7699
assertionTemplateResult.getProperty = (selector: string, property: string) => {
77100
const render = renderFunc();
78101
const node = guard(findOne(render, selector));

src/testing/harness.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import assertRender from './support/assertRender';
2-
import { select } from './support/selector';
2+
import { decorateNodes, select } from './support/selector';
33
import { WNode, DNode, WidgetBaseInterface, Constructor, VNode } from '../widget-core/interfaces';
44
import { WidgetBase } from '../widget-core/WidgetBase';
5-
import { decorate, isVNode, isWNode } from '../widget-core/d';
65

76
export interface CustomComparator {
87
selector: string;
@@ -14,11 +13,6 @@ export interface FunctionalSelector {
1413
(node: VNode | WNode): undefined | Function;
1514
}
1615

17-
export interface DecoratorResult<T> {
18-
hasDeferredProperties: boolean;
19-
nodes: T;
20-
}
21-
2216
export interface ExpectedRender {
2317
(): DNode | DNode[];
2418
}
@@ -49,26 +43,6 @@ export interface HarnessAPI {
4943
getRender: GetRender;
5044
}
5145

52-
function decorateNodes(dNode: DNode[]): DecoratorResult<DNode[]>;
53-
function decorateNodes(dNode: DNode): DecoratorResult<DNode>;
54-
function decorateNodes(dNode: DNode | DNode[]): DecoratorResult<DNode | DNode[]>;
55-
function decorateNodes(dNode: any): DecoratorResult<DNode | DNode[]> {
56-
let hasDeferredProperties = false;
57-
function addParent(parent: WNode | VNode): void {
58-
(parent.children || []).forEach((child) => {
59-
if (isVNode(child) || isWNode(child)) {
60-
(child as any).parent = parent;
61-
}
62-
});
63-
if (isVNode(parent) && typeof parent.deferredPropertiesCallback === 'function') {
64-
hasDeferredProperties = true;
65-
parent.properties = { ...parent.properties, ...parent.deferredPropertiesCallback(false) };
66-
}
67-
}
68-
const nodes = decorate(dNode, addParent, (node: DNode): node is WNode | VNode => isWNode(node) || isVNode(node));
69-
return { hasDeferredProperties, nodes };
70-
}
71-
7246
export function harness(
7347
renderFunc: () => WNode<WidgetBaseInterface>,
7448
customComparator: CustomComparator[] = []

src/testing/support/selector.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
import { DNode, DefaultWidgetBaseInterface, WNode, VNode } from '../../widget-core/interfaces';
2-
import { isVNode, isWNode } from '../../widget-core/d';
2+
import { decorate, isVNode, isWNode } from '../../widget-core/d';
33
import * as cssSelect from 'css-select-umd';
44

55
export type TestFunction = (elem: DNode<DefaultWidgetBaseInterface>) => boolean;
66

7+
export interface DecoratorResult<T> {
8+
hasDeferredProperties: boolean;
9+
nodes: T;
10+
}
11+
12+
export function decorateNodes(dNode: DNode[]): DecoratorResult<DNode[]>;
13+
export function decorateNodes(dNode: DNode): DecoratorResult<DNode>;
14+
export function decorateNodes(dNode: DNode | DNode[]): DecoratorResult<DNode | DNode[]>;
15+
export function decorateNodes(dNode: any): DecoratorResult<DNode | DNode[]> {
16+
let hasDeferredProperties = false;
17+
function addParent(parent: WNode | VNode): void {
18+
(parent.children || []).forEach((child) => {
19+
if (isVNode(child) || isWNode(child)) {
20+
(child as any).parent = parent;
21+
}
22+
});
23+
if (isVNode(parent) && typeof parent.deferredPropertiesCallback === 'function') {
24+
hasDeferredProperties = true;
25+
parent.properties = { ...parent.properties, ...parent.deferredPropertiesCallback(false) };
26+
}
27+
}
28+
const nodes = decorate(dNode, addParent, (node: DNode): node is WNode | VNode => isWNode(node) || isVNode(node));
29+
return { hasDeferredProperties, nodes };
30+
}
31+
732
export const parseSelector = (selector: string) => {
833
const selectors = selector.split(' ');
934
return selectors
@@ -102,6 +127,7 @@ export const adapter: any = {
102127
};
103128

104129
export function select(selector: string, nodes: DNode | DNode[]): (WNode | VNode)[] {
130+
nodes = decorateNodes(nodes).nodes;
105131
nodes = Array.isArray(nodes) ? nodes : [nodes];
106132
selector = parseSelector(selector);
107133
return cssSelect(selector, nodes, { adapter }) as (WNode | VNode)[];

tests/testing/unit/assertionTemplate.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ class MyWidget extends WidgetBase<{
1212
prependChild?: boolean;
1313
appendChild?: boolean;
1414
replaceChild?: boolean;
15+
before?: boolean;
16+
after?: boolean;
1517
}> {
1618
render() {
17-
const { toggleProperty, prependChild, appendChild, replaceChild } = this.properties;
19+
const { toggleProperty, prependChild, appendChild, replaceChild, before, after } = this.properties;
1820
let children = ['hello'];
1921
if (prependChild) {
2022
children = ['prepend', ...children];
@@ -27,7 +29,9 @@ class MyWidget extends WidgetBase<{
2729
}
2830
return v('div', { classes: ['root'] }, [
2931
v('h2', children),
30-
v('ul', [v('li', { foo: toggleProperty ? 'b' : 'a' }, ['one']), v('li', ['two']), v('li', ['three'])])
32+
before ? v('span', ['before']) : undefined,
33+
v('ul', [v('li', { foo: toggleProperty ? 'b' : 'a' }, ['one']), v('li', ['two']), v('li', ['three'])]),
34+
after ? v('span', ['after']) : undefined
3135
]);
3236
}
3337
}
@@ -98,6 +102,18 @@ describe('assertionTemplate', () => {
98102
h.expect(childAssertion);
99103
});
100104

105+
it('can set children after with insert', () => {
106+
const h = harness(() => w(MyWidget, { after: true }));
107+
const childAssertion = baseAssertion.insertChildren('ul', [v('span', ['after'])]);
108+
h.expect(childAssertion);
109+
});
110+
111+
it('can set children before with insert', () => {
112+
const h = harness(() => w(MyWidget, { before: true }));
113+
const childAssertion = baseAssertion.insertChildren('ul', [v('span', ['before'])], 'before');
114+
h.expect(childAssertion);
115+
});
116+
101117
it('can be used with tsx', () => {
102118
const h = harness(() => <MyWidget toggleProperty={true} />);
103119
const propertyAssertion = tsxAssertion.setProperty('~li-one', 'foo', 'b');

0 commit comments

Comments
 (0)