diff --git a/packages/webpack-plugin/lib/template-compiler/compiler.js b/packages/webpack-plugin/lib/template-compiler/compiler.js index a77da16905..2315e5d352 100644 --- a/packages/webpack-plugin/lib/template-compiler/compiler.js +++ b/packages/webpack-plugin/lib/template-compiler/compiler.js @@ -2245,7 +2245,6 @@ function postProcessIfReact (el) { delete el.elseif el._if = true addIfCondition(ifNode, { - exp: el.elseif.exp, block: el }) removeNode(el, true) diff --git a/packages/webpack-plugin/test/platform/common/wx-if.spec.js b/packages/webpack-plugin/test/platform/common/wx-if.spec.js index 1b0244ede5..143cf9a5d3 100644 --- a/packages/webpack-plugin/test/platform/common/wx-if.spec.js +++ b/packages/webpack-plugin/test/platform/common/wx-if.spec.js @@ -70,6 +70,34 @@ describe('template if should transform correct', function () { 'createElement(getComponent("mpx-text"), null,"1"))') }) + it('should keep node when condition is false', function () { + const input = '1' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null)') + }) + + it('should keep node when wx:elif condition is true', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('12') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?' + + 'createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))') + }) + + it('should remove node when wx:elif condition is false', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):null)') + }) + it('should handle __mpx_mode__ in condition', function () { const input = ` @@ -113,6 +141,177 @@ describe('template if should transform correct', function () { expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,' + '(true && someVar)?createElement(getComponent("mpx-text"), null,"1"):null)') }) + + it('should handle static true wx:if with dynamic wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))') + }) + + it('should handle static true wx:if with static true wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))') + }) + + it('should handle static true wx:if with static false wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))') + }) + + it('should handle static false wx:if with dynamic wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('2') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"2"):null)') + }) + + it('should handle static false wx:if with static true wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('2') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"2"))') + }) + + it('should handle static false wx:if with static false wx:elif', function () { + const input = '12' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null)') + }) + + it('should handle static true wx:if with wx:elif and wx:else', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"1"))') + }) + + it('should handle static false wx:if with static true wx:elif and wx:else', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('2') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"2"))') + }) + + it('should handle static false wx:if with static false wx:elif and wx:else', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('3') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,createElement(getComponent("mpx-text"), null,"3"))') + }) + + it('should handle dynamic wx:if with static false wx:elif and wx:else', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('13') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"3"))') + }) + + it('should handle dynamic wx:if with static true wx:elif and wx:else', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('12') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))') + }) + + it('should handle multiple dynamic wx:elif', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('123') + + const iosOutput = compileTemplateToIos(input) + 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)') + }) + + it('should handle multiple wx:elif with static false', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('13') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):(condition2)?createElement(getComponent("mpx-text"), null,"3"):null)') + }) + + it('should handle multiple wx:elif with static true in middle', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('12') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"2"))') + }) + + it('should handle static false wx:if with multiple wx:elif', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('23') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition1)?createElement(getComponent("mpx-text"), null,"2"):(condition2)?createElement(getComponent("mpx-text"), null,"3"):null)') + }) + + it('should handle static false wx:if with multiple static false wx:elif', function () { + const input = '123' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null)') + }) + + it('should handle multiple wx:elif with wx:else', function () { + const input = '1234' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('1234') + + const iosOutput = compileTemplateToIos(input) + 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"))') + }) + + it('should handle static false wx:if with multiple wx:elif and wx:else', function () { + const input = '1234' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('34') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"3"):createElement(getComponent("mpx-text"), null,"4"))') + }) + + it('should handle static true in second wx:elif', function () { + const input = '1234' + const wxOutput = compileTemplateToWx(input) + expect(wxOutput).toBe('13') + + const iosOutput = compileTemplateToIos(input) + expect(iosOutput).toBe('createElement(getComponent("mpx-view"), null,(condition)?createElement(getComponent("mpx-text"), null,"1"):createElement(getComponent("mpx-text"), null,"3"))') + }) }) describe('error cases', () => {