summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/app_index_spec.js
blob: 91beb5b14182215d90a99208e38e7309483c2bcc (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
import { range as rge } from 'lodash';
import Vue from 'vue';
import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import app from '~/releases/components/app_index.vue';
import createStore from '~/releases/stores';
import listModule from '~/releases/stores/modules/list';
import api from '~/api';
import { resetStore } from '../stores/modules/list/helpers';
import {
  pageInfoHeadersWithoutPagination,
  pageInfoHeadersWithPagination,
  release2 as release,
  releases,
} from '../mock_data';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import waitForPromises from 'helpers/wait_for_promises';

describe('Releases App ', () => {
  const Component = Vue.extend(app);
  let store;
  let vm;
  let releasesPagination;

  const props = {
    projectId: 'gitlab-ce',
    documentationPath: 'help/releases',
    illustrationPath: 'illustration/path',
  };

  beforeEach(() => {
    store = createStore({ modules: { list: listModule } });
    releasesPagination = rge(21).map(index => ({
      ...convertObjectPropsToCamelCase(release, { deep: true }),
      tagName: `${index}.00`,
    }));
  });

  afterEach(() => {
    resetStore(store);
    vm.$destroy();
  });

  describe('while loading', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        // Need to defer the return value here to the next stack,
        // otherwise the loading state disappears before our test even starts.
        .mockImplementation(() => waitForPromises().then(() => ({ data: [], headers: {} })));
      vm = mountComponentWithStore(Component, { props, store });
    });

    it('renders loading icon', () => {
      expect(vm.$el.querySelector('.js-loading')).not.toBeNull();
      expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
      expect(vm.$el.querySelector('.js-success-state')).toBeNull();
      expect(vm.$el.querySelector('.gl-pagination')).toBeNull();

      return waitForPromises();
    });
  });

  describe('with successful request', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        .mockResolvedValue({ data: releases, headers: pageInfoHeadersWithoutPagination });
      vm = mountComponentWithStore(Component, { props, store });
    });

    it('renders success state', () => {
      expect(vm.$el.querySelector('.js-loading')).toBeNull();
      expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
      expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
      expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
    });
  });

  describe('with successful request and pagination', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        .mockResolvedValue({ data: releasesPagination, headers: pageInfoHeadersWithPagination });
      vm = mountComponentWithStore(Component, { props, store });
    });

    it('renders success state', () => {
      expect(vm.$el.querySelector('.js-loading')).toBeNull();
      expect(vm.$el.querySelector('.js-empty-state')).toBeNull();
      expect(vm.$el.querySelector('.js-success-state')).not.toBeNull();
      expect(vm.$el.querySelector('.gl-pagination')).not.toBeNull();
    });
  });

  describe('with empty request', () => {
    beforeEach(() => {
      jest.spyOn(api, 'releases').mockResolvedValue({ data: [], headers: {} });
      vm = mountComponentWithStore(Component, { props, store });
    });

    it('renders empty state', () => {
      expect(vm.$el.querySelector('.js-loading')).toBeNull();
      expect(vm.$el.querySelector('.js-empty-state')).not.toBeNull();
      expect(vm.$el.querySelector('.js-success-state')).toBeNull();
      expect(vm.$el.querySelector('.gl-pagination')).toBeNull();
    });
  });

  describe('"New release" button', () => {
    const findNewReleaseButton = () => vm.$el.querySelector('.js-new-release-btn');

    beforeEach(() => {
      jest.spyOn(api, 'releases').mockResolvedValue({ data: [], headers: {} });
    });

    const factory = additionalProps => {
      vm = mountComponentWithStore(Component, {
        props: {
          ...props,
          ...additionalProps,
        },
        store,
      });
    };

    describe('when the user is allowed to create a new Release', () => {
      const newReleasePath = 'path/to/new/release';

      beforeEach(() => {
        factory({ newReleasePath });
      });

      it('renders the "New release" button', () => {
        expect(findNewReleaseButton()).not.toBeNull();
      });

      it('renders the "New release" button with the correct href', () => {
        expect(findNewReleaseButton().getAttribute('href')).toBe(newReleasePath);
      });
    });

    describe('when the user is not allowed to create a new Release', () => {
      beforeEach(() => factory());

      it('does not render the "New release" button', () => {
        expect(findNewReleaseButton()).toBeNull();
      });
    });
  });
});