1
1
const Vue = require ( 'vue/dist/vue.common.js' ) ;
2
2
const {
3
- onCreated,
4
3
onBeforeMount,
5
4
onMounted,
6
5
onBeforeUpdate,
7
6
onUpdated,
8
- onBeforeDestroy ,
9
- onDestroyed ,
7
+ onBeforeUnmount ,
8
+ onUnmounted ,
10
9
onErrorCaptured,
11
10
} = require ( '../../src' ) ;
12
11
13
12
describe ( 'Hooks lifecycle' , ( ) => {
14
- describe ( 'created' , ( ) => {
15
- it ( 'work with created option' , ( ) => {
16
- const spy = jest . fn ( ) ;
17
- new Vue ( {
18
- created ( ) {
19
- spy ( 'option' ) ;
20
- } ,
21
- setup ( ) {
22
- onCreated ( ( ) => spy ( 'hook' ) ) ;
23
- } ,
24
- } ) ;
25
- expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
26
- expect ( spy ) . toHaveBeenNthCalledWith ( 1 , 'option' ) ;
27
- expect ( spy ) . toHaveBeenNthCalledWith ( 2 , 'hook' ) ;
28
- } ) ;
29
-
30
- it ( 'can register multiple callbacks' , ( ) => {
31
- const spy = jest . fn ( ) ;
32
- new Vue ( {
33
- setup ( ) {
34
- onCreated ( ( ) => spy ( 'first' ) ) ;
35
- onCreated ( ( ) => spy ( 'second' ) ) ;
36
- } ,
37
- } ) ;
38
- expect ( spy . mock . calls . length ) . toBe ( 2 ) ;
39
- expect ( spy ) . toHaveBeenNthCalledWith ( 1 , 'first' ) ;
40
- expect ( spy ) . toHaveBeenNthCalledWith ( 2 , 'second' ) ;
41
- } ) ;
42
-
43
- it ( 'should have completed observation' , ( ) => {
44
- const spy = jest . fn ( ) ;
45
- new Vue ( {
46
- data ( ) {
47
- return {
48
- a : 1 ,
49
- } ;
50
- } ,
51
- setup ( _ , { _vm } ) {
52
- onCreated ( ( ) => {
53
- expect ( _vm . a ) . toBe ( 1 ) ;
54
- spy ( ) ;
55
- } ) ;
56
- } ,
57
- } ) ;
58
- expect ( spy ) . toHaveBeenCalled ( ) ;
59
- } ) ;
60
- } ) ;
61
-
62
13
describe ( 'beforeMount' , ( ) => {
63
14
it ( 'should not have mounted' , ( ) => {
64
15
const spy = jest . fn ( ) ;
@@ -198,7 +149,7 @@ describe('Hooks lifecycle', () => {
198
149
props : [ 'todo' ] ,
199
150
setup ( ) {
200
151
onBeforeUpdate ( beforeUpdate ) ;
201
- onDestroyed ( destroyed ) ;
152
+ onUnmounted ( destroyed ) ;
202
153
} ,
203
154
} ) ;
204
155
@@ -290,7 +241,7 @@ describe('Hooks lifecycle', () => {
290
241
props : [ 'todo' ] ,
291
242
setup ( ) {
292
243
onUpdated ( updated ) ;
293
- onDestroyed ( destroyed ) ;
244
+ onUnmounted ( destroyed ) ;
294
245
} ,
295
246
} ) ;
296
247
@@ -320,13 +271,13 @@ describe('Hooks lifecycle', () => {
320
271
} ) ;
321
272
} ) ;
322
273
323
- describe ( 'beforeDestroy ' , ( ) => {
274
+ describe ( 'beforeUnmount ' , ( ) => {
324
275
it ( 'should be called before destroy' , ( ) => {
325
276
const spy = jest . fn ( ) ;
326
277
const vm = new Vue ( {
327
278
render ( ) { } ,
328
279
setup ( _ , { _vm } ) {
329
- onBeforeDestroy ( ( ) => {
280
+ onBeforeUnmount ( ( ) => {
330
281
expect ( _vm . _isBeingDestroyed ) . toBe ( false ) ;
331
282
expect ( _vm . _isDestroyed ) . toBe ( false ) ;
332
283
spy ( ) ;
@@ -341,13 +292,13 @@ describe('Hooks lifecycle', () => {
341
292
} ) ;
342
293
} ) ;
343
294
344
- describe ( 'destroyed ' , ( ) => {
295
+ describe ( 'unmounted ' , ( ) => {
345
296
it ( 'should be called after destroy' , ( ) => {
346
297
const spy = jest . fn ( ) ;
347
298
const vm = new Vue ( {
348
299
render ( ) { } ,
349
300
setup ( _ , { _vm } ) {
350
- onDestroyed ( ( ) => {
301
+ onUnmounted ( ( ) => {
351
302
expect ( _vm . _isBeingDestroyed ) . toBe ( true ) ;
352
303
expect ( _vm . _isDestroyed ) . toBe ( true ) ;
353
304
spy ( ) ;
@@ -381,10 +332,8 @@ describe('Hooks lifecycle', () => {
381
332
const Child = {
382
333
setup ( _ , { _vm } ) {
383
334
child = _vm ;
384
- onCreated ( ( ) => {
385
- err = new Error ( 'child' ) ;
386
- throw err ;
387
- } ) ;
335
+ err = new Error ( 'child' ) ;
336
+ throw err ;
388
337
} ,
389
338
render ( ) { } ,
390
339
} ;
@@ -396,9 +345,9 @@ describe('Hooks lifecycle', () => {
396
345
render : h => h ( Child ) ,
397
346
} ) . $mount ( ) ;
398
347
399
- expect ( spy ) . toHaveBeenCalledWith ( err , child , 'created hook ' ) ;
348
+ expect ( spy ) . toHaveBeenCalledWith ( err , child , 'data() ' ) ;
400
349
// should propagate by default
401
- expect ( globalSpy ) . toHaveBeenCalledWith ( err , child , 'created hook ' ) ;
350
+ expect ( globalSpy ) . toHaveBeenCalledWith ( err , child , 'data() ' ) ;
402
351
} ) ;
403
352
} ) ;
404
353
} ) ;
0 commit comments