From f41a9da71b28efa8ffea9b74372605b111d47756 Mon Sep 17 00:00:00 2001 From: KATT Date: Sun, 1 Oct 2023 02:40:58 +0200 Subject: [PATCH] this could maybe work --- src/handlers/tsonCircular.test.ts | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/handlers/tsonCircular.test.ts diff --git a/src/handlers/tsonCircular.test.ts b/src/handlers/tsonCircular.test.ts new file mode 100644 index 00000000..a1a31be0 --- /dev/null +++ b/src/handlers/tsonCircular.test.ts @@ -0,0 +1,50 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { expect, test } from "vitest"; + +import { createTson } from "../tson.js"; +import { tsonCircular } from "./index.js"; + +test("circular", () => { + const t = createTson({ + types: [tsonCircular], + }); + + { + const expected: any = {}; + + expected.a = expected; + + expect(t.serialize(expected).json).toBe({ + a: [ + // + "Circular", + 0, // <-- 0 means that the direct parent is circular target + "__tson", + ], + }); + + // const deserialized = t.parse(stringified); + // expect(deserialized).toEqual(expected); + } + + { + const expected: any = { + a: { + b: {}, + }, + }; + + expected.a.b.a = expected; + + expect(t.serialize(expected).json).toBe({ + a: [ + // + "Circular", + 1, // <-- 1 means that the grandparent is circular target + "__tson", + ], + }); + } +});