2021-09-14 17:11:58 -04:00
|
|
|
import { GlAlert, GlDropdown, GlSprintf, GlLoadingIcon } from '@gitlab/ui';
|
2021-04-22 11:09:56 -04:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2021-04-27 17:10:09 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-04-22 11:09:56 -04:00
|
|
|
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
|
2021-04-27 17:10:09 -04:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import PipelineMultiActions, {
|
|
|
|
i18n,
|
|
|
|
} from '~/pipelines/components/pipelines_list/pipeline_multi_actions.vue';
|
2021-04-22 11:09:56 -04:00
|
|
|
|
|
|
|
describe('Pipeline Multi Actions Dropdown', () => {
|
|
|
|
let wrapper;
|
2021-04-27 17:10:09 -04:00
|
|
|
let mockAxios;
|
2021-04-22 11:09:56 -04:00
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
const artifacts = [
|
|
|
|
{
|
|
|
|
name: 'job my-artifact',
|
|
|
|
path: '/download/path',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'job-2 my-artifact-2',
|
|
|
|
path: '/download/path-two',
|
|
|
|
},
|
|
|
|
];
|
2021-04-22 11:09:56 -04:00
|
|
|
const artifactItemTestId = 'artifact-item';
|
2021-04-27 17:10:09 -04:00
|
|
|
const artifactsEndpointPlaceholder = ':pipeline_artifacts_id';
|
|
|
|
const artifactsEndpoint = `endpoint/${artifactsEndpointPlaceholder}/artifacts.json`;
|
|
|
|
const pipelineId = 108;
|
2021-04-22 11:09:56 -04:00
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
const createComponent = ({ mockData = {} } = {}) => {
|
2021-04-22 11:09:56 -04:00
|
|
|
wrapper = extendedWrapper(
|
|
|
|
shallowMount(PipelineMultiActions, {
|
2021-04-27 17:10:09 -04:00
|
|
|
provide: {
|
|
|
|
artifactsEndpoint,
|
|
|
|
artifactsEndpointPlaceholder,
|
|
|
|
},
|
2021-04-22 11:09:56 -04:00
|
|
|
propsData: {
|
2021-04-27 17:10:09 -04:00
|
|
|
pipelineId,
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
...mockData,
|
|
|
|
};
|
2021-04-22 11:09:56 -04:00
|
|
|
},
|
|
|
|
stubs: {
|
|
|
|
GlSprintf,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
const findAlert = () => wrapper.findComponent(GlAlert);
|
2021-04-22 11:09:56 -04:00
|
|
|
const findDropdown = () => wrapper.findComponent(GlDropdown);
|
2021-09-14 17:11:58 -04:00
|
|
|
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
|
2021-04-22 11:09:56 -04:00
|
|
|
const findAllArtifactItems = () => wrapper.findAllByTestId(artifactItemTestId);
|
|
|
|
const findFirstArtifactItem = () => wrapper.findByTestId(artifactItemTestId);
|
2021-08-16 17:11:02 -04:00
|
|
|
const findEmptyMessage = () => wrapper.findByTestId('artifacts-empty-message');
|
2021-04-22 11:09:56 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-04-27 17:10:09 -04:00
|
|
|
mockAxios = new MockAdapter(axios);
|
2021-04-22 11:09:56 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2021-04-27 17:10:09 -04:00
|
|
|
mockAxios.restore();
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render the dropdown', () => {
|
2021-04-27 17:10:09 -04:00
|
|
|
createComponent();
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
expect(findDropdown().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Artifacts', () => {
|
2021-04-27 17:10:09 -04:00
|
|
|
it('should fetch artifacts on dropdown click', async () => {
|
|
|
|
const endpoint = artifactsEndpoint.replace(artifactsEndpointPlaceholder, pipelineId);
|
|
|
|
mockAxios.onGet(endpoint).replyOnce(200, { artifacts });
|
|
|
|
createComponent();
|
|
|
|
findDropdown().vm.$emit('show');
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockAxios.history.get).toHaveLength(1);
|
|
|
|
expect(wrapper.vm.artifacts).toEqual(artifacts);
|
|
|
|
});
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
it('should render all the provided artifacts', () => {
|
2021-04-27 17:10:09 -04:00
|
|
|
createComponent({ mockData: { artifacts } });
|
|
|
|
|
|
|
|
expect(findAllArtifactItems()).toHaveLength(artifacts.length);
|
2021-08-16 17:11:02 -04:00
|
|
|
expect(findEmptyMessage().exists()).toBe(false);
|
2021-04-22 11:09:56 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render the correct artifact name and path', () => {
|
2021-04-27 17:10:09 -04:00
|
|
|
createComponent({ mockData: { artifacts } });
|
|
|
|
|
|
|
|
expect(findFirstArtifactItem().attributes('href')).toBe(artifacts[0].path);
|
|
|
|
expect(findFirstArtifactItem().text()).toBe(`Download ${artifacts[0].name} artifact`);
|
|
|
|
});
|
|
|
|
|
2021-08-16 17:11:02 -04:00
|
|
|
it('should render empty message when no artifacts are found', () => {
|
|
|
|
createComponent({ mockData: { artifacts: [] } });
|
|
|
|
|
|
|
|
expect(findEmptyMessage().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-09-14 17:11:58 -04:00
|
|
|
describe('while loading artifacts', () => {
|
|
|
|
it('should render a loading spinner and no empty message', () => {
|
|
|
|
createComponent({ mockData: { isLoading: true, artifacts: [] } });
|
|
|
|
|
|
|
|
expect(findLoadingIcon().exists()).toBe(true);
|
|
|
|
expect(findEmptyMessage().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
describe('with a failing request', () => {
|
|
|
|
it('should render an error message', async () => {
|
|
|
|
const endpoint = artifactsEndpoint.replace(artifactsEndpointPlaceholder, pipelineId);
|
|
|
|
mockAxios.onGet(endpoint).replyOnce(500);
|
|
|
|
createComponent();
|
|
|
|
findDropdown().vm.$emit('show');
|
|
|
|
await waitForPromises();
|
2021-04-22 11:09:56 -04:00
|
|
|
|
2021-04-27 17:10:09 -04:00
|
|
|
const error = findAlert();
|
|
|
|
expect(error.exists()).toBe(true);
|
|
|
|
expect(error.text()).toBe(i18n.artifactsFetchErrorMessage);
|
|
|
|
});
|
2021-04-22 11:09:56 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|