Skip to content

Commit

Permalink
Refactor code to use formatNamespacedRouteForResource from @console/s…
Browse files Browse the repository at this point in the history
…hared
  • Loading branch information
Jiri Tomasek committed Jan 7, 2020
1 parent 4b98fbc commit f689737
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 39 deletions.
33 changes: 17 additions & 16 deletions frontend/__tests__/actions/ui.spec.ts
@@ -1,16 +1,17 @@
import * as _ from 'lodash-es';
import { ALL_NAMESPACES_KEY, LAST_PERSPECTIVE_LOCAL_STORAGE_KEY } from '@console/shared';

import { formatNamespacedRouteForResource } from '@console/shared/src/utils/namespace';
import '../../__mocks__/localStorage';
import store from '../../public/redux';
import * as UIActions from '../../public/actions/ui';
import * as router from '../../public/components/utils/router';
import { getActiveNamespace } from '@console/internal/reducers/ui';

const setActiveNamespace = (ns) => store.dispatch(UIActions.setActiveNamespace(ns));
const setActivePerspective = (perspective) =>
store.dispatch(UIActions.setActivePerspective(perspective));
const getNamespacedRoute = (path) =>
UIActions.formatNamespaceRoute(UIActions.getActiveNamespace(), path);
UIActions.formatNamespaceRoute(getActiveNamespace(store.getState()), path);

describe('ui-actions', () => {
describe('UIActions.formatNamespaceRoute', () => {
Expand All @@ -37,47 +38,47 @@ describe('ui-actions', () => {

it('should set active namespace in memory', () => {
setActiveNamespace('test1');
expect(UIActions.getActiveNamespace()).toEqual('test1');
expect(getActiveNamespace(store.getState())).toEqual('test1');
setActiveNamespace('test2');
expect(UIActions.getActiveNamespace()).toEqual('test2');
expect(getActiveNamespace(store.getState())).toEqual('test2');
});

it('sets active namespace in memory to all-namespaces', () => {
setActiveNamespace('test');
setActiveNamespace(ALL_NAMESPACES_KEY);
expect(_.isUndefined(UIActions.getActiveNamespace())).toBe(false);
expect(UIActions.getActiveNamespace()).toEqual(ALL_NAMESPACES_KEY);
expect(_.isUndefined(getActiveNamespace(store.getState()))).toBe(false);
expect(getActiveNamespace(store.getState())).toEqual(ALL_NAMESPACES_KEY);
});

it('should redirect namespaced location paths for known namespace-friendly prefixes', () => {
window.location.pathname = '/k8s/ns/floorwax/pods';
setActiveNamespace('dessert-topping');
expect(UIActions.formatNamespacedRouteForResource('pods')).toEqual(
'/k8s/ns/dessert-topping/pods',
);
expect(
formatNamespacedRouteForResource('pods', getActiveNamespace(store.getState())),
).toEqual('/k8s/ns/dessert-topping/pods');
});

it('should redirect namespaced location paths to their prefixes', () => {
window.location.pathname = '/k8s/ns/floorwax/pods/new-shimmer';
setActiveNamespace(ALL_NAMESPACES_KEY); // reset active namespace
expect(UIActions.formatNamespacedRouteForResource('pods')).toEqual(
'/k8s/all-namespaces/pods',
);
expect(
formatNamespacedRouteForResource('pods', getActiveNamespace(store.getState())),
).toEqual('/k8s/all-namespaces/pods');
});

it('should redirect to all if no namespaces is selected', () => {
window.location.pathname = '/k8s/ns/floorwax/pods';
setActiveNamespace(ALL_NAMESPACES_KEY);
expect(UIActions.formatNamespacedRouteForResource('pods')).toEqual(
'/k8s/all-namespaces/pods',
);
expect(
formatNamespacedRouteForResource('pods', getActiveNamespace(store.getState())),
).toEqual('/k8s/all-namespaces/pods');
});

it("should not redirect if the current path isn't namespaced, but should set active namespace in memory", () => {
window.location.pathname = '/not-a-namespaced-path';
setActiveNamespace('dessert-topping');
expect(window.location.pathname).toEqual('/not-a-namespaced-path');
expect(UIActions.getActiveNamespace()).toEqual('dessert-topping');
expect(getActiveNamespace(store.getState())).toEqual('dessert-topping');
});

it('should redirect to list view if current path is "new" and setting to "all-namespaces"', () => {
Expand Down
Expand Up @@ -3,7 +3,7 @@ import { Gallery, GalleryItem } from '@patternfly/react-core';
import { CatalogTile } from '@patternfly/react-catalog-view-extension';
import { connect } from 'react-redux';
import { history, PageHeading, useAccessReview } from '@console/internal/components/utils';
import { formatNamespacedRouteForResource } from '@console/internal/actions/ui';
import { formatNamespacedRouteForResource } from '@console/shared/src/utils/namespace';
import {
BuildConfigModel,
ImageStreamModel,
Expand Down
9 changes: 0 additions & 9 deletions frontend/public/actions/ui.ts
Expand Up @@ -98,15 +98,6 @@ export const getPodMetric = (pod: PodKind, metric: string): number => {
return _.get(metrics, [metric, pod.metadata.namespace, pod.metadata.name], 0);
};

export const formatNamespacedRouteForResource = (
resource,
activeNamespace = getActiveNamespace(),
) => {
return activeNamespace === ALL_NAMESPACES_KEY
? `/k8s/all-namespaces/${resource}`
: `/k8s/ns/${activeNamespace}/${resource}`;
};

export const formatNamespaceRoute = (activeNamespace, originalPath, location?) => {
let path = originalPath.substr(window.SERVER_FLAGS.basePath.length);

Expand Down
8 changes: 2 additions & 6 deletions frontend/public/components/masthead-toolbar.jsx
Expand Up @@ -19,9 +19,8 @@ import {
ToolbarItem,
} from '@patternfly/react-core';
import classNames from 'classnames';

import { FLAGS, YellowExclamationTriangleIcon } from '@console/shared';
import * as UIActions from '../actions/ui';
import { formatNamespacedRouteForResource } from '@console/shared/src/utils';
import { connectToFlags, flagPending } from '../reducers/features';
import { authSvc } from '../module/auth';
import { getOCMLink } from '../module/k8s';
Expand Down Expand Up @@ -172,10 +171,7 @@ class MastheadToolbarContents_ extends React.Component {

_onImportYAML(e) {
e.preventDefault();
const importYAMLPath = UIActions.formatNamespacedRouteForResource(
'import',
this.props.activeNamespace,
);
const importYAMLPath = formatNamespacedRouteForResource('import', this.props.activeNamespace);
history.push(importYAMLPath);
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/public/components/nav/admin-nav.tsx
Expand Up @@ -2,8 +2,9 @@ import * as React from 'react';
import { connect } from 'react-redux';
import { NavItemSeparator } from '@patternfly/react-core';

import { featureReducerName } from '../../reducers/features';
import { FLAGS } from '@console/shared';
import { formatNamespacedRouteForResource } from '@console/shared/src/utils';
import { featureReducerName } from '../../reducers/features';
import { monitoringReducerName, MonitoringRoutes } from '../../reducers/monitoring';

import {
Expand All @@ -25,7 +26,6 @@ import {
import { referenceForModel } from '../../module/k8s';
import { ExternalLink, HrefLink, ResourceNSLink, ResourceClusterLink } from './items';
import { NavSection } from './section';
import { formatNamespacedRouteForResource } from '../../actions/ui';

type SeparatorProps = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion frontend/public/components/nav/items.tsx
Expand Up @@ -3,7 +3,7 @@ import { Link, LinkProps } from 'react-router-dom';
import * as _ from 'lodash-es';
import { NavItem } from '@patternfly/react-core';
import { connect } from 'react-redux';
import { formatNamespacedRouteForResource } from '../../actions/ui';
import { formatNamespacedRouteForResource } from '@console/shared/src/utils';
import { referenceForModel, K8sKind } from '../../module/k8s';
import { stripBasePath } from '../utils';
import * as plugins from '../../plugins';
Expand Down
7 changes: 3 additions & 4 deletions frontend/public/components/overview/index.tsx
Expand Up @@ -12,6 +12,7 @@ import {
OverviewItem,
getResourceList,
} from '@console/shared';
import { formatNamespacedRouteForResource } from '@console/shared/src/utils';
import { coFetchJSON } from '../../co-fetch';
import { PROMETHEUS_TENANCY_BASE_PATH } from '../graphs';
import { TextFilter } from '../factory';
Expand Down Expand Up @@ -360,10 +361,8 @@ class OverviewMainContent_ extends React.Component<
title="No Workloads Found."
detail={
<div>
<Link to={UIActions.formatNamespacedRouteForResource('import', namespace)}>
Import YAML
</Link>{' '}
or <Link to={`/add/ns/${namespace}`}>add other content</Link> to your project.
<Link to={formatNamespacedRouteForResource('import', namespace)}>Import YAML</Link> or{' '}
<Link to={`/add/ns/${namespace}`}>add other content</Link> to your project.
</div>
}
/>
Expand Down

0 comments on commit f689737

Please sign in to comment.