|
| 1 | +const { compileTemplate, warnFn, errorFn } = require('../util') |
| 2 | + |
| 3 | +describe('cross-platform syntax warning', function () { |
| 4 | + afterEach(() => { |
| 5 | + warnFn.mockClear() |
| 6 | + errorFn.mockClear() |
| 7 | + }) |
| 8 | + |
| 9 | + it('should not warn for auto-converted attributes (wx:class in ali mode)', function () { |
| 10 | + // wx:class 会被平台转换规则自动转换为 a:class,所以不会警告 |
| 11 | + const input = '<view wx:class="{{someClass}}">content</view>' |
| 12 | + compileTemplate(input, { mode: 'ali' }) |
| 13 | + expect(warnFn).not.toHaveBeenCalled() |
| 14 | + }) |
| 15 | + |
| 16 | + it('should warn when using a: prefix in wx mode (no auto-conversion)', function () { |
| 17 | + // a:class 在 wx 模式下没有转换规则,会触发警告 |
| 18 | + const input = '<view a:class="{{someClass}}">content</view>' |
| 19 | + compileTemplate(input, { mode: 'wx' }) |
| 20 | + expect(warnFn).toHaveBeenCalledWith( |
| 21 | + expect.stringContaining('Your target mode is "wx", but used "a:class". Did you mean "wx:class"?') |
| 22 | + ) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should warn when using s- prefix in wx mode (no auto-conversion)', function () { |
| 26 | + // s-if 在 wx 模式下没有转换规则,会触发警告 |
| 27 | + const input = '<view s-if="{{condition}}">content</view>' |
| 28 | + compileTemplate(input, { mode: 'wx' }) |
| 29 | + expect(warnFn).toHaveBeenCalledWith( |
| 30 | + expect.stringContaining('Your target mode is "wx", but used "s-if". Did you mean "wx:if"?') |
| 31 | + ) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should warn when using qq: prefix in ali mode (no auto-conversion)', function () { |
| 35 | + // qq:for 在 ali 模式下没有转换规则,会触发警告 |
| 36 | + const input = '<view qq:for="{{items}}">content</view>' |
| 37 | + compileTemplate(input, { mode: 'ali' }) |
| 38 | + expect(warnFn).toHaveBeenCalledWith( |
| 39 | + expect.stringContaining('Your target mode is "ali", but used "qq:for". Did you mean "a:for"?') |
| 40 | + ) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should warn when using tt: prefix in swan mode (no auto-conversion)', function () { |
| 44 | + // tt:style 在 swan 模式下没有转换规则,会触发警告 |
| 45 | + const input = '<view tt:style="{{dynamicStyle}}">content</view>' |
| 46 | + compileTemplate(input, { mode: 'swan' }) |
| 47 | + expect(warnFn).toHaveBeenCalledWith( |
| 48 | + expect.stringContaining('Your target mode is "swan", but used "tt:style". Did you mean "s-style"?') |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should warn when using dd: prefix in qq mode (no auto-conversion)', function () { |
| 53 | + // dd:show 在 qq 模式下没有转换规则,会触发警告 |
| 54 | + const input = '<view dd:show="{{visible}}">content</view>' |
| 55 | + compileTemplate(input, { mode: 'qq' }) |
| 56 | + expect(warnFn).toHaveBeenCalledWith( |
| 57 | + expect.stringContaining('Your target mode is "qq", but used "dd:show". Did you mean "qq:show"?') |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should not warn when using correct prefix for current mode', function () { |
| 62 | + const input1 = '<view wx:class="{{someClass}}">content</view>' |
| 63 | + compileTemplate(input1, { mode: 'wx' }) |
| 64 | + expect(warnFn).not.toHaveBeenCalled() |
| 65 | + |
| 66 | + const input2 = '<view a:class="{{someClass}}">content</view>' |
| 67 | + compileTemplate(input2, { mode: 'ali' }) |
| 68 | + expect(warnFn).not.toHaveBeenCalled() |
| 69 | + |
| 70 | + const input3 = '<view s-if="{{condition}}">content</view>' |
| 71 | + compileTemplate(input3, { mode: 'swan' }) |
| 72 | + expect(warnFn).not.toHaveBeenCalled() |
| 73 | + |
| 74 | + const input4 = '<view qq:for="{{items}}">content</view>' |
| 75 | + compileTemplate(input4, { mode: 'qq' }) |
| 76 | + expect(warnFn).not.toHaveBeenCalled() |
| 77 | + |
| 78 | + const input5 = '<view tt:style="{{dynamicStyle}}">content</view>' |
| 79 | + compileTemplate(input5, { mode: 'tt' }) |
| 80 | + expect(warnFn).not.toHaveBeenCalled() |
| 81 | + }) |
| 82 | + |
| 83 | + it('should not warn for regular attributes without platform prefixes', function () { |
| 84 | + const input = '<view class="normal" style="color: red;" id="test">content</view>' |
| 85 | + compileTemplate(input, { mode: 'ali' }) |
| 86 | + expect(warnFn).not.toHaveBeenCalled() |
| 87 | + }) |
| 88 | + |
| 89 | + it('should warn for cross-platform attributes without auto-conversion', function () { |
| 90 | + // 只检测没有自动转换规则的属性 |
| 91 | + const input = '<view s-if="{{condition}}" qq:for="{{items}}">content</view>' |
| 92 | + compileTemplate(input, { mode: 'ali' }) |
| 93 | + expect(warnFn).toHaveBeenCalledTimes(2) |
| 94 | + expect(warnFn).toHaveBeenCalledWith( |
| 95 | + expect.stringContaining('Your target mode is "ali", but used "s-if". Did you mean "a:if"?') |
| 96 | + ) |
| 97 | + expect(warnFn).toHaveBeenCalledWith( |
| 98 | + expect.stringContaining('Your target mode is "ali", but used "qq:for". Did you mean "a:for"?') |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should work with nested elements', function () { |
| 103 | + // 只测试没有自动转换的属性 |
| 104 | + const input = ` |
| 105 | + <view s-if="{{condition}}"> |
| 106 | + <text s-class="{{textClass}}">Hello</text> |
| 107 | + </view> |
| 108 | + ` |
| 109 | + compileTemplate(input, { mode: 'ali' }) |
| 110 | + expect(warnFn).toHaveBeenCalledTimes(2) |
| 111 | + expect(warnFn).toHaveBeenCalledWith( |
| 112 | + expect.stringContaining('Your target mode is "ali", but used "s-if". Did you mean "a:if"?') |
| 113 | + ) |
| 114 | + expect(warnFn).toHaveBeenCalledWith( |
| 115 | + expect.stringContaining('Your target mode is "ali", but used "s-class". Did you mean "a:class"?') |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should handle all supported platforms', function () { |
| 120 | + // Test jd: prefix |
| 121 | + const input1 = '<view jd:ref="myRef">content</view>' |
| 122 | + compileTemplate(input1, { mode: 'wx' }) |
| 123 | + expect(warnFn).toHaveBeenCalledWith( |
| 124 | + expect.stringContaining('Your target mode is "wx", but used "jd:ref". Did you mean "wx:ref"?') |
| 125 | + ) |
| 126 | + |
| 127 | + warnFn.mockClear() |
| 128 | + |
| 129 | + // Test qa: prefix |
| 130 | + const input2 = '<view qa:key="{{item.id}}">content</view>' |
| 131 | + compileTemplate(input2, { mode: 'tt' }) |
| 132 | + expect(warnFn).toHaveBeenCalledWith( |
| 133 | + expect.stringContaining('Your target mode is "tt", but used "qa:key". Did you mean "tt:key"?') |
| 134 | + ) |
| 135 | + }) |
| 136 | + |
| 137 | + it('should error for React Native platforms when using non-wx prefixes', function () { |
| 138 | + const input = '<view a:class="{{someClass}}">content</view>' |
| 139 | + |
| 140 | + // Test android |
| 141 | + compileTemplate(input, { mode: 'android' }) |
| 142 | + expect(errorFn).toHaveBeenCalledWith( |
| 143 | + expect.stringContaining('React Native mode "android" does not support "a:" prefix. Use "wx:" prefix instead. Found: "a:class"') |
| 144 | + ) |
| 145 | + |
| 146 | + errorFn.mockClear() |
| 147 | + |
| 148 | + // Test ios |
| 149 | + compileTemplate(input, { mode: 'ios' }) |
| 150 | + expect(errorFn).toHaveBeenCalledWith( |
| 151 | + expect.stringContaining('React Native mode "ios" does not support "a:" prefix. Use "wx:" prefix instead. Found: "a:class"') |
| 152 | + ) |
| 153 | + |
| 154 | + errorFn.mockClear() |
| 155 | + |
| 156 | + // Test harmony |
| 157 | + compileTemplate(input, { mode: 'harmony' }) |
| 158 | + expect(errorFn).toHaveBeenCalledWith( |
| 159 | + expect.stringContaining('React Native mode "harmony" does not support "a:" prefix. Use "wx:" prefix instead. Found: "a:class"') |
| 160 | + ) |
| 161 | + }) |
| 162 | + |
| 163 | + it('should not warn for React Native platforms when using wx: prefix', function () { |
| 164 | + const input = '<view wx:class="{{someClass}}">content</view>' |
| 165 | + |
| 166 | + // Test android |
| 167 | + compileTemplate(input, { mode: 'android' }) |
| 168 | + expect(warnFn).not.toHaveBeenCalled() |
| 169 | + expect(errorFn).not.toHaveBeenCalled() |
| 170 | + |
| 171 | + // Test ios |
| 172 | + compileTemplate(input, { mode: 'ios' }) |
| 173 | + expect(warnFn).not.toHaveBeenCalled() |
| 174 | + expect(errorFn).not.toHaveBeenCalled() |
| 175 | + |
| 176 | + // Test harmony |
| 177 | + compileTemplate(input, { mode: 'harmony' }) |
| 178 | + expect(warnFn).not.toHaveBeenCalled() |
| 179 | + expect(errorFn).not.toHaveBeenCalled() |
| 180 | + }) |
| 181 | + |
| 182 | + it('should handle conditional compilation attributes correctly', function () { |
| 183 | + // 测试 wx to ali 场景下的条件编译属性 |
| 184 | + const input1 = '<alicom@ali a:if="{{show}}">content</alicom>' |
| 185 | + compileTemplate(input1, { srcMode: 'wx', mode: 'ali' }) |
| 186 | + expect(warnFn).not.toHaveBeenCalled() // 应该不警告,因为目标是ali模式 |
| 187 | + |
| 188 | + const input2 = '<com a:if@ali="{{show}}">content</com>' |
| 189 | + compileTemplate(input2, { srcMode: 'wx', mode: 'ali' }) |
| 190 | + expect(warnFn).not.toHaveBeenCalled() // 应该不警告,因为目标是ali模式 |
| 191 | + }) |
| 192 | + |
| 193 | + it('should warn when using a: prefix in web mode', function () { |
| 194 | + // web 模式应该使用 v- 前缀,使用 a: 会警告 |
| 195 | + const input = '<view a:class="someClass">content</view>' |
| 196 | + compileTemplate(input, { mode: 'web' }) |
| 197 | + expect(warnFn).toHaveBeenCalledWith( |
| 198 | + expect.stringContaining('Your target mode is "web", but used "a:class". Did you mean "v-class"?') |
| 199 | + ) |
| 200 | + }) |
| 201 | + |
| 202 | + it('should warn when using s- prefix in web mode', function () { |
| 203 | + // web 模式应该使用 v- 前缀,使用 s- 会警告 |
| 204 | + const input = '<view s-if="condition">content</view>' |
| 205 | + compileTemplate(input, { mode: 'web' }) |
| 206 | + expect(warnFn).toHaveBeenCalledWith( |
| 207 | + expect.stringContaining('Your target mode is "web", but used "s-if". Did you mean "v-if"?') |
| 208 | + ) |
| 209 | + }) |
| 210 | + |
| 211 | + it('should not warn when using v- prefix in web mode', function () { |
| 212 | + // web 模式使用 v- 前缀不应该警告 |
| 213 | + const input = '<div v-if="condition">content</div>' |
| 214 | + compileTemplate(input, { mode: 'web' }) |
| 215 | + expect(warnFn).not.toHaveBeenCalled() |
| 216 | + }) |
| 217 | + |
| 218 | + it('should warn when using v- prefix in miniprogram mode', function () { |
| 219 | + // 小程序模式使用 v- 前缀应该警告 |
| 220 | + const input = '<view v-if="condition">content</view>' |
| 221 | + compileTemplate(input, { mode: 'wx' }) |
| 222 | + expect(warnFn).toHaveBeenCalledWith( |
| 223 | + expect.stringContaining('Your target mode is "wx", but used "v-if". Did you mean "wx:if"?') |
| 224 | + ) |
| 225 | + }) |
| 226 | +}) |
0 commit comments