Remove legacy renderBooleans prop (#5483)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-06-02 08:59:23 -04:00
1 parent b86d409c92
commit d4fbab7176
11 files changed
+564 -66

No files matched your search

@@ -0,0 +1,225 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`<CustomResourceDetails /> with a CRD with a boolean field should display false in an additionalPrinterColumn as 'false' 1`] = `
<div>
<div
class="CrdResourceDetails MyCrd"
>
<div
class="DrawerItem"
>
<span
class="name"
>
Created
</span>
<span
class="value"
>
&lt;unknown&gt;
ago
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
Name
</span>
<span
class="value"
>
first-crd
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
MyField
</span>
<span
class="value"
>
<ul>
<li>
false
</li>
</ul>
</span>
</div>
</div>
</div>
`;
exports[`<CustomResourceDetails /> with a CRD with a boolean field should display true in an additionalPrinterColumn as 'true' 1`] = `
<div>
<div
class="CrdResourceDetails MyCrd"
>
<div
class="DrawerItem"
>
<span
class="name"
>
Created
</span>
<span
class="value"
>
&lt;unknown&gt;
ago
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
Name
</span>
<span
class="value"
>
first-crd
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
MyField
</span>
<span
class="value"
>
<ul>
<li>
true
</li>
</ul>
</span>
</div>
</div>
</div>
`;
exports[`<CustomResourceDetails /> with a CRD with a number field should display 0 in an additionalPrinterColumn as '0' 1`] = `
<div>
<div
class="CrdResourceDetails MyCrd"
>
<div
class="DrawerItem"
>
<span
class="name"
>
Created
</span>
<span
class="value"
>
&lt;unknown&gt;
ago
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
Name
</span>
<span
class="value"
>
first-crd
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
MyField
</span>
<span
class="value"
>
<ul>
<li>
0
</li>
</ul>
</span>
</div>
</div>
</div>
`;
exports[`<CustomResourceDetails /> with a CRD with a number field should display 1234 in an additionalPrinterColumn as '1234' 1`] = `
<div>
<div
class="CrdResourceDetails MyCrd"
>
<div
class="DrawerItem"
>
<span
class="name"
>
Created
</span>
<span
class="value"
>
&lt;unknown&gt;
ago
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
Name
</span>
<span
class="value"
>
first-crd
</span>
</div>
<div
class="DrawerItem"
>
<span
class="name"
>
MyField
</span>
<span
class="value"
>
<ul>
<li>
1234
</li>
</ul>
</span>
</div>
</div>
</div>
`;
@@ -0,0 +1,206 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { CustomResourceDefinition } from "../../../../common/k8s-api/endpoints";
import { KubeObject } from "../../../../common/k8s-api/kube-object";
import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import { CustomResourceDetails } from "../crd-resource-details";
describe("<CustomResourceDetails />", () => {
let render: DiRender;
beforeEach(() => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
render = renderFor(di);
});
describe("with a CRD with a boolean field", () => {
let crd: CustomResourceDefinition;
beforeEach(() => {
crd = new CustomResourceDefinition({
apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition",
metadata: {
name: "my-crd",
resourceVersion: "1",
selfLink: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions/my-crd",
uid: "1",
},
spec: {
versions: [{
name: "v1",
served: true,
storage: true,
schema: {
openAPIV3Schema: {
type: "object",
properties: {
spec: {
type: "object",
properties: {
"my-field": {
type: "boolean",
},
},
},
},
},
},
additionalPrinterColumns: [
{
name: "MyField",
jsonPath: ".spec.my-field",
type: "boolean",
},
],
}],
group: "stable.lens.dev",
names: {
kind: "MyCrd",
plural: "my-crds",
},
scope: "Cluster",
},
});
});
it("should display false in an additionalPrinterColumn as 'false'", () => {
const cr = new KubeObject({
apiVersion: "stable.lens.dev/v1",
kind: "MyCrd",
metadata: {
name: "first-crd",
resourceVersion: "1",
selfLink: "stable.lens.dev/v1/first-crd",
uid: "2",
},
spec: {
"my-field": false,
},
});
const result = render(<CustomResourceDetails crd={crd} object={cr} />);
expect(result.container).toMatchSnapshot();
expect(result.getByText("false")).toBeTruthy();
});
it("should display true in an additionalPrinterColumn as 'true'", () => {
const cr = new KubeObject({
apiVersion: "stable.lens.dev/v1",
kind: "MyCrd",
metadata: {
name: "first-crd",
resourceVersion: "1",
selfLink: "stable.lens.dev/v1/first-crd",
uid: "2",
},
spec: {
"my-field": true,
},
});
const result = render(<CustomResourceDetails crd={crd} object={cr} />);
expect(result.container).toMatchSnapshot();
expect(result.getByText("true")).toBeTruthy();
});
});
describe("with a CRD with a number field", () => {
let crd: CustomResourceDefinition;
beforeEach(() => {
crd = new CustomResourceDefinition({
apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition",
metadata: {
name: "my-crd",
resourceVersion: "1",
selfLink: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions/my-crd",
uid: "1",
},
spec: {
versions: [{
name: "v1",
served: true,
storage: true,
schema: {
openAPIV3Schema: {
type: "object",
properties: {
spec: {
type: "object",
properties: {
"my-field": {
type: "number",
},
},
},
},
},
},
additionalPrinterColumns: [
{
name: "MyField",
jsonPath: ".spec.my-field",
type: "number",
},
],
}],
group: "stable.lens.dev",
names: {
kind: "MyCrd",
plural: "my-crds",
},
scope: "Cluster",
},
});
});
it("should display 0 in an additionalPrinterColumn as '0'", () => {
const cr = new KubeObject({
apiVersion: "stable.lens.dev/v1",
kind: "MyCrd",
metadata: {
name: "first-crd",
resourceVersion: "1",
selfLink: "stable.lens.dev/v1/first-crd",
uid: "2",
},
spec: {
"my-field": 0,
},
});
const result = render(<CustomResourceDetails crd={crd} object={cr} />);
expect(result.container).toMatchSnapshot();
expect(result.getByText("0")).toBeTruthy();
});
it("should display 1234 in an additionalPrinterColumn as '1234'", () => {
const cr = new KubeObject({
apiVersion: "stable.lens.dev/v1",
kind: "MyCrd",
metadata: {
name: "first-crd",
resourceVersion: "1",
selfLink: "stable.lens.dev/v1/first-crd",
uid: "2",
},
spec: {
"my-field": 1234,
},
});
const result = render(<CustomResourceDetails crd={crd} object={cr} />);
expect(result.container).toMatchSnapshot();
expect(result.getByText("1234")).toBeTruthy();
});
});
});
@@ -25,7 +25,7 @@ export interface CustomResourceDetailsProps extends KubeObjectDetailsProps<KubeO
crd: CustomResourceDefinition;
}
function convertSpecValue(value: any): any {
function convertSpecValue(value: unknown): React.ReactNode {
if (Array.isArray(value)) {
return (
<ul>
@@ -50,18 +50,22 @@ function convertSpecValue(value: any): any {
);
}
return value;
if (
typeof value === "boolean"
|| typeof value === "string"
|| typeof value === "number"
) {
return value.toString();
}
return null;
}
@observer
export class CustomResourceDetails extends React.Component<CustomResourceDetailsProps> {
renderAdditionalColumns(resource: KubeObject, columns: AdditionalPrinterColumnsV1[]) {
return columns.map(({ name, jsonPath }) => (
<DrawerItem
key={name}
name={name}
renderBoolean
>
<DrawerItem key={name} name={name}>
{convertSpecValue(JSONPath.query(resource, convertKubectlJsonPathToNodeJsonPath(jsonPath)))}
</DrawerItem>
));