1
1
import Vue from 'vue'
2
- import { mount , shallowMount } from '@vue/test-utils' ;
2
+ import { shallowMount } from '@vue/test-utils' ;
3
3
import CoreuiVue from '@coreui/vue'
4
4
import ProgressBars from '@/views/base/ProgressBars'
5
5
@@ -8,6 +8,9 @@ Vue.use(CoreuiVue)
8
8
jest . useFakeTimers ( )
9
9
10
10
describe ( 'ProgressBars.vue' , ( ) => {
11
+ // mount it once, to make test efficient & faster
12
+ const wrapper = shallowMount ( ProgressBars )
13
+
11
14
it ( 'has a name' , ( ) => {
12
15
expect ( ProgressBars . name ) . toBe ( 'ProgressBars' )
13
16
} )
@@ -18,23 +21,34 @@ describe('ProgressBars.vue', () => {
18
21
expect ( typeof ProgressBars . data ) . toMatch ( 'function' )
19
22
} )
20
23
it ( 'is Vue instance' , ( ) => {
21
- const wrapper = mount ( ProgressBars )
22
24
expect ( wrapper . vm ) . toBeTruthy ( )
23
25
} )
24
26
it ( 'is ProgressBars' , ( ) => {
25
- const wrapper = mount ( ProgressBars )
26
27
expect ( wrapper . findComponent ( ProgressBars ) ) . toBeTruthy ( )
27
28
} )
28
29
test ( 'renders correctly' , ( ) => {
29
- const wrapper = shallowMount ( ProgressBars )
30
+ // mock Math.random() to always return 1
31
+ jest . spyOn ( global . Math , 'random' ) . mockReturnValue ( 1 )
32
+
30
33
expect ( wrapper . element ) . toMatchSnapshot ( )
31
- } )
32
- it ( 'should be destroyed' , ( ) => {
33
- const wrapper = mount ( ProgressBars )
34
- wrapper . destroy ( )
34
+
35
+ // restore Math.random()
36
+ jest . spyOn ( global . Math , 'random' ) . mockRestore ( )
35
37
} )
36
38
it ( 'should have methods' , ( ) => {
37
39
expect ( typeof ProgressBars . methods . clicked ) . toEqual ( 'function' )
38
40
expect ( ProgressBars . methods . clicked ( ) ) . toBeUndefined ( )
39
41
} )
42
+ it ( 'should execute mounted' , ( ) => {
43
+ // mock interval 2000 ms
44
+ jest . advanceTimersByTime ( 2000 ) ;
45
+
46
+ expect ( setInterval ) . toHaveBeenCalled ( ) ;
47
+ expect ( setInterval ) . toHaveBeenLastCalledWith ( expect . any ( Function ) , 2000 )
48
+ } )
49
+
50
+ // this test should be the last
51
+ it ( 'should be destroyed' , ( ) => {
52
+ wrapper . destroy ( )
53
+ } )
40
54
} )
0 commit comments