From b5029334981dea8dbda5ac0b69722adcc8490035 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 4 Oct 2023 06:10:22 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/frontend/lib/utils/global_alerts_spec.js | 80 +++++++++++++++++++++++++++ spec/frontend/lib/utils/url_utility_spec.js | 54 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 spec/frontend/lib/utils/global_alerts_spec.js (limited to 'spec/frontend/lib') diff --git a/spec/frontend/lib/utils/global_alerts_spec.js b/spec/frontend/lib/utils/global_alerts_spec.js new file mode 100644 index 00000000000..97fe427c281 --- /dev/null +++ b/spec/frontend/lib/utils/global_alerts_spec.js @@ -0,0 +1,80 @@ +import { + getGlobalAlerts, + setGlobalAlerts, + removeGlobalAlertById, + GLOBAL_ALERTS_SESSION_STORAGE_KEY, +} from '~/lib/utils/global_alerts'; + +describe('global alerts utils', () => { + describe('getGlobalAlerts', () => { + describe('when there are alerts', () => { + beforeEach(() => { + jest + .spyOn(Storage.prototype, 'getItem') + .mockImplementation(() => '[{"id":"foo","variant":"danger","message":"Foo"}]'); + }); + + it('returns alerts from session storage', () => { + expect(getGlobalAlerts()).toEqual([{ id: 'foo', variant: 'danger', message: 'Foo' }]); + }); + }); + + describe('when there are no alerts', () => { + beforeEach(() => { + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => null); + }); + + it('returns empty array', () => { + expect(getGlobalAlerts()).toEqual([]); + }); + }); + }); +}); + +describe('setGlobalAlerts', () => { + it('sets alerts in session storage', () => { + const setItemSpy = jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {}); + + setGlobalAlerts([ + { + id: 'foo', + variant: 'danger', + message: 'Foo', + }, + { + id: 'bar', + variant: 'success', + message: 'Bar', + persistOnPages: ['dashboard:groups:index'], + dismissible: false, + }, + ]); + + expect(setItemSpy).toHaveBeenCalledWith( + GLOBAL_ALERTS_SESSION_STORAGE_KEY, + '[{"dismissible":true,"persistOnPages":[],"id":"foo","variant":"danger","message":"Foo"},{"dismissible":false,"persistOnPages":["dashboard:groups:index"],"id":"bar","variant":"success","message":"Bar"}]', + ); + }); +}); + +describe('removeGlobalAlertById', () => { + beforeEach(() => { + jest + .spyOn(Storage.prototype, 'getItem') + .mockImplementation( + () => + '[{"id":"foo","variant":"success","message":"Foo"},{"id":"bar","variant":"danger","message":"Bar"}]', + ); + }); + + it('removes alert', () => { + const setItemSpy = jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {}); + + removeGlobalAlertById('bar'); + + expect(setItemSpy).toHaveBeenCalledWith( + GLOBAL_ALERTS_SESSION_STORAGE_KEY, + '[{"id":"foo","variant":"success","message":"Foo"}]', + ); + }); +}); diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index e304ec70dc7..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', @@ -482,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'; -- cgit v1.2.3