Skip to content

Commit

Permalink
Add unit tests for extractNextUrlFromWindowLocation
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Aug 24, 2023
1 parent 19368da commit 1a994d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion public/apps/login/login-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function redirect(serverBasePath: string) {
window.location.href = nextUrl + window.location.hash;
}

function extractNextUrlFromWindowLocation(): string {
export function extractNextUrlFromWindowLocation(): string {
const urlParams = new URLSearchParams(window.location.search);
let nextUrl = urlParams.get('nextUrl');
if (!nextUrl || nextUrl.toLowerCase().includes('//')) {
Expand Down
23 changes: 22 additions & 1 deletion public/apps/login/test/login-page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { shallow } from 'enzyme';
import React from 'react';
import { ClientConfigType } from '../../../types';
import { LoginPage } from '../login-page';
import { LoginPage, extractNextUrlFromWindowLocation } from '../login-page';
import { validateCurrentPassword } from '../../../utils/login-utils';
import { API_AUTH_LOGOUT } from '../../../../common';

Expand Down Expand Up @@ -62,6 +62,27 @@ const configUiDefault = {
},
};

describe('test extractNextUrlFromWindowLocation', () => {
test('extract next url from window with nextUrl', () => {
// Trick to mock window.location
const originalLocation = window.location;
delete window.location;
window.location = new URL(
"http://localhost:5601/app/login?nextUrl=%2Fapp%2Fdashboards#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)"
) as any;
expect(extractNextUrlFromWindowLocation()).toEqual(
"?nextUrl=%2Fapp%2Fdashboards#/view/7adfa750-4c81-11e8-b3d7-01146121b73d?_g=(filters:!(),refreshInterval:(pause:!f,value:900000),time:(from:now-24h,to:now))&_a=(description:'Analyze%20mock%20flight%20data%20for%20OpenSearch-Air,%20Logstash%20Airways,%20OpenSearch%20Dashboards%20Airlines%20and%20BeatsWest',filters:!(),fullScreenMode:!f,options:(hidePanelTitles:!f,useMargins:!t),query:(language:kuery,query:''),timeRestore:!t,title:'%5BFlights%5D%20Global%20Flight%20Dashboard',viewMode:view)"
);
});

test('extract next url from window without nextUrl', () => {
const originalLocation = window.location;
delete window.location;
window.location = new URL('http://localhost:5601/app/home');
expect(extractNextUrlFromWindowLocation()).toEqual('?nextUrl=%2F');
});
});

describe('Login page', () => {
const mockHttpStart = {
basePath: {
Expand Down

0 comments on commit 1a994d4

Please sign in to comment.