debian-mirror-gitlab/spec/frontend/releases/util_spec.js

177 lines
5 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { cloneDeep } from 'lodash';
2021-01-03 14:25:43 +05:30
import { getJSONFixture } from 'helpers/fixtures';
import {
releaseToApiJson,
apiJsonToRelease,
convertGraphQLRelease,
convertAllReleasesGraphQLResponse,
convertOneReleaseGraphQLResponse,
} from '~/releases/util';
const originalAllReleasesQueryResponse = getJSONFixture(
'graphql/releases/queries/all_releases.query.graphql.json',
);
const originalOneReleaseQueryResponse = getJSONFixture(
'graphql/releases/queries/one_release.query.graphql.json',
);
2020-10-24 23:57:45 +05:30
describe('releases/util.js', () => {
describe('releaseToApiJson', () => {
it('converts a release JavaScript object into JSON that the Release API can accept', () => {
const release = {
tagName: 'tag-name',
name: 'Release name',
description: 'Release description',
milestones: [{ id: 1, title: '13.2' }, { id: 2, title: '13.3' }],
assets: {
links: [{ url: 'https://gitlab.example.com/link', linkType: 'other' }],
},
};
const expectedJson = {
tag_name: 'tag-name',
ref: null,
name: 'Release name',
description: 'Release description',
milestones: ['13.2', '13.3'],
assets: {
links: [{ url: 'https://gitlab.example.com/link', link_type: 'other' }],
},
};
expect(releaseToApiJson(release)).toEqual(expectedJson);
});
describe('when createFrom is provided', () => {
it('adds the provided createFrom ref to the JSON as a "ref" property', () => {
const createFrom = 'main';
const release = {};
const expectedJson = {
ref: createFrom,
};
expect(releaseToApiJson(release, createFrom)).toMatchObject(expectedJson);
});
});
describe('release.name', () => {
it.each`
input | output
${null} | ${null}
${''} | ${null}
${' \t\n\r\n'} | ${null}
${' Release name '} | ${'Release name'}
`('converts a name like `$input` to `$output`', ({ input, output }) => {
const release = { name: input };
const expectedJson = {
name: output,
};
expect(releaseToApiJson(release)).toMatchObject(expectedJson);
});
});
describe('when release.milestones is falsy', () => {
it('includes a "milestone" property in the returned result as an empty array', () => {
const release = {};
const expectedJson = {
milestones: [],
};
expect(releaseToApiJson(release)).toMatchObject(expectedJson);
});
});
});
describe('apiJsonToRelease', () => {
it('converts JSON received from the Release API into an object usable by the Vue application', () => {
const json = {
tag_name: 'tag-name',
assets: {
links: [
{
link_type: 'other',
},
],
},
};
const expectedRelease = {
tagName: 'tag-name',
assets: {
links: [
{
linkType: 'other',
},
],
},
milestones: [],
};
expect(apiJsonToRelease(json)).toEqual(expectedRelease);
});
});
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
describe('convertGraphQLRelease', () => {
let releaseFromResponse;
let convertedRelease;
2020-11-24 15:15:51 +05:30
beforeEach(() => {
2021-01-03 14:25:43 +05:30
releaseFromResponse = cloneDeep(originalOneReleaseQueryResponse).data.project.release;
convertedRelease = convertGraphQLRelease(releaseFromResponse);
2020-11-24 15:15:51 +05:30
});
describe('assets', () => {
it("handles asset links that don't have a linkType", () => {
2021-01-03 14:25:43 +05:30
expect(convertedRelease.assets.links[0].linkType).not.toBeUndefined();
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
delete releaseFromResponse.assets.links.nodes[0].linkType;
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
convertedRelease = convertGraphQLRelease(releaseFromResponse);
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
expect(convertedRelease.assets.links[0].linkType).toBeUndefined();
2020-11-24 15:15:51 +05:30
});
});
describe('_links', () => {
it("handles releases that don't have any links", () => {
2021-01-03 14:25:43 +05:30
expect(convertedRelease._links.selfUrl).not.toBeUndefined();
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
delete releaseFromResponse.links;
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
convertedRelease = convertGraphQLRelease(releaseFromResponse);
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
expect(convertedRelease._links.selfUrl).toBeUndefined();
2020-11-24 15:15:51 +05:30
});
});
describe('commit', () => {
it("handles releases that don't have any commit info", () => {
2021-01-03 14:25:43 +05:30
expect(convertedRelease.commit).not.toBeUndefined();
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
delete releaseFromResponse.commit;
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
convertedRelease = convertGraphQLRelease(releaseFromResponse);
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
expect(convertedRelease.commit).toBeUndefined();
2020-11-24 15:15:51 +05:30
});
});
});
2021-01-03 14:25:43 +05:30
describe('convertAllReleasesGraphQLResponse', () => {
it('matches snapshot', () => {
expect(convertAllReleasesGraphQLResponse(originalAllReleasesQueryResponse)).toMatchSnapshot();
});
});
describe('convertOneReleaseGraphQLResponse', () => {
it('matches snapshot', () => {
expect(convertOneReleaseGraphQLResponse(originalOneReleaseQueryResponse)).toMatchSnapshot();
});
});
2020-10-24 23:57:45 +05:30
});