Skip to content

Commit 7570a3c

Browse files
committed
test for directive refactor (fix #3848)
1 parent 1fa3844 commit 7570a3c

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

src/core/vdom/helpers.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ export function getFirstComponentChild (children: ?Array<any>) {
6868
export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) {
6969
key = key + hookKey
7070
const injectedHash = def.__injected || (def.__injected = {})
71-
if (injectedHash[key]) return
72-
injectedHash[key] = true
73-
const oldHook = def[hookKey]
74-
if (oldHook) {
75-
def[hookKey] = function () {
76-
oldHook.apply(this, arguments)
77-
hook.apply(this, arguments)
71+
if (!injectedHash[key]) {
72+
injectedHash[key] = true
73+
const oldHook = def[hookKey]
74+
if (oldHook) {
75+
def[hookKey] = function () {
76+
oldHook.apply(this, arguments)
77+
hook.apply(this, arguments)
78+
}
79+
} else {
80+
def[hookKey] = hook
7881
}
79-
} else {
80-
def[hookKey] = hook
8182
}
8283
}
8384

test/unit/features/options/directives.spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,76 @@ describe('Options directives', () => {
151151
}).then(done)
152152
})
153153

154+
it('should properly handle same node with different directive sets', done => {
155+
const spies = {}
156+
const createSpy = name => (spies[name] = jasmine.createSpy(name))
157+
const vm = new Vue({
158+
data: {
159+
ok: true,
160+
val: 123
161+
},
162+
template: `
163+
<div>
164+
<div v-if="ok" v-test="val" v-test.hi="val"></div>
165+
<div v-if="!ok" v-test.hi="val" v-test2="val"></div>
166+
</div>
167+
`,
168+
directives: {
169+
test: {
170+
bind: createSpy('bind1'),
171+
inserted: createSpy('inserted1'),
172+
update: createSpy('update1'),
173+
componentUpdated: createSpy('componentUpdated1'),
174+
unbind: createSpy('unbind1')
175+
},
176+
test2: {
177+
bind: createSpy('bind2'),
178+
inserted: createSpy('inserted2'),
179+
update: createSpy('update2'),
180+
componentUpdated: createSpy('componentUpdated2'),
181+
unbind: createSpy('unbind2')
182+
}
183+
}
184+
}).$mount()
185+
186+
expect(spies.bind1.calls.count()).toBe(2)
187+
expect(spies.inserted1.calls.count()).toBe(2)
188+
expect(spies.bind2.calls.count()).toBe(0)
189+
expect(spies.inserted2.calls.count()).toBe(0)
190+
191+
vm.ok = false
192+
waitForUpdate(() => {
193+
// v-test with modifier should be updated
194+
expect(spies.update1.calls.count()).toBe(1)
195+
expect(spies.componentUpdated1.calls.count()).toBe(1)
196+
197+
// v-test without modifier should be unbound
198+
expect(spies.unbind1.calls.count()).toBe(1)
199+
200+
// v-test2 should be bound
201+
expect(spies.bind2.calls.count()).toBe(1)
202+
expect(spies.inserted2.calls.count()).toBe(1)
203+
204+
vm.ok = true
205+
}).then(() => {
206+
// v-test without modifier should be bound again
207+
expect(spies.bind1.calls.count()).toBe(3)
208+
expect(spies.inserted1.calls.count()).toBe(3)
209+
210+
// v-test2 should be unbound
211+
expect(spies.unbind2.calls.count()).toBe(1)
212+
213+
// v-test with modifier should be updated again
214+
expect(spies.update1.calls.count()).toBe(2)
215+
expect(spies.componentUpdated1.calls.count()).toBe(2)
216+
217+
vm.val = 234
218+
}).then(() => {
219+
expect(spies.update1.calls.count()).toBe(4)
220+
expect(spies.componentUpdated1.calls.count()).toBe(4)
221+
}).then(done)
222+
})
223+
154224
it('warn non-existent', () => {
155225
new Vue({
156226
template: '<div v-test></div>'

0 commit comments

Comments
 (0)