summaryrefslogtreecommitdiff
path: root/spec/frontend/observability/skeleton_spec.js
blob: 5637c0e6d70179685bd18d39bbf89f6f7bdab1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { GlSkeletonLoader } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';

import ObservabilitySkeleton from '~/observability/components/skeleton/index.vue';
import DashboardsSkeleton from '~/observability/components/skeleton/dashboards.vue';
import ExploreSkeleton from '~/observability/components/skeleton/explore.vue';
import ManageSkeleton from '~/observability/components/skeleton/manage.vue';

import { SKELETON_VARIANT } from '~/observability/constants';

describe('ObservabilitySkeleton component', () => {
  let wrapper;

  const mountComponent = ({ ...props } = {}) => {
    wrapper = shallowMountExtended(ObservabilitySkeleton, {
      propsData: props,
    });
  };

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('on mount', () => {
    beforeEach(() => {
      jest.spyOn(global, 'setTimeout');
      mountComponent();
    });

    it('should call setTimeout on mount and show ObservabilitySkeleton if Observability UI is not loaded yet', () => {
      jest.runAllTimers();

      expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500);
      expect(wrapper.vm.loading).toBe(true);
      expect(wrapper.vm.timerId).not.toBeNull();
    });

    it('should call setTimeout on mount and dont show ObservabilitySkeleton if Observability UI is loaded', () => {
      wrapper.vm.loading = false;
      jest.runAllTimers();

      expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500);
      expect(wrapper.vm.loading).toBe(false);
      expect(wrapper.vm.timerId).not.toBeNull();
    });
  });

  describe('handleSkeleton', () => {
    it('will not show the skeleton if Observability UI is loaded before', () => {
      jest.spyOn(global, 'clearTimeout');
      mountComponent();
      wrapper.vm.handleSkeleton();
      expect(clearTimeout).toHaveBeenCalledWith(wrapper.vm.timerId);
    });

    it('will hide skeleton gracefully after 400ms if skeleton was present on screen before Observability UI', () => {
      jest.spyOn(global, 'setTimeout');
      mountComponent();
      jest.runAllTimers();
      wrapper.vm.handleSkeleton();
      jest.runAllTimers();

      expect(setTimeout).toHaveBeenCalledWith(wrapper.vm.hideSkeleton, 400);
      expect(wrapper.vm.loading).toBe(false);
    });
  });

  describe('skeleton variant', () => {
    it.each`
      skeletonType    | condition                                         | variant
      ${'dashboards'} | ${'variant is dashboards'}                        | ${SKELETON_VARIANT.DASHBOARDS}
      ${'explore'}    | ${'variant is explore'}                           | ${SKELETON_VARIANT.EXPLORE}
      ${'manage'}     | ${'variant is manage'}                            | ${SKELETON_VARIANT.MANAGE}
      ${'default'}    | ${'variant is not manage, dashboards or explore'} | ${'unknown'}
    `('should render $skeletonType skeleton if $condition', async ({ skeletonType, variant }) => {
      mountComponent({ variant });
      const showsDefaultSkeleton = ![
        SKELETON_VARIANT.DASHBOARDS,
        SKELETON_VARIANT.EXPLORE,
        SKELETON_VARIANT.MANAGE,
      ].includes(variant);
      expect(wrapper.findComponent(DashboardsSkeleton).exists()).toBe(
        skeletonType === SKELETON_VARIANT.DASHBOARDS,
      );
      expect(wrapper.findComponent(ExploreSkeleton).exists()).toBe(
        skeletonType === SKELETON_VARIANT.EXPLORE,
      );
      expect(wrapper.findComponent(ManageSkeleton).exists()).toBe(
        skeletonType === SKELETON_VARIANT.MANAGE,
      );

      expect(wrapper.findComponent(GlSkeletonLoader).exists()).toBe(showsDefaultSkeleton);
    });
  });
});