gitlab-org--gitlab-foss/spec/frontend/releases/components/app_index_spec.js

211 lines
6.4 KiB
JavaScript
Raw Normal View History

import { range as rge } from 'lodash';
import Vuex from 'vuex';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { getJSONFixture } from 'helpers/fixtures';
import ReleasesApp from '~/releases/components/app_index.vue';
import createStore from '~/releases/stores';
import createListModule from '~/releases/stores/modules/list';
2018-12-20 04:31:32 -05:00
import api from '~/api';
import { pageInfoHeadersWithoutPagination, pageInfoHeadersWithPagination } from '../mock_data';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import ReleasesPagination from '~/releases/components/releases_pagination.vue';
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);
2018-12-20 04:31:32 -05:00
const release = getJSONFixture('api/releases/release.json');
const releases = [release];
2018-12-20 04:31:32 -05:00
describe('Releases App ', () => {
let wrapper;
let fetchReleaseSpy;
const paginatedReleases = rge(21).map((index) => ({
...convertObjectPropsToCamelCase(release, { deep: true }),
tagName: `${index}.00`,
}));
2018-12-20 04:31:32 -05:00
const defaultInitialState = {
2018-12-20 04:31:32 -05:00
projectId: 'gitlab-ce',
projectPath: 'gitlab-org/gitlab-ce',
documentationPath: 'help/releases',
2018-12-20 04:31:32 -05:00
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,
});
};
2018-12-20 04:31:32 -05:00
afterEach(() => {
wrapper.destroy();
2018-12-20 04:31:32 -05:00
});
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',
});
});
});
2018-12-20 04:31:32 -05:00
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();
2018-12-20 04:31:32 -05:00
});
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);
2018-12-20 04:31:32 -05:00
});
});
describe('with successful request', () => {
beforeEach(() => {
jest
.spyOn(api, 'releases')
.mockResolvedValue({ data: releases, headers: pageInfoHeadersWithoutPagination });
createComponent();
2018-12-20 04:31:32 -05:00
});
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);
2018-12-20 04:31:32 -05:00
});
});
describe('with empty request', () => {
beforeEach(() => {
jest.spyOn(api, 'releases').mockResolvedValue({ data: [], headers: {} });
createComponent();
2018-12-20 04:31:32 -05:00
});
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);
});
2018-12-20 04:31:32 -05:00
});
});
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',
});
});
});
2018-12-20 04:31:32 -05:00
});