summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/kubernetes_overview_spec.js
blob: 1912fd4a82bd051964e8ed761069ea17d101807a (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { nextTick } from 'vue';
import { shallowMount } from '@vue/test-utils';
import { GlCollapse, GlButton, GlAlert } from '@gitlab/ui';
import KubernetesOverview from '~/environments/components/kubernetes_overview.vue';
import KubernetesAgentInfo from '~/environments/components/kubernetes_agent_info.vue';
import KubernetesPods from '~/environments/components/kubernetes_pods.vue';
import { agent } from './graphql/mock_data';
import { mockKasTunnelUrl } from './mock_data';

const propsData = {
  agentId: agent.id,
  agentName: agent.name,
  agentProjectPath: agent.project,
  namespace: agent.kubernetesNamespace,
};

const provide = {
  kasTunnelUrl: mockKasTunnelUrl,
};

const configuration = {
  basePath: provide.kasTunnelUrl.replace(/\/$/, ''),
  baseOptions: {
    headers: { 'GitLab-Agent-Id': '1' },
  },
};

describe('~/environments/components/kubernetes_overview.vue', () => {
  let wrapper;

  const findCollapse = () => wrapper.findComponent(GlCollapse);
  const findCollapseButton = () => wrapper.findComponent(GlButton);
  const findAgentInfo = () => wrapper.findComponent(KubernetesAgentInfo);
  const findKubernetesPods = () => wrapper.findComponent(KubernetesPods);
  const findAlert = () => wrapper.findComponent(GlAlert);

  const createWrapper = () => {
    wrapper = shallowMount(KubernetesOverview, {
      propsData,
      provide,
    });
  };

  const toggleCollapse = async () => {
    findCollapseButton().vm.$emit('click');
    await nextTick();
  };

  describe('default', () => {
    beforeEach(() => {
      createWrapper();
    });

    it('renders the kubernetes overview title', () => {
      expect(wrapper.text()).toBe(KubernetesOverview.i18n.sectionTitle);
    });
  });

  describe('collapse', () => {
    beforeEach(() => {
      createWrapper();
    });

    it('is collapsed by default', () => {
      expect(findCollapse().props('visible')).toBeUndefined();
      expect(findCollapseButton().attributes('aria-label')).toBe(KubernetesOverview.i18n.expand);
      expect(findCollapseButton().props('icon')).toBe('chevron-right');
    });

    it("doesn't render components when the collapse is not visible", () => {
      expect(findAgentInfo().exists()).toBe(false);
      expect(findKubernetesPods().exists()).toBe(false);
    });

    it('opens on click', async () => {
      findCollapseButton().vm.$emit('click');
      await nextTick();

      expect(findCollapse().attributes('visible')).toBe('true');
      expect(findCollapseButton().attributes('aria-label')).toBe(KubernetesOverview.i18n.collapse);
      expect(findCollapseButton().props('icon')).toBe('chevron-down');
    });
  });

  describe('when section is expanded', () => {
    beforeEach(() => {
      createWrapper();
      toggleCollapse();
    });

    it('renders kubernetes agent info', () => {
      expect(findAgentInfo().props()).toEqual({
        agentName: agent.name,
        agentId: agent.id,
        agentProjectPath: agent.project,
      });
    });

    it('renders kubernetes pods', () => {
      expect(findKubernetesPods().props()).toEqual({
        namespace: agent.kubernetesNamespace,
        configuration,
      });
    });
  });

  describe('on cluster error', () => {
    beforeEach(() => {
      createWrapper();
      toggleCollapse();
    });

    it('shows alert with the error message', async () => {
      const error = 'Error message from pods';

      findKubernetesPods().vm.$emit('cluster-error', error);
      await nextTick();

      expect(findAlert().text()).toBe(error);
    });
  });
});