|
| 1 | +import { componentToBuilder } from '@/generators/builder'; |
| 2 | +import { componentToMitosis } from '@/generators/mitosis'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | +import { |
| 5 | + builderContentToMitosisComponent, |
| 6 | + getStyleStringFromBlock, |
| 7 | +} from '../../parsers/builder/builder'; |
| 8 | + |
| 9 | +const options = { |
| 10 | + escapeInvalidCode: true, |
| 11 | + includeMeta: true, |
| 12 | + includeSpecialBindings: true, |
| 13 | +}; |
| 14 | + |
| 15 | +describe('Responsive Styles', () => { |
| 16 | + test('preserve bound media query styles when converting to mitosis', () => { |
| 17 | + const content = { |
| 18 | + data: { |
| 19 | + blocks: [ |
| 20 | + { |
| 21 | + '@type': '@builder.io/sdk:Element' as const, |
| 22 | + bindings: { |
| 23 | + 'responsiveStyles.small.left': 'state.left', |
| 24 | + 'responsiveStyles.small.top': 'state.top', |
| 25 | + 'responsiveStyles.large.color': 'state.color', |
| 26 | + 'style.fontSize': 'state.fontSize', |
| 27 | + 'style.background': '"red"', |
| 28 | + 'responsiveStyles.large.background': '"green"', |
| 29 | + 'component.options.responsiveStyles.medium.flexDirection': |
| 30 | + 'state.reverseColumnsWhenStacked && (state.stackColumnsAt === "tablet" || state.stackColumnsAt === "mobile") ? "column-reverse" : undefined', |
| 31 | + 'component.options.responsiveStyles.small.color': '"red"', |
| 32 | + 'component.options.responsiveStyles.medium.color': '"green"', |
| 33 | + 'component.options.responsiveStyles.large.color': '"blue"', |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + }; |
| 39 | + |
| 40 | + const result = getStyleStringFromBlock(content.data.blocks[0], options); |
| 41 | + |
| 42 | + // Should contain both media queries |
| 43 | + expect(result).toContain('@media (max-width: 1200px)'); |
| 44 | + expect(result).toContain('@media (max-width: 640px)'); |
| 45 | + expect(result).toContain('@media (max-width: 991px)'); |
| 46 | + |
| 47 | + // Should contain the correct flexDirection bindings |
| 48 | + expect(result).toContain( |
| 49 | + 'flexDirection: state.reverseColumnsWhenStacked && (state.stackColumnsAt === "tablet" || state.stackColumnsAt === "mobile") ? "column-reverse" : undefined', |
| 50 | + ); |
| 51 | + |
| 52 | + const mitosis = builderContentToMitosisComponent(content); |
| 53 | + |
| 54 | + expect(mitosis.children[0].bindings).toMatchInlineSnapshot(` |
| 55 | + { |
| 56 | + "responsiveStyles.large.color": { |
| 57 | + "bindingType": "expression", |
| 58 | + "code": "\\"blue\\"", |
| 59 | + "type": "single", |
| 60 | + }, |
| 61 | + "responsiveStyles.medium.color": { |
| 62 | + "bindingType": "expression", |
| 63 | + "code": "\\"green\\"", |
| 64 | + "type": "single", |
| 65 | + }, |
| 66 | + "responsiveStyles.medium.flexDirection": { |
| 67 | + "bindingType": "expression", |
| 68 | + "code": "state.reverseColumnsWhenStacked && (state.stackColumnsAt === \\"tablet\\" || state.stackColumnsAt === \\"mobile\\") ? \\"column-reverse\\" : undefined", |
| 69 | + "type": "single", |
| 70 | + }, |
| 71 | + "responsiveStyles.small.color": { |
| 72 | + "bindingType": "expression", |
| 73 | + "code": "\\"red\\"", |
| 74 | + "type": "single", |
| 75 | + }, |
| 76 | + "style": { |
| 77 | + "bindingType": "expression", |
| 78 | + "code": "{ fontSize: state.fontSize, background: \\"red\\", \\"@media (max-width: 640px)\\": { left: state.left, top: state.top, color: \\"red\\" }, \\"@media (max-width: 1200px)\\": { color: \\"blue\\", background: \\"green\\" }, \\"@media (max-width: 991px)\\": { flexDirection: state.reverseColumnsWhenStacked && (state.stackColumnsAt === \\"tablet\\" || state.stackColumnsAt === \\"mobile\\") ? \\"column-reverse\\" : undefined, color: \\"green\\" }, }", |
| 79 | + "type": "single", |
| 80 | + }, |
| 81 | + } |
| 82 | + `); |
| 83 | + |
| 84 | + const jsx = componentToMitosis()({ component: mitosis }); |
| 85 | + |
| 86 | + expect(jsx).toMatchInlineSnapshot(` |
| 87 | + "export default function MyComponent(props) { |
| 88 | + return ( |
| 89 | + <div |
| 90 | + style={{ |
| 91 | + fontSize: state.fontSize, |
| 92 | + background: \\"red\\", |
| 93 | + \\"@media (max-width: 640px)\\": { |
| 94 | + left: state.left, |
| 95 | + top: state.top, |
| 96 | + color: \\"red\\", |
| 97 | + }, |
| 98 | + \\"@media (max-width: 1200px)\\": { |
| 99 | + color: \\"blue\\", |
| 100 | + background: \\"green\\", |
| 101 | + }, |
| 102 | + \\"@media (max-width: 991px)\\": { |
| 103 | + flexDirection: |
| 104 | + state.reverseColumnsWhenStacked && |
| 105 | + (state.stackColumnsAt === \\"tablet\\" || |
| 106 | + state.stackColumnsAt === \\"mobile\\") |
| 107 | + ? \\"column-reverse\\" |
| 108 | + : undefined, |
| 109 | + color: \\"green\\", |
| 110 | + }, |
| 111 | + }} |
| 112 | + /> |
| 113 | + ); |
| 114 | + } |
| 115 | + " |
| 116 | + `); |
| 117 | + |
| 118 | + const json = componentToBuilder()({ component: mitosis }); |
| 119 | + expect(json).toMatchInlineSnapshot(` |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "blocks": [ |
| 123 | + { |
| 124 | + "@type": "@builder.io/sdk:Element", |
| 125 | + "actions": {}, |
| 126 | + "bindings": { |
| 127 | + "responsiveStyles.large.background": "\\"green\\"", |
| 128 | + "responsiveStyles.large.color": "\\"blue\\"", |
| 129 | + "responsiveStyles.medium.color": "\\"green\\"", |
| 130 | + "responsiveStyles.medium.flexDirection": "state.reverseColumnsWhenStacked && (state.stackColumnsAt === \\"tablet\\" || state.stackColumnsAt === \\"mobile\\") ? \\"column-reverse\\" : undefined", |
| 131 | + "responsiveStyles.small.color": "\\"red\\"", |
| 132 | + "responsiveStyles.small.left": "state.left", |
| 133 | + "responsiveStyles.small.top": "state.top", |
| 134 | + "style.background": "\\"red\\"", |
| 135 | + "style.fontSize": "state.fontSize", |
| 136 | + }, |
| 137 | + "children": [], |
| 138 | + "code": { |
| 139 | + "actions": {}, |
| 140 | + "bindings": {}, |
| 141 | + }, |
| 142 | + "properties": {}, |
| 143 | + "tagName": "div", |
| 144 | + }, |
| 145 | + ], |
| 146 | + "jsCode": "", |
| 147 | + "tsCode": "", |
| 148 | + }, |
| 149 | + } |
| 150 | + `); |
| 151 | + }); |
| 152 | +}); |
0 commit comments