summaryrefslogtreecommitdiff
path: root/spec/frontend/terraform/components/terraform_list_spec.js
blob: 882b7b55b3e8cd36c0532d50a3501b96f22eb992 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { GlAlert, GlBadge, GlKeysetPagination, GlLoadingIcon, GlTab } from '@gitlab/ui';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import EmptyState from '~/terraform/components/empty_state.vue';
import StatesTable from '~/terraform/components/states_table.vue';
import TerraformList from '~/terraform/components/terraform_list.vue';
import getStatesQuery from '~/terraform/graphql/queries/get_states.query.graphql';

const localVue = createLocalVue();
localVue.use(VueApollo);

describe('TerraformList', () => {
  let wrapper;

  const propsData = {
    emptyStateImage: '/path/to/image',
    projectPath: 'path/to/project',
  };

  const createWrapper = ({ terraformStates, queryResponse = null }) => {
    const apolloQueryResponse = {
      data: {
        project: {
          terraformStates,
        },
      },
    };

    const mockResolvers = {
      TerraformState: {
        _showDetails: jest.fn().mockResolvedValue(false),
        errorMessages: jest.fn().mockResolvedValue([]),
        loadingLock: jest.fn().mockResolvedValue(false),
        loadingRemove: jest.fn().mockResolvedValue(false),
      },
      Mutation: {
        addDataToTerraformState: jest.fn().mockResolvedValue({}),
      },
    };

    const statsQueryResponse = queryResponse || jest.fn().mockResolvedValue(apolloQueryResponse);
    const apolloProvider = createMockApollo([[getStatesQuery, statsQueryResponse]], mockResolvers);

    wrapper = shallowMount(TerraformList, {
      localVue,
      apolloProvider,
      propsData,
    });
  };

  const findBadge = () => wrapper.find(GlBadge);
  const findEmptyState = () => wrapper.find(EmptyState);
  const findPaginationButtons = () => wrapper.find(GlKeysetPagination);
  const findStatesTable = () => wrapper.find(StatesTable);
  const findTab = () => wrapper.find(GlTab);

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

  describe('when the terraform query has succeeded', () => {
    describe('when there is a list of terraform states', () => {
      const states = [
        {
          _showDetails: false,
          errorMessages: [],
          id: 'gid://gitlab/Terraform::State/1',
          name: 'state-1',
          latestVersion: null,
          loadingLock: false,
          loadingRemove: false,
          lockedAt: null,
          lockedByUser: null,
          updatedAt: null,
        },
        {
          _showDetails: false,
          errorMessages: [],
          id: 'gid://gitlab/Terraform::State/2',
          name: 'state-2',
          latestVersion: null,
          loadingLock: false,
          loadingRemove: false,
          lockedAt: null,
          lockedByUser: null,
          updatedAt: null,
        },
      ];

      beforeEach(() => {
        createWrapper({
          terraformStates: {
            nodes: states,
            count: states.length,
            pageInfo: {
              hasNextPage: true,
              hasPreviousPage: false,
              startCursor: 'prev',
              endCursor: 'next',
            },
          },
        });

        return waitForPromises();
      });

      it('displays a states tab and count', () => {
        expect(findTab().text()).toContain('States');
        expect(findBadge().text()).toBe('2');
      });

      it('renders the states table and pagination buttons', () => {
        expect(findStatesTable().exists()).toBe(true);
        expect(findPaginationButtons().exists()).toBe(true);
      });

      describe('when list has no additional pages', () => {
        beforeEach(() => {
          createWrapper({
            terraformStates: {
              nodes: states,
              count: states.length,
              pageInfo: {
                hasNextPage: false,
                hasPreviousPage: false,
                startCursor: '',
                endCursor: '',
              },
            },
          });

          return waitForPromises();
        });

        it('renders the states table without pagination buttons', () => {
          expect(findStatesTable().exists()).toBe(true);
          expect(findPaginationButtons().exists()).toBe(false);
        });
      });
    });

    describe('when the list of terraform states is empty', () => {
      beforeEach(() => {
        createWrapper({
          terraformStates: {
            nodes: [],
            count: 0,
            pageInfo: null,
          },
        });

        return waitForPromises();
      });

      it('displays a states tab with no count', () => {
        expect(findTab().text()).toContain('States');
        expect(findBadge().exists()).toBe(false);
      });

      it('renders the empty state', () => {
        expect(findEmptyState().exists()).toBe(true);
      });
    });
  });

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

      return waitForPromises();
    });

    it('displays an alert message', () => {
      expect(wrapper.find(GlAlert).exists()).toBe(true);
    });
  });

  describe('when the terraform query is loading', () => {
    beforeEach(() => {
      createWrapper({
        terraformStates: null,
        queryResponse: jest.fn().mockReturnValue(new Promise(() => {})),
      });
    });

    it('displays a loading icon', () => {
      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });
  });
});