Files
editor/src/libs/json-path-to-position.test.ts
T
Harel M 1730e9cb1c Codemirror 5 to 6 upgrade (#1386)
## Launch Checklist

- Resolves #891

This PR upgrades code mirror from version 5 to version 6.
It should not change any functionality dramatically.
The filter and other expressions have line numbers now as I was not able
to remove those without introducing a lot of code, which I preferred not
to.

Before:

<img width="571" height="933" alt="image"
src="https://github.com/user-attachments/assets/02f047ee-0857-4eb1-9431-2620099ea025"
/>


After:
<img width="571" height="933" alt="image"
src="https://github.com/user-attachments/assets/7cf60155-7cd9-4c06-915e-dec2ae8247fc"
/>



 - [x] Briefly describe the changes in this PR.
 - [x] Link to related issues.
- [x] Include before/after visuals or gifs if this PR includes visual
changes.
 - [x] Write tests for all new functionality.
 - [x] Add an entry to `CHANGELOG.md` under the `## main` section.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-09-17 19:51:26 +02:00

68 lines
1.9 KiB
TypeScript

import jsonToAst from "json-to-ast";
import { describe, it, expect } from "vitest";
import { jsonPathToPosition } from "./json-path-to-position";
describe("json-path-to-position", () => {
it("should get position of a simple key", () => {
const json = {
"key1": "value1",
"key2": "value2"
};
const text = JSON.stringify(json);
const ast = jsonToAst(text);
const node = jsonPathToPosition(["key1"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value1"');
});
it("should get position of second key", () => {
const json = {
"key1": "value1",
"key2": "value2"
};
const text = JSON.stringify(json);
const ast = jsonToAst(text);
const node = jsonPathToPosition(["key2"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"value2"');
});
it("should get position key in array", () => {
const json = {
"layers": [
{
"id": "layer1"
}, {
"id": "layer2"
}
]
};
const text = JSON.stringify(json);
const ast = jsonToAst(text);
const node = jsonPathToPosition(["layers", "1", "id"], ast);
expect(text.slice(node!.loc!.start.offset, node!.loc!.end.offset)).toBe('"layer2"');
});
it("should return undefined when key does not exist", () => {
const json = {
"layers": [
{
"id": "layer1"
}, {
"id": "layer2"
}
]
};
const text = JSON.stringify(json);
const ast = jsonToAst(text);
const node = jsonPathToPosition(["layers", "2", "id"], ast);
expect(node).toBe(undefined);
});
it("should return undefined for value type", () => {
const json = 1;
const text = JSON.stringify(json);
const ast = jsonToAst(text);
const node = jsonPathToPosition(["id"], ast);
expect(node).toBe(undefined);
});
});