Skip to content

Commit

Permalink
fix tests (#928)
Browse files Browse the repository at this point in the history
* fix tests

* fix type

* fix tests

* fix test

* fix react defaultProps

* document

* fix trim
  • Loading branch information
samijaber authored Nov 30, 2022
1 parent 4b04068 commit f68fc5b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 28 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/__snapshots__/preact.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ export default function SlotCode(props) {
}

SlotCode.defaultProps = {
children: '<div className=\\"default-slot\\">Default content</div>',
children: <div className=\\"default-slot\\">Default content</div>,
};
"
`;
Expand Down Expand Up @@ -3938,7 +3938,7 @@ export default function SlotCode(props: Props) {
}

SlotCode.defaultProps = {
children: '<div className=\\"default-slot\\">Default content</div>',
children: <div className=\\"default-slot\\">Default content</div>,
};
"
`;
Expand Down Expand Up @@ -5779,7 +5779,7 @@ export default function MyComponent(props) {

MyComponent.defaultProps = {
children: \\"default\\",
slotTest: \\"<div>default</div>\\",
slotTest: <div>default</div>,
};
"
`;
Expand Down Expand Up @@ -6172,7 +6172,7 @@ export default function MyComponent(props: any) {

MyComponent.defaultProps = {
children: \\"default\\",
slotTest: \\"<div>default</div>\\",
slotTest: <div>default</div>,
};
"
`;
Expand Down
36 changes: 26 additions & 10 deletions packages/core/src/__tests__/__snapshots__/react-native.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,11 @@ export default function SlotCode(props) {
}
SlotCode.defaultProps = {
children: \\"<View><Text>Default content</Text></View>\\",
children: (
<View>
<Text>Default content</Text>
</View>
),
};
"
`;
Expand Down Expand Up @@ -1221,8 +1225,8 @@ export default function SlotCode(props) {
}
SlotCode.defaultProps = {
slotLeft: \\"<Text>Default left</Text>\\",
children: \\"<Text>Default Child</Text>\\",
slotLeft: <Text>Default left</Text>,
children: <Text>Default Child</Text>,
};
"
`;
Expand Down Expand Up @@ -3796,7 +3800,11 @@ export default function SlotCode(props: Props) {
}
SlotCode.defaultProps = {
children: \\"<View><Text>Default content</Text></View>\\",
children: (
<View>
<Text>Default content</Text>
</View>
),
};
"
`;
Expand Down Expand Up @@ -3860,8 +3868,8 @@ export default function SlotCode(props: Props) {
}
SlotCode.defaultProps = {
slotLeft: \\"<Text>Default left</Text>\\",
children: \\"<Text>Default Child</Text>\\",
slotLeft: <Text>Default left</Text>,
children: <Text>Default Child</Text>,
};
"
`;
Expand Down Expand Up @@ -5609,8 +5617,12 @@ export default function MyComponent(props) {
}
MyComponent.defaultProps = {
children: \\"<Text>default</Text>\\",
slotTest: \\"<View><Text>default</Text></View>\\",
children: <Text>default</Text>,
slotTest: (
<View>
<Text>default</Text>
</View>
),
};
"
`;
Expand Down Expand Up @@ -5998,8 +6010,12 @@ export default function MyComponent(props: any) {
}
MyComponent.defaultProps = {
children: \\"<Text>default</Text>\\",
slotTest: \\"<View><Text>default</Text></View>\\",
children: <Text>default</Text>,
slotTest: (
<View>
<Text>default</Text>
</View>
),
};
"
`;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/__snapshots__/react.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ export default function SlotCode(props) {
}

SlotCode.defaultProps = {
children: '<div className=\\"default-slot\\">Default content</div>',
children: <div className=\\"default-slot\\">Default content</div>,
};
"
`;
Expand Down Expand Up @@ -3827,7 +3827,7 @@ export default function SlotCode(props: Props) {
}

SlotCode.defaultProps = {
children: '<div className=\\"default-slot\\">Default content</div>',
children: <div className=\\"default-slot\\">Default content</div>,
};
"
`;
Expand Down Expand Up @@ -5806,7 +5806,7 @@ export default function MyComponent(props) {

MyComponent.defaultProps = {
children: \\"default\\",
slotTest: \\"<div>default</div>\\",
slotTest: <div>default</div>,
};
"
`;
Expand Down Expand Up @@ -6183,7 +6183,7 @@ export default function MyComponent(props: any) {

MyComponent.defaultProps = {
children: \\"default\\",
slotTest: \\"<div>default</div>\\",
slotTest: <div>default</div>,
};
"
`;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/__snapshots__/rsc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5218,7 +5218,7 @@ export default function MyComponent(props) {
return <div slotContext={_context}>{props.slotTest}</div>;
}

MyComponent.defaultProps = { slotTest: \\"<div>default</div>\\" };
MyComponent.defaultProps = { slotTest: <div>default</div> };
"
`;

Expand Down Expand Up @@ -5552,7 +5552,7 @@ export default function MyComponent(props: any) {
return <div slotContext={_context}>{props.slotTest}</div>;
}

MyComponent.defaultProps = { slotTest: \\"<div>default</div>\\" };
MyComponent.defaultProps = { slotTest: <div>default</div> };
"
`;

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/__snapshots__/vue.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2434,8 +2434,8 @@ export default {
name: \\"on-update\\",

updated() {
foo({
someOption: bar,
this.foo({
someOption: this.bar,
});
},

Expand Down Expand Up @@ -5506,8 +5506,8 @@ export default {
name: \\"on-update\\",

updated() {
foo({
someOption: bar,
this.foo({
someOption: this.bar,
});
},

Expand Down
33 changes: 29 additions & 4 deletions packages/core/src/generators/react/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,27 @@ const NODE_MAPPERS: {

const hasChildren = json.children.length;

const renderChildren = () =>
json.children?.map((item) => blockToReact(item, options, component)).join('\n');
const renderChildren = () => {
const childrenStr = json.children
?.map((item) => blockToReact(item, options, component))
.join('\n')
.trim();
/**
* Ad-hoc way of figuring out if the children defaultProp is:
* - a JSX element, e.g. `<div>foo</div>`
* - a JS expression, e.g. `true`, `false`
* - a string, e.g. `'Default text'`
*
* and correctly wrapping it in quotes when appropriate.
*/
if (childrenStr.startsWith(`<`) && childrenStr.endsWith(`>`)) {
return childrenStr;
} else if (['false', 'true', 'null', 'undefined'].includes(childrenStr)) {
return childrenStr;
} else {
return `"${childrenStr}"`;
}
};

if (!slotName) {
// TODO: update MitosisNode for simple code
Expand All @@ -94,7 +113,10 @@ const NODE_MAPPERS: {
if (hasChildren) {
component.defaultProps = {
...(component.defaultProps || {}),
children: renderChildren().trim(),
children: {
code: renderChildren(),
type: 'property',
},
};
}
return `{${processBinding('props.children', options)}}`;
Expand All @@ -109,7 +131,10 @@ const NODE_MAPPERS: {
if (hasChildren) {
component.defaultProps = {
...(component.defaultProps || {}),
[slotProp.replace('props.', '')]: renderChildren(),
[slotProp.replace('props.', '')]: {
code: renderChildren(),
type: 'property',
},
};
}
return `{${slotProp}}`;
Expand Down

1 comment on commit f68fc5b

@vercel
Copy link

@vercel vercel bot commented on f68fc5b Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.