1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/utility-features/utilities/src/jsonPath.test.ts
Sebastian Malton 2789bcebcb
Convert runMany and runManySync to use injectManyWithMeta + move to seperate package (#7244)
* Convert runMany and runManySync to use injectManyWithMeta

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors due to new Runnable requirements

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add documentation for verifyRunnablesAreDAG

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Simplify convertToWithIdWith

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move all utility functions to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move testing utilities to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move run-many and run-many-sync to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Replace all internal uses of utilities with new packages

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use new @k8slens/run-many package in core

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add dep to open-lens

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of @k8slens/test-utils

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup getGlobalOverride

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move tests to new package too

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of AsyncResult and autoBind

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup remaining import issues

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Finial fixups to fix build

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix lint

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Revert moving "testUsingFakeTime" to separate package

- This fixes tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix integration tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix unit test failing due to spelling fix

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-03-10 10:07:28 +02:00

179 lines
4.8 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { convertKubectlJsonPathToNodeJsonPath, safeJSONPathValue } from "./jsonPath";
describe("convertKubectlJsonPathToNodeJsonPath", () => {
it("should convert \\. to use indexed notation", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels.kubesphere\\.io/alias-name");
expect(res).toBe("$.metadata.labels['kubesphere.io/alias-name']");
});
it("should convert keys with escaped characters to use indexed notation", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels.kubesphere\\\"io/alias-name");
expect(res).toBe("$.metadata.labels['kubesphere\"io/alias-name']");
});
it("should convert '-' to use indexed notation", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels.alias-name");
expect(res).toBe("$.metadata.labels['alias-name']");
});
it("should drop leading dot if first group is converted to index notation", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata\\.labels.alias-name");
expect(res).toBe("$['metadata.labels']['alias-name']");
});
it("should handle scenario when both \\. and indexed notation are present", () => {
const rest = convertKubectlJsonPathToNodeJsonPath(".metadata.labels\\.serving['some.other.item']");
expect(rest).toBe("$.metadata['labels.serving']['some.other.item']");
});
it("should not touch given jsonPath if no invalid characters present", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".status.conditions[?(@.type=='Ready')].status");
expect(res).toBe("$.status.conditions[?(@.type=='Ready')].status");
});
it("strips '\\' away from the result", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']");
expect(res).toBe("$.metadata.labels['serving.knative.dev/configuration']");
});
it("converts all [] to [0]", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels[].foo[]");
expect(res).toBe("$.metadata.labels[0].foo[0]");
});
it("removes trailing ..", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels[]..");
expect(res).toBe("$.metadata.labels[0]");
});
it("converts ending ...name to ..name", () => {
const res = convertKubectlJsonPathToNodeJsonPath(".metadata.labels[]...name");
expect(res).toBe("$.metadata.labels[0]..name");
});
});
describe("safeJSONPathValue", () => {
let oldWarn: typeof console["warn"];
beforeEach(() => {
oldWarn = console.warn;
console.warn = jest.fn();
});
afterEach(() => {
console.warn = oldWarn;
});
it("should convert boolean values to strings", () => {
const res = safeJSONPathValue({ bar: false }, ".bar");
expect(res).toBe(false);
});
it("should convert number values to strings", () => {
const res = safeJSONPathValue({ bar: 0 }, ".bar");
expect(res).toBe(0);
});
it("should join sliced entries with commas only", () => {
const res = safeJSONPathValue({
bar: [
{
foo: 1,
},
{
foo: "hello",
},
],
}, ".bar[].foo");
expect(res).toBe(1);
});
it("should join an array of values using JSON.stringify", () => {
const res = safeJSONPathValue({
bar: [
"world",
"hello",
],
}, ".bar");
expect(res).toEqual(["world", "hello"]);
});
it("should stringify an object value", () => {
const res = safeJSONPathValue({
foo: { bar: "bat" },
}, ".foo");
expect(res).toEqual({ "bar":"bat" });
});
it("should use convertKubectlJsonPathToNodeJsonPath", () => {
const res = safeJSONPathValue({
foo: { "hello.world": "bat" },
}, ".foo.hello\\.world");
expect(res).toBe("bat");
});
it("should not throw when given '.spec.metrics[*].external.highWatermark..'", () => {
const obj = {
spec: {
metrics: [
{
external: {
metricName: "cpu",
highWatermark: "100",
},
},
{
external: {
metricName: "memory",
highWatermark: "100",
},
},
],
},
};
const res = safeJSONPathValue(obj, ".spec.metrics[*].external.highWatermark..");
expect(res).toEqual(["100", "100"]);
});
it("should not throw if path is invalid jsonpath", () => {
const res = safeJSONPathValue({
foo: { "hello.world": "bat" },
}, "asd[");
expect(res).toBe(undefined);
});
it("should retrive value with '/' in jsonpath", () => {
const res = safeJSONPathValue({
foo: {
"hello/world": "bat",
},
}, ".foo.hello/world");
expect(res).toBe("bat");
});
});