diff options
Diffstat (limited to 'spec/frontend/diffs/utils/merge_request_spec.js')
-rw-r--r-- | spec/frontend/diffs/utils/merge_request_spec.js | 94 |
1 files changed, 80 insertions, 14 deletions
diff --git a/spec/frontend/diffs/utils/merge_request_spec.js b/spec/frontend/diffs/utils/merge_request_spec.js index c070e8c004d..11c0efb9a9c 100644 --- a/spec/frontend/diffs/utils/merge_request_spec.js +++ b/spec/frontend/diffs/utils/merge_request_spec.js @@ -1,10 +1,14 @@ -import { getDerivedMergeRequestInformation } from '~/diffs/utils/merge_request'; +import { + updateChangesTabCount, + getDerivedMergeRequestInformation, +} from '~/diffs/utils/merge_request'; +import { ZERO_CHANGES_ALT_DISPLAY } from '~/diffs/constants'; import { diffMetadata } from '../mock_data/diff_metadata'; describe('Merge Request utilities', () => { const derivedBaseInfo = { mrPath: '/gitlab-org/gitlab-test/-/merge_requests/4', - userOrGroup: 'gitlab-org', + namespace: 'gitlab-org', project: 'gitlab-test', id: '4', }; @@ -18,36 +22,98 @@ describe('Merge Request utilities', () => { }; const unparseableEndpoint = { mrPath: undefined, - userOrGroup: undefined, + namespace: undefined, project: undefined, id: undefined, ...noVersion, }; + 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'); + }); + }); + describe('getDerivedMergeRequestInformation', () => { - let endpoint = `${diffMetadata.latest_version_path}.json?searchParam=irrelevant`; + const bare = diffMetadata.latest_version_path; it.each` - argument | response - ${{ endpoint }} | ${{ ...derivedBaseInfo, ...noVersion }} - ${{}} | ${unparseableEndpoint} - ${{ endpoint: undefined }} | ${unparseableEndpoint} - ${{ endpoint: null }} | ${unparseableEndpoint} + argument | response + ${{ endpoint: `${bare}.json?searchParam=irrelevant` }} | ${{ ...derivedBaseInfo, ...noVersion }} + ${{}} | ${unparseableEndpoint} + ${{ endpoint: undefined }} | ${unparseableEndpoint} + ${{ endpoint: null }} | ${unparseableEndpoint} `('generates the correct derived results based on $argument', ({ argument, response }) => { expect(getDerivedMergeRequestInformation(argument)).toStrictEqual(response); }); - describe('version information', () => { - const bare = diffMetadata.latest_version_path; - endpoint = diffMetadata.merge_request_diffs[0].compare_path; + describe('sub-group namespace', () => { + it('extracts the entire namespace plus the project name', () => { + const { namespace, project } = getDerivedMergeRequestInformation({ + endpoint: `/some/deep/path/of/groups${bare}`, + }); + + expect(namespace).toBe('some/deep/path/of/groups/gitlab-org'); + expect(project).toBe('gitlab-test'); + }); + }); + describe('version information', () => { it('still gets the correct derived information', () => { - expect(getDerivedMergeRequestInformation({ endpoint })).toMatchObject(derivedBaseInfo); + expect( + getDerivedMergeRequestInformation({ + endpoint: diffMetadata.merge_request_diffs[0].compare_path, + }), + ).toMatchObject(derivedBaseInfo); }); it.each` url | versionPart - ${endpoint} | ${derivedVersionInfo} + ${diffMetadata.merge_request_diffs[0].compare_path} | ${derivedVersionInfo} ${`${bare}?diff_id=${derivedVersionInfo.diffId}`} | ${{ ...derivedVersionInfo, startSha: undefined }} ${`${bare}?start_sha=${derivedVersionInfo.startSha}`} | ${{ ...derivedVersionInfo, diffId: undefined }} `( |