Skip to content

Commit

Permalink
Show errors when parsing Config (#957)
Browse files Browse the repository at this point in the history
* Show errors when parsing Config

* Move getConfig call to component

* Update ConfigLoader.test.tsx

* Wrap test in describe function
  • Loading branch information
andresmgot committed Feb 6, 2019
1 parent 4ac4f0a commit 33581dc
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 7 deletions.
13 changes: 10 additions & 3 deletions dashboard/src/actions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ export const requestConfig = createAction("REQUEST_CONFIG");
export const receiveConfig = createAction("RECEIVE_CONFIG", resolve => {
return (config: IConfig) => resolve(config);
});
export const errorConfig = createAction("ERROR_CONFIG", resolve => {
return (err: Error) => resolve(err);
});

const allActions = [requestConfig, receiveConfig];
const allActions = [requestConfig, receiveConfig, errorConfig];
export type ConfigAction = ActionType<typeof allActions[number]>;

export function getConfig(): ThunkAction<Promise<void>, IStoreState, null, ConfigAction> {
return async dispatch => {
dispatch(requestConfig());
const config = await Config.getConfig();
dispatch(receiveConfig(config));
try {
const config = await Config.getConfig();
dispatch(receiveConfig(config));
} catch (e) {
dispatch(errorConfig(e));
}
};
}
35 changes: 35 additions & 0 deletions dashboard/src/components/ConfigLoader/ConfigLoader.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { shallow } from "enzyme";
import context from "jest-plugin-context";
import * as React from "react";

import ConfigLoader from ".";
import UnexpectedErrorPage from "../../components/ErrorAlert/UnexpectedErrorAlert";
import itBehavesLike from "../../shared/specs";

context("when the config is not ready", () => {
itBehavesLike("aLoadingComponent", {
component: ConfigLoader,
props: {
loaded: false,
getConfig: jest.fn(),
},
});
});

context("when there is an error", () => {
it("renders the error details", () => {
const wrapper = shallow(
<ConfigLoader error={new Error("Wrong config!")} getConfig={jest.fn()} />,
);
expect(wrapper.find(UnexpectedErrorPage)).toExist();
expect(wrapper).toMatchSnapshot();
});
});

describe("componentDidMount", () => {
it("calls getConfig", () => {
const getConfig = jest.fn();
shallow(<ConfigLoader getConfig={getConfig} />);
expect(getConfig).toHaveBeenCalled();
});
})
31 changes: 31 additions & 0 deletions dashboard/src/components/ConfigLoader/ConfigLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from "react";

import UnexpectedErrorPage from "../../components/ErrorAlert/UnexpectedErrorAlert";
import LoadingWrapper, { ILoadingWrapperProps } from "../../components/LoadingWrapper";

interface IConfigLoaderProps extends ILoadingWrapperProps {
getConfig: () => void;
error?: Error;
}

class ConfigLoader extends React.Component<IConfigLoaderProps> {
public componentDidMount() {
this.props.getConfig();
}

public render() {
const { error, ...otherProps } = this.props;
if (error) {
return (
<UnexpectedErrorPage
raw={true}
showGenericMessage={true}
text={`Unable to load Kubeapps configuration: ${error.message}`}
/>
);
}
return <LoadingWrapper {...otherProps} />;
}
}

export default ConfigLoader;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when the config is not ready loading spinner matches the snapshot 1`] = `
<LoadingWrapper
getConfig={
[MockFunction] {
"calls": Array [
Array [],
Array [],
],
}
}
loaded={false}
type={0}
/>
`;

exports[`when there is an error renders the error details 1`] = `
<UnexpectedErrorPage
raw={true}
showGenericMessage={true}
text="Unable to load Kubeapps configuration: Wrong config!"
title="Sorry! Something went wrong."
/>
`;
3 changes: 3 additions & 0 deletions dashboard/src/components/ConfigLoader/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ConfigLoader from "./ConfigLoader";

export default ConfigLoader;
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { ThunkDispatch } from "redux-thunk";
import actions from "../../actions";
import { ConfigAction } from "../../actions/config";

import LoadingWrapper, { ILoadingWrapperProps } from "../../components/LoadingWrapper";
import ConfigLoader from "../../components/ConfigLoader";
import { IStoreState } from "../../shared/types";

function mapStateToProps({ config }: IStoreState): ILoadingWrapperProps {
function mapStateToProps({ config }: IStoreState) {
return {
loaded: config.loaded,
error: config.error,
};
}
function mapDispatchToProps(dispatch: ThunkDispatch<IStoreState, null, ConfigAction>) {
dispatch(actions.config.getConfig());
return {
getConfig: () => dispatch(actions.config.getConfig()),
};
}

export default connect(
mapStateToProps,
mapDispatchToProps,
)(LoadingWrapper);
)(ConfigLoader);
5 changes: 5 additions & 0 deletions dashboard/src/reducers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const configReducer = (state: IConfigState = initialState, action: ConfigAction)
loaded: true,
...action.payload,
};
case getType(actions.config.errorConfig):
return {
...state,
error: action.payload,
};
default:
}
return state;
Expand Down
1 change: 1 addition & 0 deletions dashboard/src/shared/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import axios from "axios";
export interface IConfig {
namespace: string;
appVersion: string;
error?: Error;
}

export default class Config {
Expand Down

0 comments on commit 33581dc

Please sign in to comment.