Welcome to mirror list, hosted at ThFree Co, Russian Federation.

app_index_spec.js « components « releases « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b5270e29d6b58f19430cf4519636237dbd1bcbc (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { shallowMount, createLocalVue } from '@vue/test-utils';
import { range as rge } from 'lodash';
import Vuex from 'vuex';
import { getJSONFixture } from 'helpers/fixtures';
import waitForPromises from 'helpers/wait_for_promises';
import api from '~/api';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import ReleasesApp from '~/releases/components/app_index.vue';
import ReleasesPagination from '~/releases/components/releases_pagination.vue';
import createStore from '~/releases/stores';
import createListModule from '~/releases/stores/modules/list';
import { pageInfoHeadersWithoutPagination, pageInfoHeadersWithPagination } from '../mock_data';

jest.mock('~/lib/utils/common_utils', () => ({
  ...jest.requireActual('~/lib/utils/common_utils'),
  getParameterByName: jest.fn().mockImplementation((paramName) => {
    return `${paramName}_param_value`;
  }),
}));

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

const release = getJSONFixture('api/releases/release.json');
const releases = [release];

describe('Releases App ', () => {
  let wrapper;
  let fetchReleaseSpy;

  const paginatedReleases = rge(21).map((index) => ({
    ...convertObjectPropsToCamelCase(release, { deep: true }),
    tagName: `${index}.00`,
  }));

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

  const createComponent = (stateUpdates = {}) => {
    const listModule = createListModule({
      ...defaultInitialState,
      ...stateUpdates,
    });

    fetchReleaseSpy = jest.spyOn(listModule.actions, 'fetchReleases');

    const store = createStore({
      modules: { list: listModule },
      featureFlags: {
        graphqlReleaseData: true,
        graphqlReleasesPage: false,
        graphqlMilestoneStats: true,
      },
    });

    wrapper = shallowMount(ReleasesApp, {
      store,
      localVue,
    });
  };

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

  describe('on startup', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        .mockResolvedValue({ data: releases, headers: pageInfoHeadersWithoutPagination });

      createComponent();
    });

    it('calls fetchRelease with the page, before, and after parameters', () => {
      expect(fetchReleaseSpy).toHaveBeenCalledTimes(1);
      expect(fetchReleaseSpy).toHaveBeenCalledWith(expect.anything(), {
        page: 'page_param_value',
        before: 'before_param_value',
        after: 'after_param_value',
      });
    });
  });

  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: {} })));

      createComponent();
    });

    it('renders loading icon', () => {
      expect(wrapper.find('.js-loading').exists()).toBe(true);
      expect(wrapper.find('.js-empty-state').exists()).toBe(false);
      expect(wrapper.find('.js-success-state').exists()).toBe(false);
      expect(wrapper.find(ReleasesPagination).exists()).toBe(false);
    });
  });

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

      createComponent();
    });

    it('renders success state', () => {
      expect(wrapper.find('.js-loading').exists()).toBe(false);
      expect(wrapper.find('.js-empty-state').exists()).toBe(false);
      expect(wrapper.find('.js-success-state').exists()).toBe(true);
      expect(wrapper.find(ReleasesPagination).exists()).toBe(true);
    });
  });

  describe('with successful request and pagination', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        .mockResolvedValue({ data: paginatedReleases, headers: pageInfoHeadersWithPagination });

      createComponent();
    });

    it('renders success state', () => {
      expect(wrapper.find('.js-loading').exists()).toBe(false);
      expect(wrapper.find('.js-empty-state').exists()).toBe(false);
      expect(wrapper.find('.js-success-state').exists()).toBe(true);
      expect(wrapper.find(ReleasesPagination).exists()).toBe(true);
    });
  });

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

      createComponent();
    });

    it('renders empty state', () => {
      expect(wrapper.find('.js-loading').exists()).toBe(false);
      expect(wrapper.find('.js-empty-state').exists()).toBe(true);
      expect(wrapper.find('.js-success-state').exists()).toBe(false);
    });
  });

  describe('"New release" button', () => {
    const findNewReleaseButton = () => wrapper.find('.js-new-release-btn');

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

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

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

      it('renders the "New release" button', () => {
        expect(findNewReleaseButton().exists()).toBe(true);
      });

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

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

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

  describe('when the back button is pressed', () => {
    beforeEach(() => {
      jest
        .spyOn(api, 'releases')
        .mockResolvedValue({ data: releases, headers: pageInfoHeadersWithoutPagination });

      createComponent();

      fetchReleaseSpy.mockClear();

      window.dispatchEvent(new PopStateEvent('popstate'));
    });

    it('calls fetchRelease with the page parameter', () => {
      expect(fetchReleaseSpy).toHaveBeenCalledTimes(1);
      expect(fetchReleaseSpy).toHaveBeenCalledWith(expect.anything(), {
        page: 'page_param_value',
        before: 'before_param_value',
        after: 'after_param_value',
      });
    });
  });
});