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: 43e88650ae3986e6c2996a1ffdd44cb699e4ad5c (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { shallowMount } from '@vue/test-utils';
import { merge } from 'lodash';
import Vue from 'vue';
import Vuex from 'vuex';
import { getParameterByName } from '~/lib/utils/url_utility';
import AppIndex from '~/releases/components/app_index.vue';
import ReleaseSkeletonLoader from '~/releases/components/release_skeleton_loader.vue';
import ReleasesPagination from '~/releases/components/releases_pagination.vue';
import ReleasesSort from '~/releases/components/releases_sort.vue';

jest.mock('~/lib/utils/url_utility', () => ({
  ...jest.requireActual('~/lib/utils/url_utility'),
  getParameterByName: jest.fn(),
}));

Vue.use(Vuex);

describe('app_index.vue', () => {
  let wrapper;
  let fetchReleasesSpy;
  let urlParams;

  const createComponent = (storeUpdates) => {
    wrapper = shallowMount(AppIndex, {
      store: new Vuex.Store({
        modules: {
          index: merge(
            {
              namespaced: true,
              actions: {
                fetchReleases: fetchReleasesSpy,
              },
              state: {
                isLoading: true,
                releases: [],
              },
            },
            storeUpdates,
          ),
        },
      }),
    });
  };

  beforeEach(() => {
    fetchReleasesSpy = jest.fn();
    getParameterByName.mockImplementation((paramName) => urlParams[paramName]);
  });

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

  // Finders
  const findLoadingIndicator = () => wrapper.find(ReleaseSkeletonLoader);
  const findEmptyState = () => wrapper.find('[data-testid="empty-state"]');
  const findSuccessState = () => wrapper.find('[data-testid="success-state"]');
  const findPagination = () => wrapper.find(ReleasesPagination);
  const findSortControls = () => wrapper.find(ReleasesSort);
  const findNewReleaseButton = () => wrapper.find('[data-testid="new-release-button"]');

  // Expectations
  const expectLoadingIndicator = (shouldExist) => {
    it(`${shouldExist ? 'renders' : 'does not render'} a loading indicator`, () => {
      expect(findLoadingIndicator().exists()).toBe(shouldExist);
    });
  };

  const expectEmptyState = (shouldExist) => {
    it(`${shouldExist ? 'renders' : 'does not render'} an empty state`, () => {
      expect(findEmptyState().exists()).toBe(shouldExist);
    });
  };

  const expectSuccessState = (shouldExist) => {
    it(`${shouldExist ? 'renders' : 'does not render'} the success state`, () => {
      expect(findSuccessState().exists()).toBe(shouldExist);
    });
  };

  const expectPagination = (shouldExist) => {
    it(`${shouldExist ? 'renders' : 'does not render'} the pagination controls`, () => {
      expect(findPagination().exists()).toBe(shouldExist);
    });
  };

  const expectNewReleaseButton = (shouldExist) => {
    it(`${shouldExist ? 'renders' : 'does not render'} the "New release" button`, () => {
      expect(findNewReleaseButton().exists()).toBe(shouldExist);
    });
  };

  // Tests
  describe('on startup', () => {
    it.each`
      before                  | after
      ${null}                 | ${null}
      ${'before_param_value'} | ${null}
      ${null}                 | ${'after_param_value'}
    `(
      'calls fetchRelease with the correct parameters based on the curent query parameters: before: $before, after: $after',
      ({ before, after }) => {
        urlParams = { before, after };

        createComponent();

        expect(fetchReleasesSpy).toHaveBeenCalledTimes(1);
        expect(fetchReleasesSpy).toHaveBeenCalledWith(expect.anything(), urlParams);
      },
    );
  });

  describe('when the request to fetch releases has not yet completed', () => {
    beforeEach(() => {
      createComponent();
    });

    expectLoadingIndicator(true);
    expectEmptyState(false);
    expectSuccessState(false);
    expectPagination(false);
  });

  describe('when the request fails', () => {
    beforeEach(() => {
      createComponent({
        state: {
          isLoading: false,
          hasError: true,
        },
      });
    });

    expectLoadingIndicator(false);
    expectEmptyState(false);
    expectSuccessState(false);
    expectPagination(true);
  });

  describe('when the request succeeds but returns no releases', () => {
    beforeEach(() => {
      createComponent({
        state: {
          isLoading: false,
        },
      });
    });

    expectLoadingIndicator(false);
    expectEmptyState(true);
    expectSuccessState(false);
    expectPagination(true);
  });

  describe('when the request succeeds and includes at least one release', () => {
    beforeEach(() => {
      createComponent({
        state: {
          isLoading: false,
          releases: [{}],
        },
      });
    });

    expectLoadingIndicator(false);
    expectEmptyState(false);
    expectSuccessState(true);
    expectPagination(true);
  });

  describe('sorting', () => {
    beforeEach(() => {
      createComponent();
    });

    it('renders the sort controls', () => {
      expect(findSortControls().exists()).toBe(true);
    });

    it('calls the fetchReleases store method when the sort is updated', () => {
      fetchReleasesSpy.mockClear();

      findSortControls().vm.$emit('sort:changed');

      expect(fetchReleasesSpy).toHaveBeenCalledTimes(1);
    });
  });

  describe('"New release" button', () => {
    describe('when the user is allowed to create releases', () => {
      const newReleasePath = 'path/to/new/release/page';

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

      expectNewReleaseButton(true);

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

    describe('when the user is not allowed to create releases', () => {
      beforeEach(() => {
        createComponent();
      });

      expectNewReleaseButton(false);
    });
  });

  describe("when the browser's back button is pressed", () => {
    beforeEach(() => {
      urlParams = {
        before: 'before_param_value',
      };

      createComponent();

      fetchReleasesSpy.mockClear();

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

    it('calls the fetchRelease store method with the parameters from the URL query', () => {
      expect(fetchReleasesSpy).toHaveBeenCalledTimes(1);
      expect(fetchReleasesSpy).toHaveBeenCalledWith(expect.anything(), urlParams);
    });
  });
});