Skip to content

Commit af3bf56

Browse files
authored
Fix boolean and property object formatting (#793)
* fix boolean and property object formatting * fix formatting to be valid * remove console
1 parent 228401e commit af3bf56

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

src/testing/assertRender.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,50 @@ function getTagName(node: VNode | WNode): string {
4242
return name;
4343
}
4444

45-
function formatObject(obj: { [index: string]: any }, depth = 0): string {
45+
function formatObject(obj: { [index: string]: any }, depth = 0, style: 'prop' | 'object'): string {
4646
const objectKeys = Object.keys(obj).sort();
4747
if (!objectKeys) {
4848
return '';
4949
}
5050
const tabs = getTabs(depth + 1);
51+
const sep = style === 'prop' ? '=' : ': ';
52+
const opening = style === 'prop' ? '{' : '';
53+
const closing = style === 'prop' ? '}' : '';
5154
return objectKeys.reduce((props, propKey, index) => {
5255
const prop = obj[propKey];
56+
propKey = style === 'object' ? `"${propKey}"` : propKey;
5357

5458
if (index < objectKeys.length) {
5559
props = `${props}${LINE_BREAK}${tabs}`;
5660
}
57-
props = `${props}${propKey}=`;
61+
props = `${props}${propKey}${sep}`;
62+
5863
switch (typeof prop) {
5964
case 'function':
60-
props = `${props}{function}`;
65+
props = `${props}${opening}function${closing}`;
66+
break;
67+
case 'boolean':
68+
props = `${props}${opening}${prop}${closing}`;
6169
break;
6270
case 'object':
6371
const isArrayLike = prop instanceof Set || prop instanceof Map || Array.isArray(prop);
6472
if (isArrayLike) {
65-
props = `${props}{${JSON.stringify(arrayFrom(prop))}}`;
73+
props = `${props}${opening}${JSON.stringify(arrayFrom(prop))}${closing}`;
6674
} else {
67-
props = `${props}{{${formatObject(prop, depth + 1)}${LINE_BREAK}${tabs}}}`;
75+
props = `${props}${opening}{${formatObject(
76+
prop,
77+
depth + 1,
78+
'object'
79+
)}${LINE_BREAK}${tabs}}${closing}`;
6880
}
6981
break;
7082
default:
71-
props = `${props}{${JSON.stringify(prop)}}`;
83+
props = `${props}${opening}${JSON.stringify(prop)}${closing}`;
7284
break;
7385
}
86+
if (style === 'object' && index < objectKeys.length - 1) {
87+
props = `${props},`;
88+
}
7489
return props;
7590
}, '');
7691
}
@@ -95,7 +110,7 @@ function format(nodes: DNode | DNode[], depth = 0): string {
95110

96111
const propertyKeys = Object.keys(node.properties).sort();
97112
if (propertyKeys.length) {
98-
str = `${str}${formatObject(node.properties, depth)}`;
113+
str = `${str}${formatObject(node.properties, depth, 'prop')}`;
99114
str = `${str}${LINE_BREAK}${getTabs(depth)}`;
100115
}
101116

@@ -124,6 +139,9 @@ function format(nodes: DNode | DNode[], depth = 0): string {
124139
str = `${str}${LINE_BREAK}${getTabs(depth + 3)}"child function"`;
125140
}
126141
str = `${str}${LINE_BREAK}${getTabs(depth + 2)})`;
142+
if (j < childrenKeys.length - 1) {
143+
str = `${str},`;
144+
}
127145
}
128146
str = `${str}${LINE_BREAK}${getTabs(depth + 1)}}`;
129147
}

tests/testing/unit/assertRender.tsx

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ class MockWidget extends Themed(WidgetBase) {
1515
}
1616
}
1717

18-
const WidgetWithNamedChildren = create().children<{ content: RenderResult }>()(function WidgetWithNamedChildren() {
19-
return '';
20-
});
18+
const WidgetWithNamedChildren = create().children<{ content: RenderResult; other: RenderResult }>()(
19+
function WidgetWithNamedChildren() {
20+
return '';
21+
}
22+
);
2123

2224
class OtherWidget extends WidgetBase {
2325
render() {
@@ -49,6 +51,7 @@ function getExpectedError() {
4951
return `
5052
<div
5153
(A) classes={["one","two","three"]}
54+
(A) disabled={false}
5255
(E) classes={"other"}
5356
extras={"foo"}
5457
(A) func={function}
@@ -65,14 +68,15 @@ function getExpectedError() {
6568
</span>
6669
(A) <${mockWidgetName}
6770
(A) classes={{
68-
(A) widget/Widget={{
69-
(A) root={["class"]}
70-
(A) }}
71+
(A) "widget/Widget": {
72+
(A) "root": ["class"]
73+
(A) }
7174
(A) }}
7275
(A) theme={{
73-
(A) widget/Widget={{
74-
(A) root={"theme-class"}
75-
(A) }}
76+
(A) "widget/Widget": {
77+
(A) "other": "root-other",
78+
(A) "root": "theme-class"
79+
(A) }
7680
(A) }}
7781
(A) >
7882
(A) </${mockWidgetName}>
@@ -84,6 +88,13 @@ function getExpectedError() {
8488
(A) Child
8589
(A) </span>
8690
(A) </div>
91+
(A) ),
92+
(A) other: (
93+
(A) <div>
94+
(A) <span>
95+
(A) Other
96+
(A) </span>
97+
(A) </div>
8798
(A) )
8899
(A) }
89100
(A) </${widgetWithChildrenName}>
@@ -94,17 +105,23 @@ describe('new/assertRender', () => {
94105
it('should create an informative error message', () => {
95106
try {
96107
assertRender(
97-
v('div', { extras: 'foo', key: 'two', classes: ['one', 'two', 'three'], func: () => {} }, [
98-
v('span', ['text node', 'other']),
99-
v('span'),
100-
'text node',
101-
v('span'),
102-
w(MockWidget, {
103-
theme: { 'widget/Widget': { root: 'theme-class' } },
104-
classes: { 'widget/Widget': { root: ['class'] } }
105-
}),
106-
w(WidgetWithNamedChildren, {}, [{ content: v('div', [v('span', ['Child'])]) }])
107-
]),
108+
v(
109+
'div',
110+
{ extras: 'foo', key: 'two', disabled: false, classes: ['one', 'two', 'three'], func: () => {} },
111+
[
112+
v('span', ['text node', 'other']),
113+
v('span'),
114+
'text node',
115+
v('span'),
116+
w(MockWidget, {
117+
theme: { 'widget/Widget': { root: 'theme-class', other: 'root-other' } },
118+
classes: { 'widget/Widget': { root: ['class'] } }
119+
}),
120+
w(WidgetWithNamedChildren, {}, [
121+
{ content: v('div', [v('span', ['Child'])]), other: v('div', [v('span', ['Other'])]) }
122+
])
123+
]
124+
),
108125
v('div', { extras: 'foo', key: 'two', classes: 'other' }, [
109126
v('span', ['text node', 'other']),
110127
v('span'),

0 commit comments

Comments
 (0)