Skip to content

Commit

Permalink
Fix resource table when working with lists (#2122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor committed Oct 26, 2020
1 parent 41cc376 commit e5a1bea
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const sampleResourceRef = {
} as ResourceRef;

const deployment = {
kind: "Deployment",
metadata: {
name: "foo",
},
Expand Down Expand Up @@ -135,3 +136,44 @@ it("renders a table with an error", () => {
expect(row.name).toEqual("foo");
expect(wrapper.text()).toContain("Error: Boom!");
});

it("do not fail if the resources are already populated but the refs not yet", () => {
const state = getStore({
kube: {
items: {
"deployment-foo": {
isFetching: false,
item: deployment as IResource,
},
},
},
});
const wrapper = mountWrapper(state, <ResourceTable {...defaultProps} resourceRefs={[]} />);
expect(wrapper.find(Table)).not.toExist();
});

it("renders the table with two entries with a list reference", () => {
const state = getStore({
kube: {
items: {
"deployment-foo": {
isFetching: false,
item: {
items: [
deployment as IResource,
{ ...deployment, metadata: { name: "bar" } } as IResource,
],
},
},
},
},
});
const wrapper = mountWrapper(
state,
<ResourceTable
{...defaultProps}
resourceRefs={[{ ...sampleResourceRef, name: "" } as ResourceRef]}
/>,
);
expect(wrapper.find(Table).prop("data")).toHaveLength(2);
});
26 changes: 17 additions & 9 deletions dashboard/src/components/AppView/ResourceTable/ResourceTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,25 @@ function ResourceTable({ id, title, resourceRefs }: IResourceTableProps) {
flattenResources(resourceRefs, state.kube.items),
);

const columns = useMemo(() => getColumns(resourceRefs[0]), [resourceRefs]);
const columns = useMemo(
() => (resourceRefs.length ? getColumns(resourceRefs[0]) : OtherResourceColumns),
[resourceRefs],
);
const data = useMemo(
() =>
resources.map((resource, index) =>
getData(
resourceRefs[index].name,
columns.map(c => c.accessor),
columns.map(c => c.getter),
resource,
),
),
resources.map((resource, index) => {
// When the resourceRef is a list, the list of references will be just one
const ref = resourceRefs.length === 1 ? resourceRefs[0] : resourceRefs[index];
if (ref) {
return getData(
ref.name,
columns.map(c => c.accessor),
columns.map(c => c.getter),
resource,
);
}
return {};
}),
[columns, resourceRefs, resources],
);

Expand Down

0 comments on commit e5a1bea

Please sign in to comment.