1 |
import * as objects from "@framework/utils/objects"; |
2 |
import { describe, expect, it } from "vitest"; |
3 |
|
4 |
describe("utils/objects", () => { |
5 |
describe("get", () => { |
6 |
it("should return the value at the specified path", () => { |
7 |
const object = { |
8 |
a: { |
9 |
b: { |
10 |
c: "value" |
11 |
} |
12 |
} |
13 |
}; |
14 |
|
15 |
expect(objects.get(object, "a.b.c")).toBe("value"); |
16 |
}); |
17 |
|
18 |
it("should return undefined if the path does not exist", () => { |
19 |
const object = { |
20 |
a: { |
21 |
b: { |
22 |
c: "value" |
23 |
} |
24 |
} |
25 |
}; |
26 |
|
27 |
expect(objects.get(object, "a.b.d")).toBeUndefined(); |
28 |
}); |
29 |
|
30 |
it("should return undefined if the path is invalid", () => { |
31 |
const object = { |
32 |
a: { |
33 |
b: { |
34 |
c: "value" |
35 |
} |
36 |
} |
37 |
}; |
38 |
|
39 |
expect(objects.get(object, "a.b.c.d")).toBeUndefined(); |
40 |
}); |
41 |
|
42 |
it("should return undefined if the object is null", () => { |
43 |
expect(objects.get(null as unknown as object, "a.b.c")).toBeUndefined(); |
44 |
}); |
45 |
|
46 |
it("should return undefined if the object is undefined", () => { |
47 |
expect(objects.get(undefined as unknown as object, "a.b.c")).toBeUndefined(); |
48 |
}); |
49 |
|
50 |
it("should return undefined if the object is not an object", () => { |
51 |
expect(objects.get("string" as unknown as object, "a.b.c")).toBeUndefined(); |
52 |
}); |
53 |
|
54 |
it("should throw if the path is not a string or is empty string", () => { |
55 |
const object = { |
56 |
a: { |
57 |
b: { |
58 |
c: "value" |
59 |
} |
60 |
} |
61 |
}; |
62 |
|
63 |
expect(() => objects.get(object, 1 as unknown as string)).toThrowError(); |
64 |
expect(() => objects.get(object, "" as unknown as string)).toThrowError(); |
65 |
}); |
66 |
|
67 |
it("should return undefined if the path is a space", () => { |
68 |
const object = { |
69 |
a: { |
70 |
b: { |
71 |
c: "value" |
72 |
} |
73 |
} |
74 |
}; |
75 |
|
76 |
expect(objects.get(object, " ")).toBeUndefined(); |
77 |
}); |
78 |
|
79 |
it("should return undefined if the path is a period", () => { |
80 |
const object = { |
81 |
a: { |
82 |
b: { |
83 |
c: "value" |
84 |
} |
85 |
} |
86 |
}; |
87 |
|
88 |
expect(objects.get(object, ".")).toBeUndefined(); |
89 |
}); |
90 |
}); |
91 |
}); |