1import { DiffType } from "../src/utils/diff.js";
2import { ObjectTransformer } from "../src/transform.js";
3import { ObjNode, ObjDiffNode, toPlainObject } from "./utils/tree.js";
4
5describe("ObjectTransformer", () => {
6    it("can transform a simple object", () => {
7        const obj = {
8            obj: {
9                string: 'string',
10                number: 3,
11            },
12            array: [
13                {
14                    nested: "item",
15                },
16                "two",
17            ],
18        };
19
20        const expectedTransformedObj = toPlainObject(
21            new ObjNode('root', [
22                new ObjNode('obj', [
23                    new ObjNode('string: string', [], true, 'root.obj.string'),
24                    new ObjNode('number: 3', [], true, 'root.obj.number'),
25                ], undefined, 'root.obj'),
26                new ObjNode('array', [
27                    new ObjNode('0', [
28                        new ObjNode('nested: item', [], true, 'root.array.0.nested'),
29                    ], undefined, 'root.array.0'),
30                    new ObjNode("1: two", [], true, 'root.array.1'),
31                ], undefined, 'root.array'),
32            ], undefined, 'root')
33        );
34
35        const transformedObj = new ObjectTransformer(obj, 'root', 'root')
36            .setOptions({ formatter: () => { } })
37            .transform();
38
39        expect(transformedObj).toEqual(expectedTransformedObj);
40    });
41
42    it("handles null as expected", () => {
43        const obj = {
44            obj: {
45                null: null,
46            },
47        }
48
49        const expectedTransformedObj = toPlainObject(
50            new ObjNode('root', [
51                new ObjNode('obj', [
52                    new ObjNode('null: null', [], true, 'root.obj.null'),
53                ], undefined, 'root.obj'),
54            ], undefined, 'root')
55        );
56
57        const transformedObj = new ObjectTransformer(obj, 'root', 'root')
58            .setOptions({ formatter: () => { } })
59            .transform();
60
61        expect(transformedObj).toEqual(expectedTransformedObj);
62    });
63
64    it("can generate a simple add diff", () => {
65        const oldObj = {
66            a: {
67                b: 1,
68            },
69            c: 2,
70        };
71
72        const newObj = {
73            a: {
74                b: 1,
75                d: 3,
76            },
77            c: 2,
78        };
79
80        const expectedTransformedObj = toPlainObject(
81            new ObjDiffNode('root', DiffType.NONE, [
82                new ObjDiffNode('a', DiffType.NONE, [
83                    new ObjDiffNode('b: 1', DiffType.NONE, [], true, 'root.a.b'),
84                    new ObjDiffNode('d: 3', DiffType.ADDED, [], true, 'root.a.d'),
85                ], false, 'root.a'),
86                new ObjDiffNode('c: 2', DiffType.NONE, [], true, 'root.c'),
87            ], false, 'root')
88        );
89
90        const transformedObj = new ObjectTransformer(newObj, 'root', 'root')
91            .setOptions({ formatter: () => { } })
92            .withDiff(oldObj)
93            .transform();
94
95        expect(transformedObj).toEqual(expectedTransformedObj);
96    });
97
98    it("can handle null", () => {
99        const oldObj = {
100            a: null,
101        };
102
103        const newObj = {
104            a: 1,
105        };
106
107        const expectedTransformedObj = toPlainObject(
108            new ObjDiffNode('root', DiffType.NONE, [
109                new ObjDiffNode('a', DiffType.NONE, [
110                    new ObjDiffNode('1', DiffType.ADDED, [], false, 'root.a.1'),
111                    new ObjDiffNode('null', DiffType.DELETED, [], false, 'root.a.null'),
112                ], false, 'root.a'),
113            ], false, 'root')
114        );
115
116        const transformedObj = new ObjectTransformer(newObj, 'root', 'root')
117            .setOptions({ formatter: () => { } })
118            .withDiff(oldObj)
119            .transform();
120
121        expect(transformedObj).toEqual(expectedTransformedObj);
122    });
123
124    it("can handle nested null", () => {
125        const oldObj = {
126            a: {
127                b: null,
128            },
129            c: 2,
130        };
131
132        const newObj = {
133            a: {
134                b: 1,
135            },
136            c: 2,
137        };
138
139        const expectedTransformedObj = toPlainObject(
140            new ObjDiffNode('root', DiffType.NONE, [
141                new ObjDiffNode('a', DiffType.NONE, [
142                    new ObjDiffNode('b', DiffType.NONE, [
143                        new ObjDiffNode('1', DiffType.ADDED, [], false, 'root.a.b.1'),
144                        new ObjDiffNode('null', DiffType.DELETED, [],  false, 'root.a.b.null'),
145                    ], false, 'root.a.b'),
146                ], false, 'root.a'),
147                new ObjDiffNode('c: 2', DiffType.NONE, [], true, 'root.c'),
148            ],  false, 'root')
149        );
150
151        const transformedObj = new ObjectTransformer(newObj, 'root', 'root')
152            .setOptions({ formatter: () => { } })
153            .withDiff(oldObj)
154            .transform();
155
156        expect(transformedObj).toEqual(expectedTransformedObj);
157    });
158});
159