|
| 1 | +import { componentToBuilder } from '@/generators/builder'; |
| 2 | +import { componentToMitosis } from '@/generators/mitosis'; |
1 | 3 | import { BuilderContent } from '@builder.io/sdk'; |
| 4 | +import { parseJsx } from '../jsx'; |
2 | 5 | import { builderContentToMitosisComponent } from './builder'; |
3 | 6 |
|
4 | 7 | describe('Unpaired Surrogates', () => { |
@@ -28,4 +31,84 @@ describe('Unpaired Surrogates', () => { |
28 | 31 | expect(output.children[0].children[0].properties._text).not.toContain('\uD800'); |
29 | 32 | expect(output.children[0].children[0].properties._text).not.toContain('\uDFFF'); |
30 | 33 | }); |
| 34 | + |
| 35 | + test('should handle builder component with/without a colon in the name', () => { |
| 36 | + const builderContent: BuilderContent = { |
| 37 | + data: { |
| 38 | + blocks: [ |
| 39 | + { |
| 40 | + '@type': '@builder.io/sdk:Element' as const, |
| 41 | + component: { |
| 42 | + name: 'Text:123', |
| 43 | + options: { |
| 44 | + text: 'Hello World', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + '@type': '@builder.io/sdk:Element' as const, |
| 50 | + component: { |
| 51 | + name: 'Text123', |
| 52 | + options: { |
| 53 | + text: 'Hello World', |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + const component = builderContentToMitosisComponent(builderContent); |
| 62 | + expect(component.children[0].name).toBe('Text123'); |
| 63 | + expect(component.children[1].name).toBe('Text123'); |
| 64 | + const backToBuilder = componentToBuilder()({ component }); |
| 65 | + expect(backToBuilder?.data?.blocks?.[0]?.component?.name).toBe('Text:123'); |
| 66 | + expect(backToBuilder?.data?.blocks?.[1]?.component?.name).toBe('Text123'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should handle builder content -> mitosis json -> mitosis jsx -> mitosis json -> builder content', () => { |
| 70 | + const builderContent: BuilderContent = { |
| 71 | + data: { |
| 72 | + blocks: [ |
| 73 | + { |
| 74 | + '@type': '@builder.io/sdk:Element' as const, |
| 75 | + component: { |
| 76 | + name: 'Text:123', |
| 77 | + options: { |
| 78 | + text: 'Hello World', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + // Convert Builder JSON to Mitosis JSON |
| 87 | + const mitosisCmp = builderContentToMitosisComponent(builderContent); |
| 88 | + expect(mitosisCmp.children[0].name).toBe('Text123'); |
| 89 | + expect(mitosisCmp.children[0].properties['data-builder-originalName']).toBe('Text:123'); |
| 90 | + |
| 91 | + // Convert Mitosis JSON to Mitosis JSX |
| 92 | + const mitosisJsx = componentToMitosis()({ component: mitosisCmp }); |
| 93 | + expect(mitosisJsx).toMatchInlineSnapshot(` |
| 94 | + "import { Text123 } from \\"@components\\"; |
| 95 | +
|
| 96 | + export default function MyComponent(props) { |
| 97 | + return <Text123 text=\\"Hello World\\" data-builder-originalName=\\"Text:123\\" />; |
| 98 | + } |
| 99 | + " |
| 100 | + `); |
| 101 | + |
| 102 | + // Convert back Mitosis JSX to Mitosis JSON |
| 103 | + const backToMitosisCmp = parseJsx(mitosisJsx); |
| 104 | + expect(backToMitosisCmp.children[0].name).toBe('Text123'); |
| 105 | + expect(backToMitosisCmp.children[0].properties['data-builder-originalName']).toBe('Text:123'); |
| 106 | + |
| 107 | + // Convert back Mitosis JSON to Builder JSON |
| 108 | + const backToBuilder = componentToBuilder()({ component: backToMitosisCmp }); |
| 109 | + expect(backToBuilder?.data?.blocks?.[0]?.component?.name).toBe('Text:123'); |
| 110 | + expect(backToBuilder?.data?.blocks?.[0]?.component?.options).not.toHaveProperty( |
| 111 | + 'data-builder-originalName', |
| 112 | + ); |
| 113 | + }); |
31 | 114 | }); |
0 commit comments