11import Vue from 'vue'
2- import { mount , shallowMount } from '@vue/test-utils' ;
2+ import { shallowMount } from '@vue/test-utils' ;
33import CoreuiVue from '@coreui/vue'
44import ProgressBars from '@/views/base/ProgressBars'
55
@@ -8,6 +8,9 @@ Vue.use(CoreuiVue)
88jest . useFakeTimers ( )
99
1010describe ( 'ProgressBars.vue' , ( ) => {
11+ // mount it once, to make test efficient & faster
12+ const wrapper = shallowMount ( ProgressBars )
13+
1114 it ( 'has a name' , ( ) => {
1215 expect ( ProgressBars . name ) . toBe ( 'ProgressBars' )
1316 } )
@@ -18,23 +21,34 @@ describe('ProgressBars.vue', () => {
1821 expect ( typeof ProgressBars . data ) . toMatch ( 'function' )
1922 } )
2023 it ( 'is Vue instance' , ( ) => {
21- const wrapper = mount ( ProgressBars )
2224 expect ( wrapper . vm ) . toBeTruthy ( )
2325 } )
2426 it ( 'is ProgressBars' , ( ) => {
25- const wrapper = mount ( ProgressBars )
2627 expect ( wrapper . findComponent ( ProgressBars ) ) . toBeTruthy ( )
2728 } )
2829 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+
3033 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 ( )
3537 } )
3638 it ( 'should have methods' , ( ) => {
3739 expect ( typeof ProgressBars . methods . clicked ) . toEqual ( 'function' )
3840 expect ( ProgressBars . methods . clicked ( ) ) . toBeUndefined ( )
3941 } )
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+ } )
4054} )
0 commit comments