diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-28 12:09:06 +0300 |
commit | 7e8278c0f46cf6058efad5afd0aef177977bd663 (patch) | |
tree | 7ac46710921145bb782bcb208ea896e1548b168b /spec/frontend | |
parent | bbf6581214128ae12a6ff32f66a0d03ee57a2e91 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 107 | ||||
-rw-r--r-- | spec/frontend/repository/utils/title_spec.js | 25 |
2 files changed, 131 insertions, 1 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index 048736d75f6..989de1a8337 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -1,5 +1,20 @@ import * as urlUtils from '~/lib/utils/url_utility'; +const shas = { + valid: [ + 'ad9be38573f9ee4c4daec22673478c2dd1d81cd8', + '76e07a692f65a2f4fd72f107a3e83908bea9b7eb', + '9dd8f215b1e8605b1d59eaf9df1178081cda0aaf', + 'f2e0be58c4091b033203bae1cc0302febd54117d', + ], + invalid: [ + 'zd9be38573f9ee4c4daec22673478c2dd1d81cd8', + ':6e07a692f65a2f4fd72f107a3e83908bea9b7eb', + '-dd8f215b1e8605b1d59eaf9df1178081cda0aaf', + ' 2e0be58c4091b033203bae1cc0302febd54117d', + ], +}; + const setWindowLocation = value => { Object.defineProperty(window, 'location', { writable: true, @@ -154,6 +169,44 @@ describe('URL utility', () => { }); }); + describe('urlContainsSha', () => { + it('returns true when there is a valid 40-character SHA1 hash in the URL', () => { + shas.valid.forEach(sha => { + expect( + urlUtils.urlContainsSha({ url: `http://urlstuff/${sha}/moreurlstuff` }), + ).toBeTruthy(); + }); + }); + + it('returns false when there is not a valid 40-character SHA1 hash in the URL', () => { + shas.invalid.forEach(str => { + expect(urlUtils.urlContainsSha({ url: `http://urlstuff/${str}/moreurlstuff` })).toBeFalsy(); + }); + }); + }); + + describe('getShaFromUrl', () => { + let validUrls = []; + let invalidUrls = []; + + beforeAll(() => { + validUrls = shas.valid.map(sha => `http://urlstuff/${sha}/moreurlstuff`); + invalidUrls = shas.invalid.map(str => `http://urlstuff/${str}/moreurlstuff`); + }); + + it('returns the valid 40-character SHA1 hash from the URL', () => { + validUrls.forEach((url, idx) => { + expect(urlUtils.getShaFromUrl({ url })).toBe(shas.valid[idx]); + }); + }); + + it('returns null from a URL with no valid 40-character SHA1 hash', () => { + invalidUrls.forEach(url => { + expect(urlUtils.getShaFromUrl({ url })).toBeNull(); + }); + }); + }); + describe('setUrlFragment', () => { it('should set fragment when url has no fragment', () => { const url = urlUtils.setUrlFragment('/home/feature', 'usage'); @@ -174,6 +227,44 @@ describe('URL utility', () => { }); }); + describe('updateHistory', () => { + const state = { key: 'prop' }; + const title = 'TITLE'; + const url = 'URL'; + const win = { + history: { + pushState: jest.fn(), + replaceState: jest.fn(), + }, + }; + + beforeEach(() => { + win.history.pushState.mockReset(); + win.history.replaceState.mockReset(); + }); + + it('should call replaceState if the replace option is true', () => { + urlUtils.updateHistory({ state, title, url, replace: true, win }); + + expect(win.history.replaceState).toHaveBeenCalledWith(state, title, url); + expect(win.history.pushState).not.toHaveBeenCalled(); + }); + + it('should call pushState if the replace option is missing', () => { + urlUtils.updateHistory({ state, title, url, win }); + + expect(win.history.replaceState).not.toHaveBeenCalled(); + expect(win.history.pushState).toHaveBeenCalledWith(state, title, url); + }); + + it('should call pushState if the replace option is false', () => { + urlUtils.updateHistory({ state, title, url, replace: false, win }); + + expect(win.history.replaceState).not.toHaveBeenCalled(); + expect(win.history.pushState).toHaveBeenCalledWith(state, title, url); + }); + }); + describe('getBaseURL', () => { beforeEach(() => { setWindowLocation({ @@ -331,6 +422,22 @@ describe('URL utility', () => { }); }); + describe('urlIsDifferent', () => { + beforeEach(() => { + setWindowLocation('current'); + }); + + it('should compare against the window location if no compare value is provided', () => { + expect(urlUtils.urlIsDifferent('different')).toBeTruthy(); + expect(urlUtils.urlIsDifferent('current')).toBeFalsy(); + }); + + it('should use the provided compare value', () => { + expect(urlUtils.urlIsDifferent('different', 'current')).toBeTruthy(); + expect(urlUtils.urlIsDifferent('current', 'current')).toBeFalsy(); + }); + }); + describe('setUrlParams', () => { it('adds new params as query string', () => { const url = 'https://gitlab.com/test'; diff --git a/spec/frontend/repository/utils/title_spec.js b/spec/frontend/repository/utils/title_spec.js index 63035933424..a1213c13be8 100644 --- a/spec/frontend/repository/utils/title_spec.js +++ b/spec/frontend/repository/utils/title_spec.js @@ -1,4 +1,4 @@ -import { setTitle } from '~/repository/utils/title'; +import { setTitle, updateRefPortionOfTitle } from '~/repository/utils/title'; describe('setTitle', () => { it.each` @@ -13,3 +13,26 @@ describe('setTitle', () => { expect(document.title).toEqual(`${title} · master · GitLab Org / GitLab · GitLab`); }); }); + +describe('updateRefPortionOfTitle', () => { + const sha = 'abc'; + const testCases = [ + [ + 'updates the title with the SHA', + { title: 'part 1 · part 2 · part 3' }, + 'part 1 · abc · part 3', + ], + ["makes no change if there's no title", { foo: null }, undefined], + [ + "makes no change if the title doesn't split predictably", + { title: 'part 1 - part 2 - part 3' }, + 'part 1 - part 2 - part 3', + ], + ]; + + it.each(testCases)('%s', (desc, doc, title) => { + updateRefPortionOfTitle(sha, doc); + + expect(doc.title).toEqual(title); + }); +}); |