Skip to content

Commit

Permalink
Fix a null pointer in fetchNamespaces (#2973)
Browse files Browse the repository at this point in the history
* Add test if no 'namespaces' is provided

* Check if fetchNamespaces response contains 'namespaces'

* Simplify code with lodash.get
  • Loading branch information
antgamdia committed Jun 15, 2021
1 parent 11c64cf commit b471dcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions dashboard/src/actions/namespace.test.tsx
Expand Up @@ -83,6 +83,22 @@ describe("fetchNamespaces", () => {
expect(store.getActions()).toEqual(expectedActions);
});

it("dispatches errorNamespace if the request returns no 'namespaces'", async () => {
Namespace.list = jest.fn().mockImplementationOnce(() => {
return {};
});
const err = new Error("The current account does not have access to any namespaces");
const expectedActions = [
{
type: getType(errorNamespaces),
payload: { cluster: "default-c", err, op: "list" },
},
];

await store.dispatch(fetchNamespaces("default-c"));
expect(store.getActions()).toEqual(expectedActions);
});

it("dispatches errorNamespace if error listing namespaces", async () => {
const err = new Error("Bang!");
Namespace.list = jest.fn().mockImplementationOnce(() => Promise.reject(err));
Expand Down
5 changes: 4 additions & 1 deletion dashboard/src/actions/namespace.ts
@@ -1,5 +1,6 @@
import { ThunkAction } from "redux-thunk";
import { Kube } from "shared/Kube";
import { get } from "lodash";

import { ActionType, deprecated } from "typesafe-actions";

Expand Down Expand Up @@ -55,7 +56,9 @@ export function fetchNamespaces(
return async dispatch => {
try {
const namespaceList = await Namespace.list(cluster);
const namespaceStrings = namespaceList.namespaces.map((n: IResource) => n.metadata.name);
const namespaceStrings = get(namespaceList, "namespaces", []).map(
(n: IResource) => n.metadata.name,
);
if (namespaceStrings.length === 0) {
dispatch(
errorNamespaces(
Expand Down

0 comments on commit b471dcf

Please sign in to comment.