debian-mirror-gitlab/spec/frontend/packages/details/components/package_title_spec.js

190 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import Vuex from 'vuex';
import { shallowMount, createLocalVue } from '@vue/test-utils';
2021-01-29 00:20:46 +05:30
import { GlBreakpointInstance } from '@gitlab/ui/dist/utils';
2020-10-24 23:57:45 +05:30
import PackageTitle from '~/packages/details/components/package_title.vue';
import PackageTags from '~/packages/shared/components/package_tags.vue';
2020-11-24 15:15:51 +05:30
import TitleArea from '~/vue_shared/components/registry/title_area.vue';
2020-10-24 23:57:45 +05:30
import {
conanPackage,
mavenFiles,
mavenPackage,
mockTags,
npmFiles,
npmPackage,
nugetPackage,
} from '../../mock_data';
const localVue = createLocalVue();
localVue.use(Vuex);
describe('PackageTitle', () => {
let wrapper;
let store;
function createComponent({
packageEntity = mavenPackage,
packageFiles = mavenFiles,
icon = null,
} = {}) {
store = new Vuex.Store({
state: {
packageEntity,
packageFiles,
},
getters: {
packageTypeDisplay: ({ packageEntity: { package_type: type } }) => type,
packagePipeline: ({ packageEntity: { pipeline = null } }) => pipeline,
packageIcon: () => icon,
},
});
wrapper = shallowMount(PackageTitle, {
localVue,
store,
2020-11-24 15:15:51 +05:30
stubs: {
TitleArea,
},
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
return wrapper.vm.$nextTick();
2020-10-24 23:57:45 +05:30
}
2020-11-24 15:15:51 +05:30
const findTitleArea = () => wrapper.find(TitleArea);
2020-10-24 23:57:45 +05:30
const packageType = () => wrapper.find('[data-testid="package-type"]');
const packageSize = () => wrapper.find('[data-testid="package-size"]');
const pipelineProject = () => wrapper.find('[data-testid="pipeline-project"]');
const packageRef = () => wrapper.find('[data-testid="package-ref"]');
const packageTags = () => wrapper.find(PackageTags);
2021-01-29 00:20:46 +05:30
const packageBadges = () => wrapper.findAll('[data-testid="tag-badge"]');
2020-10-24 23:57:45 +05:30
afterEach(() => {
wrapper.destroy();
});
describe('renders', () => {
2020-11-24 15:15:51 +05:30
it('without tags', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
expect(wrapper.element).toMatchSnapshot();
});
2020-11-24 15:15:51 +05:30
it('with tags', async () => {
await createComponent({ packageEntity: { ...mavenPackage, tags: mockTags } });
2020-10-24 23:57:45 +05:30
expect(wrapper.element).toMatchSnapshot();
});
2021-01-29 00:20:46 +05:30
it('with tags on mobile', async () => {
jest.spyOn(GlBreakpointInstance, 'isDesktop').mockReturnValue(false);
await createComponent({ packageEntity: { ...mavenPackage, tags: mockTags } });
await wrapper.vm.$nextTick();
expect(packageBadges()).toHaveLength(mockTags.length);
});
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
describe('package title', () => {
it('is correctly bound', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(findTitleArea().props('title')).toBe('Test package');
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
});
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
describe('package icon', () => {
const fakeSrc = 'a-fake-src';
it('binds an icon when provided one from vuex', async () => {
await createComponent({ icon: fakeSrc });
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(findTitleArea().props('avatar')).toBe(fakeSrc);
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
it('do not binds an icon when not provided one', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(findTitleArea().props('avatar')).toBe(null);
2020-10-24 23:57:45 +05:30
});
});
describe.each`
2020-11-24 15:15:51 +05:30
packageEntity | text
2020-10-24 23:57:45 +05:30
${conanPackage} | ${'conan'}
${mavenPackage} | ${'maven'}
${npmPackage} | ${'npm'}
${nugetPackage} | ${'nuget'}
2020-11-24 15:15:51 +05:30
`(`package type`, ({ packageEntity, text }) => {
2020-10-24 23:57:45 +05:30
beforeEach(() => createComponent({ packageEntity }));
2020-11-24 15:15:51 +05:30
it(`${packageEntity.package_type} should render from Vuex getters ${text}`, () => {
expect(packageType().props()).toEqual(expect.objectContaining({ text, icon: 'package' }));
2020-10-24 23:57:45 +05:30
});
});
describe('calculates the package size', () => {
2020-11-24 15:15:51 +05:30
it('correctly calculates when there is only 1 file', async () => {
await createComponent({ packageEntity: npmPackage, packageFiles: npmFiles });
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(packageSize().props()).toMatchObject({ text: '200 bytes', icon: 'disk' });
2020-10-24 23:57:45 +05:30
});
2020-11-24 15:15:51 +05:30
it('correctly calulates when there are multiple files', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(packageSize().props('text')).toBe('300 bytes');
2020-10-24 23:57:45 +05:30
});
});
describe('package tags', () => {
2020-11-24 15:15:51 +05:30
it('displays the package-tags component when the package has tags', async () => {
await createComponent({
2020-10-24 23:57:45 +05:30
packageEntity: {
...npmPackage,
tags: mockTags,
},
});
expect(packageTags().exists()).toBe(true);
});
2020-11-24 15:15:51 +05:30
it('does not display the package-tags component when there are no tags', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
expect(packageTags().exists()).toBe(false);
});
});
describe('package ref', () => {
2020-11-24 15:15:51 +05:30
it('does not display the ref if missing', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
expect(packageRef().exists()).toBe(false);
});
2020-11-24 15:15:51 +05:30
it('correctly shows the package ref if there is one', async () => {
await createComponent({ packageEntity: npmPackage });
expect(packageRef().props()).toMatchObject({
text: npmPackage.pipeline.ref,
icon: 'branch',
});
2020-10-24 23:57:45 +05:30
});
});
describe('pipeline project', () => {
2020-11-24 15:15:51 +05:30
it('does not display the project if missing', async () => {
await createComponent();
2020-10-24 23:57:45 +05:30
expect(pipelineProject().exists()).toBe(false);
});
2020-11-24 15:15:51 +05:30
it('correctly shows the pipeline project if there is one', async () => {
await createComponent({ packageEntity: npmPackage });
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
expect(pipelineProject().props()).toMatchObject({
text: npmPackage.pipeline.project.name,
icon: 'review-list',
link: npmPackage.pipeline.project.web_url,
});
2020-10-24 23:57:45 +05:30
});
});
});