ProgressBars.spec.js 1.5 KB

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