Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change defaultProps generation for react #1661

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-eyes-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[react]: Changed `defaultProps` generation for react, because defaultProps for function components is deprecated
18 changes: 18 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ jobs:

- name: Run E2E tests
run: yarn ci:e2e

- name: 🆙 Upload test results
if: failure()
uses: actions/upload-artifact@v4
with:
name: playwright-results
path: ./e2e/e2e-app/test-results
retention-days: 14

- name: 🆙 Upload e2e builds
if: failure()
uses: actions/upload-artifact@v4
with:
name: e2e-builds
path: |
./e2e/**/dist
!**/node_modules/**
retention-days: 14
1 change: 1 addition & 0 deletions e2e/e2e-app/src/component-paths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const COMPONENT_PATHS = [
'/default-props/',
'/one-component/',
'/two-components/',
'/types/',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useDefaultProps, useStore } from '@builder.io/mitosis';

useDefaultProps({ foo: 'abc', bar: 'foo' });

type DefaultPropsType = {
foo?: string;
bar?: string;
};

export default function DefaultProps(props: DefaultPropsType) {
const state = useStore({
getProps: () => {
return JSON.stringify({ foo: props.foo, bar: props.bar });
},
});

return <div data-testid="default-props">{state.getProps()}</div>;
}
5 changes: 5 additions & 0 deletions e2e/e2e-app/src/homepage.lite.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { For, onMount, Show, useStore } from '@builder.io/mitosis';
import { COMPONENT_PATHS } from './component-paths';
import ComponentWithTypes from './components/component-with-types.lite';
import DefaultProps from './components/default-props/use-default-props.lite';
import DisabledInput from './components/disabled-input/disabled-input.lite';
import NestedParent from './components/nested/nested-parent.lite';
import OneComponent from './components/one-component.lite';
Expand Down Expand Up @@ -42,6 +43,10 @@ export default function Homepage(props: { pathname?: string }) {
<NestedParent />
</Show>

<Show when={state.pathToUse.startsWith('/default-props')}>
<DefaultProps bar="xyz" />
</Show>

<Show when={state.pathToUse.startsWith('/types')}>
<ComponentWithTypes name="Lorem ipsum" />
</Show>
Expand Down
12 changes: 12 additions & 0 deletions e2e/e2e-app/tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const test = playwrightTest.extend<{ packageName: PackageName | 'DEFAULT' }>({
});

test.describe('e2e', () => {
test('default props', async ({ page, packageName }) => {
// TODO: Some targets don't support `defaultProps` :(
if (['e2e-qwik', 'e2e-solid'].includes(packageName)) {
test.skip();
}

await page.goto('/default-props/');
const text = await page.getByTestId('default-props').textContent();

expect(text?.includes('abc')).toBeTruthy();
expect(text?.includes('xyz')).toBeTruthy();
});
test('todo list add', async ({ page }) => {
await page.goto('/one-component/');

Expand Down
1 change: 1 addition & 0 deletions e2e/e2e-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Mitosis output for not yet include .d.ts, so ignore the types when importing.
// Trigger nx remote cache: 1
Copy link
Contributor

Choose a reason for hiding this comment

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

did you face issued with nx cache not rebuilding the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, somehow the cache for e2e-react was always used...

All the other e2e targets didn't use the cache, I don't know why react was the exception

// @ts-ignore
import { E2eApp } from '@builder.io/e2e-app/react';

Expand Down
28 changes: 28 additions & 0 deletions packages/core/src/__tests__/__snapshots__/alpine.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2967,6 +2967,20 @@ exports[`Alpine.js > jsx > Javascript Test > typeDependency 1`] = `
"
`;

exports[`Alpine.js > jsx > Javascript Test > typeExternalProps 1`] = `
"<div x-data=\\"typeExternalProps()\\">
Hello
<span x-html=\\"name\\"></span>
!
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"typeExternalProps\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Javascript Test > typeExternalStore 1`] = `
"<div x-data=\\"typeExternalStore()\\">
Hello
Expand Down Expand Up @@ -6085,6 +6099,20 @@ exports[`Alpine.js > jsx > Typescript Test > typeDependency 1`] = `
"
`;

exports[`Alpine.js > jsx > Typescript Test > typeExternalProps 1`] = `
"<div x-data=\\"typeExternalProps()\\">
Hello
<span x-html=\\"name\\"></span>
!
</div>
<script>
document.addEventListener(\\"alpine:init\\", () => {
Alpine.data(\\"typeExternalProps\\", () => ({}));
});
</script>
"
`;

exports[`Alpine.js > jsx > Typescript Test > typeExternalStore 1`] = `
"<div x-data=\\"typeExternalStore()\\">
Hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7140,6 +7140,38 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name;
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Javascript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -15133,6 +15165,40 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

import { FooProps } from \\"./foo-props\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name!: FooProps[\\"name\\"];
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with Preserve Imports and File Extensions > jsx > Typescript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7259,6 +7259,39 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name;
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
bootstrap: [SomeOtherComponent],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Javascript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -15382,6 +15415,41 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

import { FooProps } from \\"./foo-props\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name!: FooProps[\\"name\\"];
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
bootstrap: [SomeOtherComponent],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with Import Mapper Tests > jsx > Typescript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7357,6 +7357,38 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with manually creating and handling class properties as bindings (more stable) > jsx > Javascript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name;
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with manually creating and handling class properties as bindings (more stable) > jsx > Javascript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down Expand Up @@ -15626,6 +15658,40 @@ export class TypeDependencyModule {}
"
`;

exports[`Angular with manually creating and handling class properties as bindings (more stable) > jsx > Typescript Test > typeExternalProps 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";

import { Component, Input } from \\"@angular/core\\";

import { FooProps } from \\"./foo-props\\";

@Component({
selector: \\"type-external-props\\",
template: \`
<div>Hello {{name}} !</div>
\`,
styles: [
\`
:host {
display: contents;
}
\`,
],
})
export default class TypeExternalProps {
@Input() name!: FooProps[\\"name\\"];
}

@NgModule({
declarations: [TypeExternalProps],
imports: [CommonModule],
exports: [TypeExternalProps],
})
export class TypeExternalPropsModule {}
"
`;

exports[`Angular with manually creating and handling class properties as bindings (more stable) > jsx > Typescript Test > typeExternalStore 1`] = `
"import { NgModule } from \\"@angular/core\\";
import { CommonModule } from \\"@angular/common\\";
Expand Down
Loading
Loading