debian-mirror-gitlab/spec/frontend/contributors/component/contributors_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import MockAdapter from 'axios-mock-adapter';
2022-04-04 11:22:00 +05:30
import Vue, { nextTick } from 'vue';
2023-04-23 21:23:45 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2021-03-11 19:13:27 +05:30
import ContributorsCharts from '~/contributors/components/contributors.vue';
2019-12-26 22:10:19 +05:30
import { createStore } from '~/contributors/stores';
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import { visitUrl } from '~/lib/utils/url_utility';
import RefSelector from '~/ref/components/ref_selector.vue';
import { REF_TYPE_BRANCHES, REF_TYPE_TAGS } from '~/ref/constants';
jest.mock('~/lib/utils/url_utility', () => ({
visitUrl: jest.fn(),
}));
2019-12-26 22:10:19 +05:30
let wrapper;
let mock;
let store;
const Component = Vue.extend(ContributorsCharts);
2023-04-23 21:23:45 +05:30
const endpoint = 'contributors/-/graphs';
2021-06-08 01:23:25 +05:30
const branch = 'main';
2019-12-26 22:10:19 +05:30
const chartData = [
{ author_name: 'John', author_email: 'jawnnypoo@gmail.com', date: '2019-05-05' },
{ author_name: 'John', author_email: 'jawnnypoo@gmail.com', date: '2019-03-03' },
];
2023-04-23 21:23:45 +05:30
const projectId = '23';
const commitsPath = 'some/path';
2019-12-26 22:10:19 +05:30
function factory() {
mock = new MockAdapter(axios);
jest.spyOn(axios, 'get');
2023-04-23 21:23:45 +05:30
mock.onGet().reply(HTTP_STATUS_OK, chartData);
2019-12-26 22:10:19 +05:30
store = createStore();
2023-04-23 21:23:45 +05:30
wrapper = mountExtended(Component, {
2019-12-26 22:10:19 +05:30
propsData: {
endpoint,
branch,
2023-04-23 21:23:45 +05:30
projectId,
commitsPath,
2019-12-26 22:10:19 +05:30
},
stubs: {
GlLoadingIcon: true,
GlAreaChart: true,
2023-04-23 21:23:45 +05:30
RefSelector: true,
2019-12-26 22:10:19 +05:30
},
store,
});
}
2023-04-23 21:23:45 +05:30
const findLoadingIcon = () => wrapper.findByTestId('loading-app-icon');
const findRefSelector = () => wrapper.findComponent(RefSelector);
const findHistoryButton = () => wrapper.findByTestId('history-button');
const findContributorsCharts = () => wrapper.findByTestId('contributors-charts');
2019-12-26 22:10:19 +05:30
describe('Contributors charts', () => {
beforeEach(() => {
factory();
});
afterEach(() => {
mock.restore();
wrapper.destroy();
});
it('should fetch chart data when mounted', () => {
expect(axios.get).toHaveBeenCalledWith(endpoint);
});
2022-04-04 11:22:00 +05:30
it('should display loader whiled loading data', async () => {
2019-12-26 22:10:19 +05:30
wrapper.vm.$store.state.loading = true;
2022-04-04 11:22:00 +05:30
await nextTick();
2023-04-23 21:23:45 +05:30
expect(findLoadingIcon().exists()).toBe(true);
2019-12-26 22:10:19 +05:30
});
2023-04-23 21:23:45 +05:30
it('should render charts and a RefSelector when loading completed and there is chart data', async () => {
2019-12-26 22:10:19 +05:30
wrapper.vm.$store.state.loading = false;
wrapper.vm.$store.state.chartData = chartData;
2022-04-04 11:22:00 +05:30
await nextTick();
2023-04-23 21:23:45 +05:30
expect(findLoadingIcon().exists()).toBe(false);
expect(findRefSelector().exists()).toBe(true);
expect(findRefSelector().props()).toMatchObject({
enabledRefTypes: [REF_TYPE_BRANCHES, REF_TYPE_TAGS],
value: branch,
projectId,
translations: { dropdownHeader: 'Switch branch/tag' },
useSymbolicRefNames: false,
state: true,
name: '',
});
expect(findContributorsCharts().exists()).toBe(true);
2022-04-04 11:22:00 +05:30
expect(wrapper.element).toMatchSnapshot();
2019-12-26 22:10:19 +05:30
});
2023-04-23 21:23:45 +05:30
it('should have a history button with a set href attribute', async () => {
wrapper.vm.$store.state.loading = false;
wrapper.vm.$store.state.chartData = chartData;
await nextTick();
const historyButton = findHistoryButton();
expect(historyButton.exists()).toBe(true);
expect(historyButton.attributes('href')).toBe(commitsPath);
});
it('visits a URL when clicking on a branch/tag', async () => {
wrapper.vm.$store.state.loading = false;
wrapper.vm.$store.state.chartData = chartData;
await nextTick();
findRefSelector().vm.$emit('input', branch);
expect(visitUrl).toHaveBeenCalledWith(`${endpoint}/${branch}`);
});
2019-12-26 22:10:19 +05:30
});