diff options
Diffstat (limited to 'spec/frontend/lib/utils/url_utility_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index ecd2d7f888d..3a846bbda06 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -1,8 +1,20 @@ import setWindowLocation from 'helpers/set_window_location_helper'; import { TEST_HOST } from 'helpers/test_constants'; import * as urlUtils from '~/lib/utils/url_utility'; +import { setGlobalAlerts } from '~/lib/utils/global_alerts'; import { safeUrls, unsafeUrls } from './mock_data'; +jest.mock('~/lib/utils/global_alerts', () => ({ + getGlobalAlerts: jest.fn().mockImplementation(() => [ + { + id: 'foo', + message: 'Foo', + variant: 'success', + }, + ]), + setGlobalAlerts: jest.fn(), +})); + const shas = { valid: [ 'ad9be38573f9ee4c4daec22673478c2dd1d81cd8', @@ -327,6 +339,26 @@ describe('URL utility', () => { }); }); + describe('getLocationHash', () => { + it('gets a default empty value', () => { + setWindowLocation(TEST_HOST); + + expect(urlUtils.getLocationHash()).toBeUndefined(); + }); + + it('gets a value', () => { + setWindowLocation('#hash-value'); + + expect(urlUtils.getLocationHash()).toBe('hash-value'); + }); + + it('gets an empty value when only hash is set', () => { + setWindowLocation('#'); + + expect(urlUtils.getLocationHash()).toBeUndefined(); + }); + }); + describe('doesHashExistInUrl', () => { beforeEach(() => { setWindowLocation('#note_1'); @@ -462,6 +494,48 @@ describe('URL utility', () => { }); }); + describe('visitUrlWithAlerts', () => { + let originalLocation; + + beforeAll(() => { + originalLocation = window.location; + + Object.defineProperty(window, 'location', { + writable: true, + value: { + assign: jest.fn(), + protocol: 'http:', + host: TEST_HOST, + }, + }); + }); + + afterAll(() => { + window.location = originalLocation; + }); + + it('sets alerts and then visits url', () => { + const url = '/foo/bar'; + const alert = { + id: 'bar', + message: 'Bar', + variant: 'danger', + }; + + urlUtils.visitUrlWithAlerts(url, [alert]); + + expect(setGlobalAlerts).toHaveBeenCalledWith([ + { + id: 'foo', + message: 'Foo', + variant: 'success', + }, + alert, + ]); + expect(window.location.assign).toHaveBeenCalledWith(url); + }); + }); + describe('updateHistory', () => { const state = { key: 'prop' }; const title = 'TITLE'; |