From b342f2e8b7edb42f245daa62d330f054ea99cdb9 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 4 May 2017 13:16:53 -0400 Subject: [PATCH] fix outros on compound if blocks, get IfBlock.js coverage to 100% --- src/generators/dom/visitors/IfBlock.js | 53 +++++++++---------- .../_config.js | 22 ++++++++ .../main.html | 20 +++++++ .../_config.js | 18 +++++++ .../main.html | 20 +++++++ .../_config.js | 23 ++++++++ .../main.html | 20 +++++++ 7 files changed, 149 insertions(+), 27 deletions(-) create mode 100644 test/runtime/samples/transition-js-if-else-block-dynamic-outro/_config.js create mode 100644 test/runtime/samples/transition-js-if-else-block-dynamic-outro/main.html create mode 100644 test/runtime/samples/transition-js-if-else-block-outro/_config.js create mode 100644 test/runtime/samples/transition-js-if-else-block-outro/main.html create mode 100644 test/runtime/samples/transition-js-if-elseif-block-outro/_config.js create mode 100644 test/runtime/samples/transition-js-if-elseif-block-outro/main.html diff --git a/src/generators/dom/visitors/IfBlock.js b/src/generators/dom/visitors/IfBlock.js index f5e8ad8418..c8da4b7d62 100644 --- a/src/generators/dom/visitors/IfBlock.js +++ b/src/generators/dom/visitors/IfBlock.js @@ -215,6 +215,7 @@ function compoundWithOutros ( generator, block, state, node, branches, dynamic, const if_current_block_index = hasElse ? '' : `if ( ~${current_block_index} ) `; block.addVariable( current_block_index ); + block.addVariable( name ); block.builders.create.addBlock( deindent` var ${if_block_creators} = [ @@ -233,12 +234,12 @@ function compoundWithOutros ( generator, block, state, node, branches, dynamic, if ( hasElse ) { block.builders.create.addBlock( deindent` ${current_block_index} = ${get_block}( ${params} ); - ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); + ${name} = ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); ` ); } else { block.builders.create.addBlock( deindent` if ( ~( ${current_block_index} = ${get_block}( ${params} ) ) ) { - ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); + ${name} = ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); } ` ); } @@ -253,37 +254,35 @@ function compoundWithOutros ( generator, block, state, node, branches, dynamic, const parentNode = state.parentNode || `${anchor}.parentNode`; - const changeBlock = deindent` - var ${name} = ${if_blocks}[ ${previous_block_index} ]; - if ( ${name} ) { - ${name}.outro( function () { - ${if_blocks}[ ${previous_block_index} ].destroy( true ); - ${if_blocks}[ ${previous_block_index} ] = null; - }); - } + const destroyOldBlock = deindent` + ${name}.outro( function () { + ${if_blocks}[ ${previous_block_index} ].destroy( true ); + ${if_blocks}[ ${previous_block_index} ] = null; + }); `; - if ( hasElse ) { - block.builders.create.addBlock( deindent` - ${name} = ${if_blocks}[ ${current_block_index} ]; - if ( !${name} ) { - ${name} = ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); + const createNewBlock = deindent` + ${name} = ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); + ${name}.${mountOrIntro}( ${parentNode}, ${anchor} ); + `; + + const changeBlock = hasElse ? + deindent` + ${destroyOldBlock} + + ${createNewBlock} + ` : + deindent` + if ( ${name} ) { + ${destroyOldBlock} } - ${name}.${mountOrIntro}( ${parentNode}, ${anchor} ); - ` ); - } else { - block.builders.create.addBlock( deindent` if ( ~${current_block_index} ) { - ${name} = ${if_blocks}[ ${current_block_index} ]; - if ( !${name} ) { - ${name} = ${if_blocks}[ ${current_block_index} ] = ${if_block_creators}[ ${current_block_index} ]( ${params}, ${block.component} ); - } - - ${name}.${mountOrIntro}( ${parentNode}, ${anchor} ); + ${createNewBlock} + } else { + ${name} = null; } - ` ); - } + `; if ( dynamic ) { block.builders.update.addBlock( deindent` diff --git a/test/runtime/samples/transition-js-if-else-block-dynamic-outro/_config.js b/test/runtime/samples/transition-js-if-else-block-dynamic-outro/_config.js new file mode 100644 index 0000000000..3ea7079830 --- /dev/null +++ b/test/runtime/samples/transition-js-if-else-block-dynamic-outro/_config.js @@ -0,0 +1,22 @@ +export default { + data: { + z: 'z' + }, + + test ( assert, component, target, window, raf ) { + assert.equal( target.querySelector( 'div' ), component.refs.no ); + + component.set({ x: true }); + + raf.tick( 25 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.75 ); + + raf.tick( 75 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.25 ); + + raf.tick( 100 ); + component.destroy(); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/transition-js-if-else-block-dynamic-outro/main.html b/test/runtime/samples/transition-js-if-else-block-dynamic-outro/main.html new file mode 100644 index 0000000000..aa1ca15fef --- /dev/null +++ b/test/runtime/samples/transition-js-if-else-block-dynamic-outro/main.html @@ -0,0 +1,20 @@ +{{#if x}} +
{{z}}
+{{else}} +
{{z}}
+{{/if}} + + \ No newline at end of file diff --git a/test/runtime/samples/transition-js-if-else-block-outro/_config.js b/test/runtime/samples/transition-js-if-else-block-outro/_config.js new file mode 100644 index 0000000000..98053c817b --- /dev/null +++ b/test/runtime/samples/transition-js-if-else-block-outro/_config.js @@ -0,0 +1,18 @@ +export default { + test ( assert, component, target, window, raf ) { + assert.equal( target.querySelector( 'div' ), component.refs.no ); + + component.set({ x: true }); + + raf.tick( 25 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.75 ); + + raf.tick( 75 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.25 ); + + raf.tick( 100 ); + component.destroy(); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/transition-js-if-else-block-outro/main.html b/test/runtime/samples/transition-js-if-else-block-outro/main.html new file mode 100644 index 0000000000..47cc670d1f --- /dev/null +++ b/test/runtime/samples/transition-js-if-else-block-outro/main.html @@ -0,0 +1,20 @@ +{{#if x}} +
yes
+{{else}} +
no
+{{/if}} + + \ No newline at end of file diff --git a/test/runtime/samples/transition-js-if-elseif-block-outro/_config.js b/test/runtime/samples/transition-js-if-elseif-block-outro/_config.js new file mode 100644 index 0000000000..fad2d41e3b --- /dev/null +++ b/test/runtime/samples/transition-js-if-elseif-block-outro/_config.js @@ -0,0 +1,23 @@ +export default { + data: { + x: false, + y: true + }, + + test ( assert, component, target, window, raf ) { + assert.equal( target.querySelector( 'div' ), component.refs.no ); + + component.set({ x: true, y: false }); + + raf.tick( 25 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.75 ); + + raf.tick( 75 ); + assert.equal( component.refs.yes.foo, undefined ); + assert.equal( component.refs.no.foo, 0.25 ); + + raf.tick( 100 ); + component.destroy(); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/transition-js-if-elseif-block-outro/main.html b/test/runtime/samples/transition-js-if-elseif-block-outro/main.html new file mode 100644 index 0000000000..7b45898ab5 --- /dev/null +++ b/test/runtime/samples/transition-js-if-elseif-block-outro/main.html @@ -0,0 +1,20 @@ +{{#if x}} +
yes
+{{elseif y}} +
no
+{{/if}} + + \ No newline at end of file