2021-08-09 12:10:09 +00:00
|
|
|
import { GlEmptyState, GlModal } from '@gitlab/ui';
|
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2021-07-21 15:08:52 +00:00
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
2021-08-09 12:10:09 +00:00
|
|
|
import { useMockLocationHelper } from 'helpers/mock_window_location_helper';
|
|
|
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
|
2021-07-21 15:08:52 +00:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import createFlash from '~/flash';
|
2021-07-07 12:08:23 +00:00
|
|
|
|
2021-08-02 18:08:48 +00:00
|
|
|
import AdditionalMetadata from '~/packages_and_registries/package_registry/components/details/additional_metadata.vue';
|
2021-07-07 12:08:23 +00:00
|
|
|
import PackagesApp from '~/packages_and_registries/package_registry/components/details/app.vue';
|
2021-08-06 12:10:15 +00:00
|
|
|
import InstallationCommands from '~/packages_and_registries/package_registry/components/details/installation_commands.vue';
|
2021-07-28 18:10:23 +00:00
|
|
|
import PackageHistory from '~/packages_and_registries/package_registry/components/details/package_history.vue';
|
2021-07-21 15:08:52 +00:00
|
|
|
import PackageTitle from '~/packages_and_registries/package_registry/components/details/package_title.vue';
|
2021-08-09 12:10:09 +00:00
|
|
|
import {
|
|
|
|
FETCH_PACKAGE_DETAILS_ERROR_MESSAGE,
|
|
|
|
DELETE_PACKAGE_ERROR_MESSAGE,
|
|
|
|
} from '~/packages_and_registries/package_registry/constants';
|
|
|
|
import destroyPackageMutation from '~/packages_and_registries/package_registry/graphql/mutations/destroy_package.mutation.graphql';
|
2021-07-21 15:08:52 +00:00
|
|
|
import getPackageDetails from '~/packages_and_registries/package_registry/graphql/queries/get_package_details.query.graphql';
|
2021-08-09 12:10:09 +00:00
|
|
|
import {
|
|
|
|
packageDetailsQuery,
|
|
|
|
packageData,
|
|
|
|
emptyPackageDetailsQuery,
|
|
|
|
packageDestroyMutation,
|
|
|
|
packageDestroyMutationError,
|
|
|
|
} from '../../mock_data';
|
2021-07-21 15:08:52 +00:00
|
|
|
|
|
|
|
jest.mock('~/flash');
|
2021-08-09 12:10:09 +00:00
|
|
|
useMockLocationHelper();
|
2021-07-21 15:08:52 +00:00
|
|
|
|
|
|
|
const localVue = createLocalVue();
|
2021-07-07 12:08:23 +00:00
|
|
|
|
|
|
|
describe('PackagesApp', () => {
|
|
|
|
let wrapper;
|
2021-07-21 15:08:52 +00:00
|
|
|
let apolloProvider;
|
|
|
|
|
2021-07-28 18:10:23 +00:00
|
|
|
const provide = {
|
|
|
|
packageId: '111',
|
|
|
|
titleComponent: 'PackageTitle',
|
|
|
|
projectName: 'projectName',
|
|
|
|
canDelete: 'canDelete',
|
|
|
|
svgPath: 'svgPath',
|
|
|
|
npmPath: 'npmPath',
|
|
|
|
npmHelpPath: 'npmHelpPath',
|
|
|
|
projectListUrl: 'projectListUrl',
|
|
|
|
groupListUrl: 'groupListUrl',
|
|
|
|
};
|
|
|
|
|
2021-08-09 12:10:09 +00:00
|
|
|
function createComponent({
|
|
|
|
resolver = jest.fn().mockResolvedValue(packageDetailsQuery()),
|
|
|
|
mutationResolver = jest.fn().mockResolvedValue(packageDestroyMutation()),
|
|
|
|
} = {}) {
|
2021-07-21 15:08:52 +00:00
|
|
|
localVue.use(VueApollo);
|
|
|
|
|
2021-08-09 12:10:09 +00:00
|
|
|
const requestHandlers = [
|
|
|
|
[getPackageDetails, resolver],
|
|
|
|
[destroyPackageMutation, mutationResolver],
|
|
|
|
];
|
2021-07-21 15:08:52 +00:00
|
|
|
apolloProvider = createMockApollo(requestHandlers);
|
2021-07-07 12:08:23 +00:00
|
|
|
|
2021-08-09 12:10:09 +00:00
|
|
|
wrapper = shallowMountExtended(PackagesApp, {
|
2021-07-21 15:08:52 +00:00
|
|
|
localVue,
|
|
|
|
apolloProvider,
|
2021-07-28 18:10:23 +00:00
|
|
|
provide,
|
2021-08-09 12:10:09 +00:00
|
|
|
stubs: { PackageTitle },
|
2021-07-07 12:08:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-21 15:08:52 +00:00
|
|
|
const findEmptyState = () => wrapper.findComponent(GlEmptyState);
|
|
|
|
const findPackageTitle = () => wrapper.findComponent(PackageTitle);
|
2021-07-28 18:10:23 +00:00
|
|
|
const findPackageHistory = () => wrapper.findComponent(PackageHistory);
|
2021-08-02 18:08:48 +00:00
|
|
|
const findAdditionalMetadata = () => wrapper.findComponent(AdditionalMetadata);
|
2021-08-06 12:10:15 +00:00
|
|
|
const findInstallationCommands = () => wrapper.findComponent(InstallationCommands);
|
2021-08-09 12:10:09 +00:00
|
|
|
const findDeleteModal = () => wrapper.findComponent(GlModal);
|
|
|
|
const findDeleteButton = () => wrapper.findByTestId('delete-package');
|
2021-07-07 12:08:23 +00:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
2021-07-28 18:10:23 +00:00
|
|
|
it('renders an empty state component', async () => {
|
|
|
|
createComponent({ resolver: jest.fn().mockResolvedValue(emptyPackageDetailsQuery) });
|
|
|
|
|
|
|
|
await waitForPromises();
|
2021-07-07 12:08:23 +00:00
|
|
|
|
2021-07-21 15:08:52 +00:00
|
|
|
expect(findEmptyState().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders the app and displays the package title', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findPackageTitle().exists()).toBe(true);
|
|
|
|
expect(findPackageTitle().props()).toMatchObject({
|
|
|
|
packageEntity: expect.objectContaining(packageData()),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits an error message if the load fails', async () => {
|
|
|
|
createComponent({ resolver: jest.fn().mockRejectedValue() });
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(createFlash).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: FETCH_PACKAGE_DETAILS_ERROR_MESSAGE,
|
|
|
|
}),
|
|
|
|
);
|
2021-07-07 12:08:23 +00:00
|
|
|
});
|
2021-07-28 18:10:23 +00:00
|
|
|
|
|
|
|
it('renders history and has the right props', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findPackageHistory().exists()).toBe(true);
|
|
|
|
expect(findPackageHistory().props()).toMatchObject({
|
|
|
|
packageEntity: expect.objectContaining(packageData()),
|
|
|
|
projectName: provide.projectName,
|
|
|
|
});
|
|
|
|
});
|
2021-08-02 18:08:48 +00:00
|
|
|
|
2021-08-06 12:10:15 +00:00
|
|
|
it('renders additional metadata and has the right props', async () => {
|
2021-08-02 18:08:48 +00:00
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findAdditionalMetadata().exists()).toBe(true);
|
|
|
|
expect(findAdditionalMetadata().props()).toMatchObject({
|
|
|
|
packageEntity: expect.objectContaining(packageData()),
|
|
|
|
});
|
|
|
|
});
|
2021-08-06 12:10:15 +00:00
|
|
|
|
|
|
|
it('renders installation commands and has the right props', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(findInstallationCommands().exists()).toBe(true);
|
|
|
|
expect(findInstallationCommands().props()).toMatchObject({
|
|
|
|
packageEntity: expect.objectContaining(packageData()),
|
|
|
|
});
|
|
|
|
});
|
2021-08-09 12:10:09 +00:00
|
|
|
|
|
|
|
describe('delete package', () => {
|
|
|
|
const originalReferrer = document.referrer;
|
|
|
|
const setReferrer = (value = provide.projectName) => {
|
|
|
|
Object.defineProperty(document, 'referrer', {
|
|
|
|
value,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const performDeletePackage = async () => {
|
|
|
|
await findDeleteButton().trigger('click');
|
|
|
|
|
|
|
|
findDeleteModal().vm.$emit('primary');
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
};
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
Object.defineProperty(document, 'referrer', {
|
|
|
|
value: originalReferrer,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows the delete confirmation modal when delete is clicked', async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
await findDeleteButton().trigger('click');
|
|
|
|
|
|
|
|
expect(findDeleteModal().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('successful request', () => {
|
|
|
|
it('when referrer contains project name calls window.replace with project url', async () => {
|
|
|
|
setReferrer();
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
await performDeletePackage();
|
|
|
|
|
|
|
|
expect(window.location.replace).toHaveBeenCalledWith(
|
|
|
|
'projectListUrl?showSuccessDeleteAlert=true',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when referrer does not contain project name calls window.replace with group url', async () => {
|
|
|
|
setReferrer('baz');
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
await performDeletePackage();
|
|
|
|
|
|
|
|
expect(window.location.replace).toHaveBeenCalledWith(
|
|
|
|
'groupListUrl?showSuccessDeleteAlert=true',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('request failure', () => {
|
|
|
|
it('on global failure it displays an alert', async () => {
|
|
|
|
createComponent({ mutationResolver: jest.fn().mockRejectedValue() });
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
await performDeletePackage();
|
|
|
|
|
|
|
|
expect(createFlash).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: DELETE_PACKAGE_ERROR_MESSAGE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on payload with error it displays an alert', async () => {
|
|
|
|
createComponent({
|
|
|
|
mutationResolver: jest.fn().mockResolvedValue(packageDestroyMutationError()),
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
await performDeletePackage();
|
|
|
|
|
|
|
|
expect(createFlash).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: DELETE_PACKAGE_ERROR_MESSAGE,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-07-07 12:08:23 +00:00
|
|
|
});
|