clusters_spec.js 8.1 KB
Newer Older
1 2
import MockAdapter from 'axios-mock-adapter';
import { mount } from '@vue/test-utils';
3 4 5 6 7 8
import {
  GlLoadingIcon,
  GlPagination,
  GlDeprecatedSkeletonLoading as GlSkeletonLoading,
  GlTable,
} from '@gitlab/ui';
9
import * as Sentry from '@sentry/browser';
10 11 12 13
import axios from '~/lib/utils/axios_utils';
import Clusters from '~/clusters_list/components/clusters.vue';
import ClusterStore from '~/clusters_list/store';
import { apiData } from '../mock_data';
14 15

describe('Clusters', () => {
16 17
  let mock;
  let store;
18 19
  let wrapper;

20 21
  const endpoint = 'some/endpoint';

22 23 24 25 26 27 28
  const entryData = {
    endpoint,
    imgTagsAwsText: 'AWS Icon',
    imgTagsDefaultText: 'Default Icon',
    imgTagsGcpText: 'GCP Icon',
  };

29
  const findLoader = () => wrapper.find(GlLoadingIcon);
30
  const findPaginatedButtons = () => wrapper.find(GlPagination);
31
  const findTable = () => wrapper.find(GlTable);
32
  const findStatuses = () => findTable().findAll('.js-status');
33

34
  const mockPollingApi = (response, body, header) => {
35
    mock.onGet(`${endpoint}?page=${header['x-page']}`).reply(response, body, header);
36
  };
37

38
  const mountWrapper = () => {
39
    store = ClusterStore(entryData);
40 41
    wrapper = mount(Clusters, { store });
    return axios.waitForAll();
42 43
  };

44 45 46 47 48 49 50 51
  const paginationHeader = (total = apiData.clusters.length, perPage = 20, currentPage = 1) => {
    return {
      'x-total': total,
      'x-per-page': perPage,
      'x-page': currentPage,
    };
  };

52 53
  let captureException;

54
  beforeEach(() => {
55 56
    captureException = jest.spyOn(Sentry, 'captureException');

57
    mock = new MockAdapter(axios);
58
    mockPollingApi(200, apiData, paginationHeader());
59 60 61 62 63 64 65

    return mountWrapper();
  });

  afterEach(() => {
    wrapper.destroy();
    mock.restore();
66
    captureException.mockRestore();
67 68 69
  });

  describe('clusters table', () => {
70 71
    describe('when data is loading', () => {
      beforeEach(() => {
72
        wrapper.vm.$store.state.loadingClusters = true;
73 74 75 76 77 78 79
        return wrapper.vm.$nextTick();
      });

      it('displays a loader instead of the table while loading', () => {
        expect(findLoader().exists()).toBe(true);
        expect(findTable().exists()).toBe(false);
      });
80 81 82 83 84 85 86
    });

    it('displays a table component', () => {
      expect(findTable().exists()).toBe(true);
    });

    it('renders the correct table headers', () => {
87
      const tableHeaders = wrapper.vm.fields;
88 89 90 91 92 93 94 95 96 97 98 99 100
      const headers = findTable().findAll('th');

      expect(headers.length).toBe(tableHeaders.length);

      tableHeaders.forEach((headerText, i) =>
        expect(headers.at(i).text()).toEqual(headerText.label),
      );
    });

    it('should stack on smaller devices', () => {
      expect(findTable().classes()).toContain('b-table-stacked-md');
    });
  });
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

  describe('cluster icon', () => {
    it.each`
      providerText      | lineNumber
      ${'GCP Icon'}     | ${0}
      ${'AWS Icon'}     | ${1}
      ${'Default Icon'} | ${2}
      ${'Default Icon'} | ${3}
      ${'Default Icon'} | ${4}
      ${'Default Icon'} | ${5}
    `('renders provider image and alt text for each cluster', ({ providerText, lineNumber }) => {
      const images = findTable().findAll('.js-status img');
      const image = images.at(lineNumber);

      expect(image.attributes('alt')).toBe(providerText);
    });
  });
118 119 120

  describe('cluster status', () => {
    it.each`
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      statusName    | lineNumber | result
      ${'creating'} | ${0}       | ${true}
      ${null}       | ${1}       | ${false}
      ${null}       | ${2}       | ${false}
      ${'deleting'} | ${3}       | ${true}
      ${null}       | ${4}       | ${false}
      ${null}       | ${5}       | ${false}
    `(
      'renders $result when status=$statusName and lineNumber=$lineNumber',
      ({ lineNumber, result }) => {
        const statuses = findStatuses();
        const status = statuses.at(lineNumber);
        expect(status.find(GlLoadingIcon).exists()).toBe(result);
      },
    );
136
  });
137

138
  describe('nodes present', () => {
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
    describe('nodes while loading', () => {
      it.each`
        nodeSize | lineNumber
        ${null}  | ${0}
        ${'1'}   | ${1}
        ${'2'}   | ${2}
        ${'1'}   | ${3}
        ${'1'}   | ${4}
        ${null}  | ${5}
      `('renders node size for each cluster', ({ nodeSize, lineNumber }) => {
        const sizes = findTable().findAll('td:nth-child(3)');
        const size = sizes.at(lineNumber);

        if (nodeSize) {
          expect(size.text()).toBe(nodeSize);
        } else {
          expect(size.find(GlSkeletonLoading).exists()).toBe(true);
        }
      });
    });

    describe('nodes finish loading', () => {
      beforeEach(() => {
        wrapper.vm.$store.state.loadingNodes = false;
        return wrapper.vm.$nextTick();
      });

      it.each`
        nodeSize     | lineNumber
        ${'Unknown'} | ${0}
        ${'1'}       | ${1}
        ${'2'}       | ${2}
        ${'1'}       | ${3}
        ${'1'}       | ${4}
        ${'Unknown'} | ${5}
      `('renders node size for each cluster', ({ nodeSize, lineNumber }) => {
        const sizes = findTable().findAll('td:nth-child(3)');
        const size = sizes.at(lineNumber);

        expect(size.text()).toBe(nodeSize);
        expect(size.find(GlSkeletonLoading).exists()).toBe(false);
      });
181
    });
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

    describe('nodes with unknown quantity', () => {
      it('notifies Sentry about all missing quantity types', () => {
        expect(captureException).toHaveBeenCalledTimes(8);
      });

      it('notifies Sentry about CPU missing quantity types', () => {
        const missingCpuTypeError = new Error('UnknownK8sCpuQuantity:1missingCpuUnit');

        expect(captureException).toHaveBeenCalledWith(missingCpuTypeError);
      });

      it('notifies Sentry about Memory missing quantity types', () => {
        const missingMemoryTypeError = new Error('UnknownK8sMemoryQuantity:1missingMemoryUnit');

        expect(captureException).toHaveBeenCalledWith(missingMemoryTypeError);
      });
    });
  });

  describe('cluster CPU', () => {
    it.each`
      clusterCpu           | lineNumber
      ${''}                | ${0}
      ${'1.93 (87% free)'} | ${1}
      ${'3.87 (86% free)'} | ${2}
      ${'(% free)'}        | ${3}
      ${'(% free)'}        | ${4}
      ${''}                | ${5}
    `('renders total cpu for each cluster', ({ clusterCpu, lineNumber }) => {
      const clusterCpus = findTable().findAll('td:nth-child(4)');
      const cpuData = clusterCpus.at(lineNumber);

      expect(cpuData.text()).toBe(clusterCpu);
    });
  });

  describe('cluster Memory', () => {
    it.each`
      clusterMemory         | lineNumber
      ${''}                 | ${0}
      ${'5.92 (78% free)'}  | ${1}
      ${'12.86 (79% free)'} | ${2}
      ${'(% free)'}         | ${3}
      ${'(% free)'}         | ${4}
      ${''}                 | ${5}
    `('renders total memory for each cluster', ({ clusterMemory, lineNumber }) => {
      const clusterMemories = findTable().findAll('td:nth-child(5)');
      const memoryData = clusterMemories.at(lineNumber);

      expect(memoryData.text()).toBe(clusterMemory);
    });
234 235
  });

236 237 238 239 240 241
  describe('pagination', () => {
    const perPage = apiData.clusters.length;
    const totalFirstPage = 100;
    const totalSecondPage = 500;

    beforeEach(() => {
242
      mockPollingApi(200, apiData, paginationHeader(totalFirstPage, perPage, 1));
243 244 245 246 247 248 249 250 251 252 253 254 255
      return mountWrapper();
    });

    it('should load to page 1 with header values', () => {
      const buttons = findPaginatedButtons();

      expect(buttons.props('perPage')).toBe(perPage);
      expect(buttons.props('totalItems')).toBe(totalFirstPage);
      expect(buttons.props('value')).toBe(1);
    });

    describe('when updating currentPage', () => {
      beforeEach(() => {
256
        mockPollingApi(200, apiData, paginationHeader(totalSecondPage, perPage, 2));
257 258 259 260 261 262 263 264 265 266 267 268 269
        wrapper.setData({ currentPage: 2 });
        return axios.waitForAll();
      });

      it('should change pagination when currentPage changes', () => {
        const buttons = findPaginatedButtons();

        expect(buttons.props('perPage')).toBe(perPage);
        expect(buttons.props('totalItems')).toBe(totalSecondPage);
        expect(buttons.props('value')).toBe(2);
      });
    });
  });
270
});