2023-06-20 00:43:36 +05:30
|
|
|
import {
|
|
|
|
updateChangesTabCount,
|
|
|
|
getDerivedMergeRequestInformation,
|
|
|
|
} from '~/diffs/utils/merge_request';
|
|
|
|
import { ZERO_CHANGES_ALT_DISPLAY } from '~/diffs/constants';
|
2021-03-08 18:12:59 +05:30
|
|
|
import { diffMetadata } from '../mock_data/diff_metadata';
|
|
|
|
|
|
|
|
describe('Merge Request utilities', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
const derivedBaseInfo = {
|
2021-03-08 18:12:59 +05:30
|
|
|
mrPath: '/gitlab-org/gitlab-test/-/merge_requests/4',
|
2023-07-09 08:55:56 +05:30
|
|
|
namespace: 'gitlab-org',
|
2021-03-08 18:12:59 +05:30
|
|
|
project: 'gitlab-test',
|
|
|
|
id: '4',
|
|
|
|
};
|
2023-03-04 22:38:38 +05:30
|
|
|
const derivedVersionInfo = {
|
|
|
|
diffId: '4',
|
|
|
|
startSha: 'eb227b3e214624708c474bdab7bde7afc17cefcc',
|
|
|
|
};
|
|
|
|
const noVersion = {
|
|
|
|
diffId: undefined,
|
|
|
|
startSha: undefined,
|
|
|
|
};
|
2021-03-08 18:12:59 +05:30
|
|
|
const unparseableEndpoint = {
|
|
|
|
mrPath: undefined,
|
2023-07-09 08:55:56 +05:30
|
|
|
namespace: undefined,
|
2021-03-08 18:12:59 +05:30
|
|
|
project: undefined,
|
|
|
|
id: undefined,
|
2023-03-04 22:38:38 +05:30
|
|
|
...noVersion,
|
2021-03-08 18:12:59 +05:30
|
|
|
};
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
describe('updateChangesTabCount', () => {
|
|
|
|
let dummyTab;
|
|
|
|
let badge;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dummyTab = document.createElement('div');
|
|
|
|
dummyTab.classList.add('js-diffs-tab');
|
|
|
|
dummyTab.insertAdjacentHTML('afterbegin', '<span class="gl-badge">ERROR</span>');
|
|
|
|
badge = dummyTab.querySelector('.gl-badge');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
dummyTab.remove();
|
|
|
|
dummyTab = null;
|
|
|
|
badge = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses the alt hyphen display when the new changes are falsey', () => {
|
|
|
|
updateChangesTabCount({ count: 0, badge });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe(ZERO_CHANGES_ALT_DISPLAY);
|
|
|
|
|
|
|
|
updateChangesTabCount({ badge });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe(ZERO_CHANGES_ALT_DISPLAY);
|
|
|
|
|
|
|
|
updateChangesTabCount({ count: false, badge });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe(ZERO_CHANGES_ALT_DISPLAY);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses the actual value for display when the value is truthy', () => {
|
|
|
|
updateChangesTabCount({ count: 42, badge });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe('42');
|
|
|
|
|
|
|
|
updateChangesTabCount({ count: '999+', badge });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe('999+');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('selects the proper element to modify by default', () => {
|
|
|
|
document.body.insertAdjacentElement('afterbegin', dummyTab);
|
|
|
|
|
|
|
|
updateChangesTabCount({ count: 42 });
|
|
|
|
|
|
|
|
expect(dummyTab.textContent).toBe('42');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
describe('getDerivedMergeRequestInformation', () => {
|
2023-07-09 08:55:56 +05:30
|
|
|
const bare = diffMetadata.latest_version_path;
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
it.each`
|
2023-07-09 08:55:56 +05:30
|
|
|
argument | response
|
|
|
|
${{ endpoint: `${bare}.json?searchParam=irrelevant` }} | ${{ ...derivedBaseInfo, ...noVersion }}
|
|
|
|
${{}} | ${unparseableEndpoint}
|
|
|
|
${{ endpoint: undefined }} | ${unparseableEndpoint}
|
|
|
|
${{ endpoint: null }} | ${unparseableEndpoint}
|
2021-03-08 18:12:59 +05:30
|
|
|
`('generates the correct derived results based on $argument', ({ argument, response }) => {
|
|
|
|
expect(getDerivedMergeRequestInformation(argument)).toStrictEqual(response);
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
describe('sub-group namespace', () => {
|
|
|
|
it('extracts the entire namespace plus the project name', () => {
|
|
|
|
const { namespace, project } = getDerivedMergeRequestInformation({
|
|
|
|
endpoint: `/some/deep/path/of/groups${bare}`,
|
|
|
|
});
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
expect(namespace).toBe('some/deep/path/of/groups/gitlab-org');
|
|
|
|
expect(project).toBe('gitlab-test');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('version information', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
it('still gets the correct derived information', () => {
|
2023-07-09 08:55:56 +05:30
|
|
|
expect(
|
|
|
|
getDerivedMergeRequestInformation({
|
|
|
|
endpoint: diffMetadata.merge_request_diffs[0].compare_path,
|
|
|
|
}),
|
|
|
|
).toMatchObject(derivedBaseInfo);
|
2023-03-04 22:38:38 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
url | versionPart
|
2023-07-09 08:55:56 +05:30
|
|
|
${diffMetadata.merge_request_diffs[0].compare_path} | ${derivedVersionInfo}
|
2023-03-04 22:38:38 +05:30
|
|
|
${`${bare}?diff_id=${derivedVersionInfo.diffId}`} | ${{ ...derivedVersionInfo, startSha: undefined }}
|
|
|
|
${`${bare}?start_sha=${derivedVersionInfo.startSha}`} | ${{ ...derivedVersionInfo, diffId: undefined }}
|
|
|
|
`(
|
|
|
|
'generates the correct derived version information based on $url',
|
|
|
|
({ url, versionPart }) => {
|
|
|
|
expect(getDerivedMergeRequestInformation({ endpoint: url })).toMatchObject(versionPart);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('extracts nothing if there is no available version-like information in the URL', () => {
|
|
|
|
expect(getDerivedMergeRequestInformation({ endpoint: bare })).toMatchObject(noVersion);
|
|
|
|
});
|
|
|
|
});
|
2021-03-08 18:12:59 +05:30
|
|
|
});
|
|
|
|
});
|