From 661af3acdef04dcfc9fc3782e11b25f03a1984b0 Mon Sep 17 00:00:00 2001
From: xujiongbo <xujiongbo@gmail.com>
Date: Thu, 6 Jul 2017 18:27:40 +0800
Subject: [PATCH 1/2] fix #5997: refactor function registerRef

---
 .flowconfig                  | 1 +
 src/core/vdom/modules/ref.js | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/.flowconfig b/.flowconfig
index 4bef7624d38..b070e08fddb 100644
--- a/.flowconfig
+++ b/.flowconfig
@@ -20,3 +20,4 @@ module.name_mapper='^weex/\(.*\)$' -> '<PROJECT_ROOT>/src/platforms/weex/\1'
 module.name_mapper='^server/\(.*\)$' -> '<PROJECT_ROOT>/src/server/\1'
 module.name_mapper='^entries/\(.*\)$' -> '<PROJECT_ROOT>/src/entries/\1'
 module.name_mapper='^sfc/\(.*\)$' -> '<PROJECT_ROOT>/src/sfc/\1'
+suppress_comment= \\(.\\|\n\\)*\\$flow-disable-line
diff --git a/src/core/vdom/modules/ref.js b/src/core/vdom/modules/ref.js
index c69547ce7a3..00bb5f52709 100644
--- a/src/core/vdom/modules/ref.js
+++ b/src/core/vdom/modules/ref.js
@@ -32,10 +32,11 @@ export function registerRef (vnode: VNodeWithData, isRemoval: ?boolean) {
     }
   } else {
     if (vnode.data.refInFor) {
-      if (Array.isArray(refs[key]) && refs[key].indexOf(ref) < 0) {
-        refs[key].push(ref)
-      } else {
+      if (!Array.isArray(refs[key])) {
         refs[key] = [ref]
+      } else if (refs[key].indexOf(ref) < 0) {
+        // $flow-disable-line
+        refs[key].push(ref)
       }
     } else {
       refs[key] = ref

From 56e868ed228c7556e02191808a383ab56e29544d Mon Sep 17 00:00:00 2001
From: xujiongbo <xujiongbo@gmail.com>
Date: Thu, 6 Jul 2017 21:05:28 +0800
Subject: [PATCH 2/2] add test case

---
 test/unit/features/ref.spec.js | 50 ++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/test/unit/features/ref.spec.js b/test/unit/features/ref.spec.js
index da637b7bfa3..f82efe4513a 100644
--- a/test/unit/features/ref.spec.js
+++ b/test/unit/features/ref.spec.js
@@ -146,6 +146,56 @@ describe('ref', () => {
     }
   })
 
+  it('should work with v-for on dynamic component', done => {
+    components.test3 = {
+      id: 'test3',
+      template: `<test1 v-if="!normal"></test1><div v-else>test3</div>`,
+      data () {
+        return { normal: false }
+      },
+      components: { test1: components.test }
+    }
+    // a flag that representing whether to test component content or not
+    let testContent = false
+
+    const vm = new Vue({
+      template: `
+        <div>
+          <component
+            v-for="(item, index) in items"
+            :key="index"
+            :is="item"
+            ref="children">
+          </component>
+        </div>
+      `,
+      data: {
+        items: ['test2', 'test3']
+      },
+      components
+    }).$mount()
+    assertRefs()
+    expect(vm.$refs.children[0].$el.textContent).toBe('test2')
+    expect(vm.$refs.children[1].$el.textContent).toBe('test')
+    // updating
+    vm.$refs.children[1].normal = true
+    testContent = true
+    waitForUpdate(assertRefs)
+      .then(() => { vm.items.push('test') })
+      .then(assertRefs)
+      .then(done)
+
+    function assertRefs () {
+      expect(Array.isArray(vm.$refs.children)).toBe(true)
+      expect(vm.$refs.children.length).toBe(vm.items.length)
+      if (testContent) {
+        expect(
+          vm.$refs.children.every((comp, i) => comp.$el.textContent === vm.items[i])
+        ).toBe(true)
+      }
+    }
+  })
+
   it('should register on component with empty roots', () => {
     const vm = new Vue({
       template: '<child ref="test"></child>',