Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/frontend/lib/utils/url_utility_spec.js')
-rw-r--r--spec/frontend/lib/utils/url_utility_spec.js107
1 files changed, 107 insertions, 0 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';