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:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-10-04 09:10:22 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-10-04 09:10:22 +0300
commitb5029334981dea8dbda5ac0b69722adcc8490035 (patch)
treee303ccd133ad750c44833537345c20c9f2b55e69 /spec/frontend/lib
parent7feabd8d8ef235e1a2376bb6879caea1b88b178d (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/lib')
-rw-r--r--spec/frontend/lib/utils/global_alerts_spec.js80
-rw-r--r--spec/frontend/lib/utils/url_utility_spec.js54
2 files changed, 134 insertions, 0 deletions
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';