Skip to content

Commit

Permalink
fixup! chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
akurinnoy committed Aug 31, 2023
1 parent 5ffaf17 commit 58c5853
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* Red Hat, Inc. - initial API and implementation
*/

const mockMain = jest.fn();
const mockRedirectToDashboard = jest.fn();
jest.mock('../main.ts', () => ({
main: mockMain,
redirectToDashboard: mockRedirectToDashboard,
}));

it('should call main()', () => {
require('../index.ts');
expect(mockMain).toHaveBeenCalled();
expect(mockRedirectToDashboard).toHaveBeenCalled();
});
21 changes: 6 additions & 15 deletions packages/dashboard-frontend/src/preload/__tests__/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import { REMOTES_ATTR } from '../../services/helpers/factoryFlow/buildFactoryParams';
import SessionStorageService, { SessionStorageKey } from '../../services/session-storage';
import { main, buildFactoryLoaderPath, storePathIfNeeded } from '../main';
import { redirectToDashboard, buildFactoryLoaderPath, storePathIfNeeded } from '../main';

describe('test buildFactoryLoaderPath()', () => {
describe('SSHLocation', () => {
Expand Down Expand Up @@ -158,28 +158,19 @@ describe('test storePathnameIfNeeded()', () => {
});
});

describe('test main()', () => {
describe('test redirectToDashboard()', () => {
const origin = 'https://che-host';
let spyWindowLocation: jest.SpyInstance;

afterEach(() => {
spyWindowLocation.mockRestore();
});

describe('known pathname', () => {
it('should not redirect', () => {
spyWindowLocation = createWindowLocationSpy(origin + '/dashboard/#/workspaces');

main();
expect(spyWindowLocation).not.toHaveBeenCalled();
});
});

describe('wrong pathname', () => {
it('should redirect to home', () => {
spyWindowLocation = createWindowLocationSpy(origin + '/test');

main();
redirectToDashboard();
expect(spyWindowLocation).toHaveBeenCalledWith(origin + '/dashboard/');
});
});
Expand All @@ -190,7 +181,7 @@ describe('test main()', () => {
const query = 'new';
spyWindowLocation = createWindowLocationSpy(origin + '#' + repoUrl + '&' + query);

main();
redirectToDashboard();
expect(spyWindowLocation).toHaveBeenCalledWith(
origin + '/dashboard/f?policies.create=perclick&url=' + encodeURIComponent(repoUrl),
);
Expand All @@ -201,7 +192,7 @@ describe('test main()', () => {
const query = 'devfilePath=my-devfile.yaml';
spyWindowLocation = createWindowLocationSpy(origin + '#' + repoUrl + '&' + query);

main();
redirectToDashboard();
expect(spyWindowLocation).toHaveBeenCalledWith(
origin +
'/dashboard/f?override.devfileFilename=my-devfile.yaml&url=' +
Expand All @@ -215,7 +206,7 @@ describe('test main()', () => {
const remoteUrl = '{https://origin-url,https://upstream-url}';
spyWindowLocation = createWindowLocationSpy(origin + '?' + REMOTES_ATTR + '=' + remoteUrl);

main();
redirectToDashboard();
expect(spyWindowLocation).toHaveBeenCalledWith(
origin + '/dashboard/f?remotes=' + encodeURIComponent(remoteUrl),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/dashboard-frontend/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* Red Hat, Inc. - initial API and implementation
*/

import { main } from './main';
import { redirectToDashboard } from './main';

(function (): void {
main();
redirectToDashboard();
})();
2 changes: 1 addition & 1 deletion packages/dashboard-frontend/src/preload/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { sanitizeLocation } from '../services/helpers/location';
import SessionStorageService, { SessionStorageKey } from '../services/session-storage';

export function main(): void {
export function redirectToDashboard(): void {
if (window.location.pathname.startsWith('/dashboard/')) {
// known location, do nothing
return;
Expand Down

0 comments on commit 58c5853

Please sign in to comment.