This commit is contained in:
HarelM
2025-09-17 11:12:56 +03:00
parent 9081107e73
commit e83e570ec1
+57 -57
View File
@@ -3,65 +3,65 @@ import { describe, it, expect } from "vitest";
import { jsonPathToPosition } from "./json-path-to-position"; import { jsonPathToPosition } from "./json-path-to-position";
describe("json-path-to-position", () => { describe("json-path-to-position", () => {
it("should get position of a simple key", () => { it("should get position of a simple key", () => {
const json = { const json = {
"key1": "value1", "key1": "value1",
"key2": "value2" "key2": "value2"
}; };
const text = JSON.stringify(json); const text = JSON.stringify(json);
const ast = jsonToAst(text); const ast = jsonToAst(text);
const node = jsonPathToPosition(["key1"], ast); const node = jsonPathToPosition(["key1"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value1"'); expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value1"');
}); });
it("should get position of second key", () => { it("should get position of second key", () => {
const json = { const json = {
"key1": "value1", "key1": "value1",
"key2": "value2" "key2": "value2"
}; };
const text = JSON.stringify(json); const text = JSON.stringify(json);
const ast = jsonToAst(text); const ast = jsonToAst(text);
const node = jsonPathToPosition(["key2"], ast); const node = jsonPathToPosition(["key2"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value2"'); expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value2"');
}); });
it("should get position key in array", () => { it("should get position key in array", () => {
const json = { const json = {
"layers": [ "layers": [
{ {
"id": "layer1" "id": "layer1"
}, { }, {
"id": "layer2" "id": "layer2"
} }
] ]
}; };
const text = JSON.stringify(json); const text = JSON.stringify(json);
const ast = jsonToAst(text); const ast = jsonToAst(text);
const node = jsonPathToPosition(["layers", "1", "id"], ast); const node = jsonPathToPosition(["layers", "1", "id"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"layer2"'); expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"layer2"');
}); });
it("should return undefined when key does not exist", () => { it("should return undefined when key does not exist", () => {
const json = { const json = {
"layers": [ "layers": [
{ {
"id": "layer1" "id": "layer1"
}, { }, {
"id": "layer2" "id": "layer2"
} }
] ]
}; };
const text = JSON.stringify(json); const text = JSON.stringify(json);
const ast = jsonToAst(text); const ast = jsonToAst(text);
const node = jsonPathToPosition(["layers", "2", "id"], ast); const node = jsonPathToPosition(["layers", "2", "id"], ast);
expect(node).toBe(undefined); expect(node).toBe(undefined);
}); });
it("should return undefined for value type", () => { it("should return undefined for value type", () => {
const json = 1; const json = 1;
const text = JSON.stringify(json); const text = JSON.stringify(json);
const ast = jsonToAst(text); const ast = jsonToAst(text);
const node = jsonPathToPosition(["id"], ast); const node = jsonPathToPosition(["id"], ast);
expect(node).toBe(undefined); expect(node).toBe(undefined);
}); });
}); });