Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #5997: refactor function registerRef #6039

Merged
merged 2 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions src/core/vdom/modules/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

@javoski javoski Jul 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's seems that flow is a little weak to check array type, even codes below couldn't pass.

if (Array.isArray(refs[key])) {
  if (refs[key].indexOf(ref)) {
    refs[key].push(ref)
  }
}

So I added a suppress_comment option to .flowconfig.

}
} else {
refs[key] = ref
Expand Down
50 changes: 50 additions & 0 deletions test/unit/features/ref.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>',
Expand Down