Skip to content

Commit 9a132a9

Browse files
committed
enteredView/leftView hooks & test
1 parent 7cf1788 commit 9a132a9

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

src/transition.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var transition = module.exports = function (el, stage, changeState, compiler) {
4242
return applyTransitionClass(
4343
el,
4444
stage,
45-
changeState
45+
changeState,
46+
compiler
4647
)
4748
} else {
4849
changeState()
@@ -56,7 +57,7 @@ transition.codes = codes
5657
/**
5758
* Togggle a CSS class to trigger transition
5859
*/
59-
function applyTransitionClass (el, stage, changeState) {
60+
function applyTransitionClass (el, stage, changeState, compiler) {
6061

6162
if (!endEvent) {
6263
changeState()
@@ -78,6 +79,7 @@ function applyTransitionClass (el, stage, changeState) {
7879
classList.add(enterClass)
7980
// append
8081
changeState()
82+
compiler.execHook('enteredView')
8183
// force a layout so transition can be triggered
8284
/* jshint unused: false */
8385
var forceLayout = el.clientHeight
@@ -96,6 +98,7 @@ function applyTransitionClass (el, stage, changeState) {
9698
// actually remove node here
9799
changeState()
98100
classList.remove(leaveClass)
101+
compiler.execHook('leftView')
99102
}
100103
}
101104
// attach transition end listener
@@ -120,20 +123,30 @@ function applyTransitionFunctions (el, stage, changeState, functionId, compiler)
120123

121124
if (stage > 0) { // enter
122125
if (typeof enter !== 'function') {
123-
changeState()
126+
doEnter()
124127
return codes.JS_SKIP_E
125128
}
126-
enter(el, changeState)
129+
enter(el, doEnter)
127130
return codes.JS_E
128131
} else { // leave
129132
if (typeof leave !== 'function') {
130-
changeState()
133+
doLeave()
131134
return codes.JS_SKIP_L
132135
}
133-
leave(el, changeState)
136+
leave(el, doLeave)
134137
return codes.JS_L
135138
}
136139

140+
function doEnter () {
141+
compiler.execHook('enteredView')
142+
changeState()
143+
}
144+
145+
function doLeave () {
146+
compiler.execHook('leftView')
147+
changeState()
148+
}
149+
137150
}
138151

139152
/**

test/unit/specs/transition.js

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('UNIT: Transition', function () {
4848
c.called = true
4949
assert.ok(el.classList.contains(enterClass))
5050
}),
51+
compiler = mockCompiler(),
5152
code,
5253
cbCalled = false
5354
el.vue_trans_cb = function () {
@@ -56,7 +57,7 @@ describe('UNIT: Transition', function () {
5657
el.addEventListener(endEvent, el.vue_trans_cb)
5758

5859
it('should add the class before calling changeState()', function () {
59-
code = transition(el, 1, c.change, {})
60+
code = transition(el, 1, c.change, compiler)
6061
assert.ok(c.called)
6162
})
6263

@@ -75,13 +76,18 @@ describe('UNIT: Transition', function () {
7576
assert.strictEqual(code, codes.CSS_E)
7677
})
7778

79+
it('should have called enteredView hook', function () {
80+
assert.ok(compiler.enteredView)
81+
})
82+
7883
})
7984

8085
describe('leave', function () {
8186

8287
var el = mockEl('css'),
8388
c = mockChange(),
84-
code = transition(el, -1, c.change, {})
89+
compiler = mockCompiler(),
90+
code = transition(el, -1, c.change, compiler)
8591

8692
it('should attach an ontransitionend listener', function () {
8793
assert.ok(typeof el.vue_trans_cb === 'function')
@@ -112,6 +118,10 @@ describe('UNIT: Transition', function () {
112118
assert.strictEqual(code, codes.CSS_L)
113119
})
114120

121+
it('should have called leftView hook', function () {
122+
assert.ok(compiler.leftView)
123+
})
124+
115125
})
116126

117127
})
@@ -120,29 +130,19 @@ describe('UNIT: Transition', function () {
120130

121131
it('should skip if correspinding option is not defined', function () {
122132
var c = mockChange(),
123-
code = transition(mockEl('js'), 1, c.change, {
124-
getOption: function () {}
125-
})
133+
code = transition(mockEl('js'), 1, c.change, mockCompiler())
126134
assert.ok(c.called)
127135
assert.strictEqual(code, codes.JS_SKIP)
128136
})
129137

130138
it('should skip if the option is given but the enter/leave func is not defined', function () {
131139
var c = mockChange(),
132-
code = transition(mockEl('js'), 1, c.change, {
133-
getOption: function () {
134-
return {}
135-
}
136-
})
140+
code = transition(mockEl('js'), 1, c.change, mockCompiler({}))
137141
assert.ok(c.called)
138142
assert.strictEqual(code, codes.JS_SKIP_E)
139143

140144
c = mockChange()
141-
code = transition(mockEl('js'), -1, c.change, {
142-
getOption: function () {
143-
return {}
144-
}
145-
})
145+
code = transition(mockEl('js'), -1, c.change, mockCompiler({}))
146146
assert.ok(c.called)
147147
assert.strictEqual(code, codes.JS_SKIP_L)
148148
})
@@ -157,21 +157,22 @@ describe('UNIT: Transition', function () {
157157
assert.strictEqual(el, element)
158158
change()
159159
}
160-
}
160+
},
161+
compiler = mockCompiler(def)
161162

162163
it('should call the enter function', function () {
163-
code = transition(el, 1, c.change, {
164-
getOption: function () {
165-
return def
166-
}
167-
})
164+
code = transition(el, 1, c.change, compiler)
168165
assert.ok(c.called)
169166
})
170167

171168
it('should return correct code', function () {
172169
assert.strictEqual(code, codes.JS_E)
173170
})
174171

172+
it('should have called enteredView hook', function () {
173+
assert.ok(compiler.enteredView)
174+
})
175+
175176
})
176177

177178
describe('leave', function () {
@@ -184,21 +185,22 @@ describe('UNIT: Transition', function () {
184185
assert.strictEqual(el, element)
185186
change()
186187
}
187-
}
188+
},
189+
compiler = mockCompiler(def)
188190

189191
it('should call the leave function', function () {
190-
code = transition(el, -1, c.change, {
191-
getOption: function () {
192-
return def
193-
}
194-
})
192+
code = transition(el, -1, c.change, compiler)
195193
assert.ok(c.called)
196194
})
197195

198196
it('should return correct code', function () {
199197
assert.strictEqual(code, codes.JS_L)
200198
})
201199

200+
it('should have called leftView hook', function () {
201+
assert.ok(compiler.leftView)
202+
})
203+
202204
})
203205

204206
})
@@ -225,6 +227,17 @@ describe('UNIT: Transition', function () {
225227
return el
226228
}
227229

230+
function mockCompiler (opt) {
231+
return {
232+
getOption: function () {
233+
return opt
234+
},
235+
execHook: function (hook) {
236+
this[hook] = true
237+
}
238+
}
239+
}
240+
228241
function sniffTransitionEndEvent () {
229242
var el = document.createElement('vue'),
230243
defaultEvent = 'transitionend',

0 commit comments

Comments
 (0)