@@ -70,6 +70,34 @@ describe('template if should transform correct', function () {
7070 'createElement(getComponent("mpx-text"), null,"1"))' )
7171 } )
7272
73+ it ( 'should keep node when condition is false' , function ( ) {
74+ const input = '<view><text wx:if="{{false}}">1</text></view>'
75+ const wxOutput = compileTemplateToWx ( input )
76+ expect ( wxOutput ) . toBe ( '<view></view>' )
77+
78+ const iosOutput = compileTemplateToIos ( input )
79+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null)' )
80+ } )
81+
82+ it ( 'should keep node when wx:elif condition is true' , function ( ) {
83+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{true}}">2</text></view>'
84+ const wxOutput = compileTemplateToWx ( input )
85+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:else>2</text></view>' )
86+
87+ const iosOutput = compileTemplateToIos ( input )
88+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?' +
89+ 'createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))' )
90+ } )
91+
92+ it ( 'should remove node when wx:elif condition is false' , function ( ) {
93+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{false}}">2</text></view>'
94+ const wxOutput = compileTemplateToWx ( input )
95+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text></view>' )
96+
97+ const iosOutput = compileTemplateToIos ( input )
98+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):null)' )
99+ } )
100+
73101 it ( 'should handle __mpx_mode__ in condition' , function ( ) {
74102 const input = `
75103 <view>
@@ -113,6 +141,177 @@ describe('template if should transform correct', function () {
113141 expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,' +
114142 '(true && someVar)?createElement(getComponent("mpx-text"), null,"1"):null)' )
115143 } )
144+
145+ it ( 'should handle static true wx:if with dynamic wx:elif' , function ( ) {
146+ const input = '<view><text wx:if="{{true}}">1</text><text wx:elif="{{condition}}">2</text></view>'
147+ const wxOutput = compileTemplateToWx ( input )
148+ expect ( wxOutput ) . toBe ( '<view><text>1</text></view>' )
149+
150+ const iosOutput = compileTemplateToIos ( input )
151+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))' )
152+ } )
153+
154+ it ( 'should handle static true wx:if with static true wx:elif' , function ( ) {
155+ const input = '<view><text wx:if="{{true}}">1</text><text wx:elif="{{true}}">2</text></view>'
156+ const wxOutput = compileTemplateToWx ( input )
157+ expect ( wxOutput ) . toBe ( '<view><text>1</text></view>' )
158+
159+ const iosOutput = compileTemplateToIos ( input )
160+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))' )
161+ } )
162+
163+ it ( 'should handle static true wx:if with static false wx:elif' , function ( ) {
164+ const input = '<view><text wx:if="{{true}}">1</text><text wx:elif="{{false}}">2</text></view>'
165+ const wxOutput = compileTemplateToWx ( input )
166+ expect ( wxOutput ) . toBe ( '<view><text>1</text></view>' )
167+
168+ const iosOutput = compileTemplateToIos ( input )
169+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))' )
170+ } )
171+
172+ it ( 'should handle static false wx:if with dynamic wx:elif' , function ( ) {
173+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{condition}}">2</text></view>'
174+ const wxOutput = compileTemplateToWx ( input )
175+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">2</text></view>' )
176+
177+ const iosOutput = compileTemplateToIos ( input )
178+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"2"):null)' )
179+ } )
180+
181+ it ( 'should handle static false wx:if with static true wx:elif' , function ( ) {
182+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{true}}">2</text></view>'
183+ const wxOutput = compileTemplateToWx ( input )
184+ expect ( wxOutput ) . toBe ( '<view><text>2</text></view>' )
185+
186+ const iosOutput = compileTemplateToIos ( input )
187+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"2"))' )
188+ } )
189+
190+ it ( 'should handle static false wx:if with static false wx:elif' , function ( ) {
191+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{false}}">2</text></view>'
192+ const wxOutput = compileTemplateToWx ( input )
193+ expect ( wxOutput ) . toBe ( '<view></view>' )
194+
195+ const iosOutput = compileTemplateToIos ( input )
196+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null)' )
197+ } )
198+
199+ it ( 'should handle static true wx:if with wx:elif and wx:else' , function ( ) {
200+ const input = '<view><text wx:if="{{true}}">1</text><text wx:elif="{{condition}}">2</text><text wx:else>3</text></view>'
201+ const wxOutput = compileTemplateToWx ( input )
202+ expect ( wxOutput ) . toBe ( '<view><text>1</text></view>' )
203+
204+ const iosOutput = compileTemplateToIos ( input )
205+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))' )
206+ } )
207+
208+ it ( 'should handle static false wx:if with static true wx:elif and wx:else' , function ( ) {
209+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{true}}">2</text><text wx:else>3</text></view>'
210+ const wxOutput = compileTemplateToWx ( input )
211+ expect ( wxOutput ) . toBe ( '<view><text>2</text></view>' )
212+
213+ const iosOutput = compileTemplateToIos ( input )
214+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"2"))' )
215+ } )
216+
217+ it ( 'should handle static false wx:if with static false wx:elif and wx:else' , function ( ) {
218+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{false}}">2</text><text wx:else>3</text></view>'
219+ const wxOutput = compileTemplateToWx ( input )
220+ expect ( wxOutput ) . toBe ( '<view><text>3</text></view>' )
221+
222+ const iosOutput = compileTemplateToIos ( input )
223+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"3"))' )
224+ } )
225+
226+ it ( 'should handle dynamic wx:if with static false wx:elif and wx:else' , function ( ) {
227+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{false}}">2</text><text wx:else>3</text></view>'
228+ const wxOutput = compileTemplateToWx ( input )
229+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:else>3</text></view>' )
230+
231+ const iosOutput = compileTemplateToIos ( input )
232+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"3"))' )
233+ } )
234+
235+ it ( 'should handle dynamic wx:if with static true wx:elif and wx:else' , function ( ) {
236+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{true}}">2</text><text wx:else>3</text></view>'
237+ const wxOutput = compileTemplateToWx ( input )
238+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:else>2</text></view>' )
239+
240+ const iosOutput = compileTemplateToIos ( input )
241+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))' )
242+ } )
243+
244+ it ( 'should handle multiple dynamic wx:elif' , function ( ) {
245+ const input = '<view><text wx:if="{{condition1}}">1</text><text wx:elif="{{condition2}}">2</text><text wx:elif="{{condition3}}">3</text></view>'
246+ const wxOutput = compileTemplateToWx ( input )
247+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition1}}">1</text><text wx:elif="{{condition2}}">2</text><text wx:elif="{{condition3}}">3</text></view>' )
248+
249+ const iosOutput = compileTemplateToIos ( input )
250+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition1)?createElement(getComponent("mpx-text"), null,"1"):(condition2)?createElement(getComponent("mpx-text"), null,"2"):(condition3)?createElement(getComponent("mpx-text"), null,"3"):null)' )
251+ } )
252+
253+ it ( 'should handle multiple wx:elif with static false' , function ( ) {
254+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{false}}">2</text><text wx:elif="{{condition2}}">3</text></view>'
255+ const wxOutput = compileTemplateToWx ( input )
256+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{condition2}}">3</text></view>' )
257+
258+ const iosOutput = compileTemplateToIos ( input )
259+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):(condition2)?createElement(getComponent("mpx-text"), null,"3"):null)' )
260+ } )
261+
262+ it ( 'should handle multiple wx:elif with static true in middle' , function ( ) {
263+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{true}}">2</text><text wx:elif="{{condition2}}">3</text></view>'
264+ const wxOutput = compileTemplateToWx ( input )
265+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:else>2</text></view>' )
266+
267+ const iosOutput = compileTemplateToIos ( input )
268+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))' )
269+ } )
270+
271+ it ( 'should handle static false wx:if with multiple wx:elif' , function ( ) {
272+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{condition1}}">2</text><text wx:elif="{{condition2}}">3</text></view>'
273+ const wxOutput = compileTemplateToWx ( input )
274+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition1}}">2</text><text wx:elif="{{condition2}}">3</text></view>' )
275+
276+ const iosOutput = compileTemplateToIos ( input )
277+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition1)?createElement(getComponent("mpx-text"), null,"2"):(condition2)?createElement(getComponent("mpx-text"), null,"3"):null)' )
278+ } )
279+
280+ it ( 'should handle static false wx:if with multiple static false wx:elif' , function ( ) {
281+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{false}}">2</text><text wx:elif="{{false}}">3</text></view>'
282+ const wxOutput = compileTemplateToWx ( input )
283+ expect ( wxOutput ) . toBe ( '<view></view>' )
284+
285+ const iosOutput = compileTemplateToIos ( input )
286+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null)' )
287+ } )
288+
289+ it ( 'should handle multiple wx:elif with wx:else' , function ( ) {
290+ const input = '<view><text wx:if="{{condition1}}">1</text><text wx:elif="{{condition2}}">2</text><text wx:elif="{{condition3}}">3</text><text wx:else>4</text></view>'
291+ const wxOutput = compileTemplateToWx ( input )
292+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition1}}">1</text><text wx:elif="{{condition2}}">2</text><text wx:elif="{{condition3}}">3</text><text wx:else>4</text></view>' )
293+
294+ const iosOutput = compileTemplateToIos ( input )
295+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition1)?createElement(getComponent("mpx-text"), null,"1"):(condition2)?createElement(getComponent("mpx-text"), null,"2"):(condition3)?createElement(getComponent("mpx-text"), null,"3"):createElement(getComponent("mpx-text"), null,"4"))' )
296+ } )
297+
298+ it ( 'should handle static false wx:if with multiple wx:elif and wx:else' , function ( ) {
299+ const input = '<view><text wx:if="{{false}}">1</text><text wx:elif="{{false}}">2</text><text wx:elif="{{condition}}">3</text><text wx:else>4</text></view>'
300+ const wxOutput = compileTemplateToWx ( input )
301+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">3</text><text wx:else>4</text></view>' )
302+
303+ const iosOutput = compileTemplateToIos ( input )
304+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"3"):createElement(getComponent("mpx-text"), null,"4"))' )
305+ } )
306+
307+ it ( 'should handle static true in second wx:elif' , function ( ) {
308+ const input = '<view><text wx:if="{{condition}}">1</text><text wx:elif="{{false}}">2</text><text wx:elif="{{true}}">3</text><text wx:elif="{{condition2}}">4</text></view>'
309+ const wxOutput = compileTemplateToWx ( input )
310+ expect ( wxOutput ) . toBe ( '<view><text wx:if="{{condition}}">1</text><text wx:else>3</text></view>' )
311+
312+ const iosOutput = compileTemplateToIos ( input )
313+ expect ( iosOutput ) . toBe ( 'createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"3"))' )
314+ } )
116315 } )
117316
118317 describe ( 'error cases' , ( ) => {
0 commit comments