summaryrefslogtreecommitdiff
path: root/spec/frontend/environments/kubernetes_agent_info_spec.js
blob: b1795065281ab8691fff3b5da4965fa9d09992b9 (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
123
124
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { GlIcon, GlLink, GlSprintf, GlLoadingIcon, GlAlert } from '@gitlab/ui';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import KubernetesAgentInfo from '~/environments/components/kubernetes_agent_info.vue';
import { AGENT_STATUSES, ACTIVE_CONNECTION_TIME } from '~/clusters_list/constants';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import getK8sClusterAgentQuery from '~/environments/graphql/queries/k8s_cluster_agent.query.graphql';

Vue.use(VueApollo);

const propsData = {
  agentName: 'my-agent',
  agentId: '1',
  agentProjectPath: 'path/to/agent-config-project',
};

const mockClusterAgent = {
  id: '1',
  name: 'token-1',
  webPath: 'path/to/agent-page',
};

const connectedTimeNow = new Date();
const connectedTimeInactive = new Date(connectedTimeNow.getTime() - ACTIVE_CONNECTION_TIME);

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

  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findAgentLink = () => wrapper.findComponent(GlLink);
  const findAgentStatus = () => wrapper.findByTestId('agent-status');
  const findAgentStatusIcon = () => findAgentStatus().findComponent(GlIcon);
  const findAgentLastUsedDate = () => wrapper.findByTestId('agent-last-used-date');
  const findAlert = () => wrapper.findComponent(GlAlert);

  const createWrapper = ({ tokens = [], queryResponse = null } = {}) => {
    const clusterAgent = { ...mockClusterAgent, tokens: { nodes: tokens } };

    agentQueryResponse =
      queryResponse ||
      jest.fn().mockResolvedValue({ data: { project: { id: 'project-1', clusterAgent } } });
    const apolloProvider = createMockApollo([[getK8sClusterAgentQuery, agentQueryResponse]]);

    wrapper = extendedWrapper(
      shallowMount(KubernetesAgentInfo, {
        apolloProvider,
        propsData,
        stubs: { TimeAgoTooltip, GlSprintf },
      }),
    );
  };

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

    it('shows loading icon while fetching the agent details', async () => {
      expect(findLoadingIcon().exists()).toBe(true);
      await waitForPromises();
      expect(findLoadingIcon().exists()).toBe(false);
    });

    it('sends expected params', async () => {
      await waitForPromises();

      const variables = {
        agentName: propsData.agentName,
        projectPath: propsData.agentProjectPath,
      };

      expect(agentQueryResponse).toHaveBeenCalledWith(variables);
    });

    it('renders the agent name with the link', async () => {
      await waitForPromises();

      expect(findAgentLink().attributes('href')).toBe(mockClusterAgent.webPath);
      expect(findAgentLink().text()).toContain(mockClusterAgent.id);
    });
  });

  describe.each`
    lastUsedAt               | status        | lastUsedText
    ${null}                  | ${'unused'}   | ${KubernetesAgentInfo.i18n.neverConnectedText}
    ${connectedTimeNow}      | ${'active'}   | ${'just now'}
    ${connectedTimeInactive} | ${'inactive'} | ${'8 minutes ago'}
  `('when agent connection status is "$status"', ({ lastUsedAt, status, lastUsedText }) => {
    beforeEach(async () => {
      const tokens = [{ id: 'token-id', lastUsedAt }];
      createWrapper({ tokens });
      await waitForPromises();
    });

    it('displays correct status text', () => {
      expect(findAgentStatus().text()).toBe(AGENT_STATUSES[status].name);
    });

    it('displays correct status icon', () => {
      expect(findAgentStatusIcon().props('name')).toBe(AGENT_STATUSES[status].icon);
      expect(findAgentStatusIcon().attributes('class')).toBe(AGENT_STATUSES[status].class);
    });

    it('displays correct last used date status', () => {
      expect(findAgentLastUsedDate().text()).toBe(lastUsedText);
    });
  });

  describe('when the agent query has errored', () => {
    beforeEach(() => {
      createWrapper({ clusterAgent: null, queryResponse: jest.fn().mockRejectedValue() });
      return waitForPromises();
    });

    it('displays an alert message', () => {
      expect(findAlert().text()).toBe(KubernetesAgentInfo.i18n.loadingError);
    });
  });
});