|
1 |
| -import { createVertexId } from "@/core"; |
| 1 | +import { createVertexId, EntityRawId } from "@/core"; |
2 | 2 | import { neighborCounts } from "./neighborCounts";
|
3 | 3 | import { query } from "@/utils";
|
4 | 4 | import { NeighborCount } from "../useGEFetchTypes";
|
|
7 | 7 | createGremlinResponse,
|
8 | 8 | createRandomVertexId,
|
9 | 9 | } from "@/utils/testing";
|
| 10 | +import { GMap } from "./types"; |
10 | 11 |
|
11 | 12 | describe("neighborCounts", () => {
|
12 | 13 | it("should return empty for an empty request", async () => {
|
@@ -42,6 +43,7 @@ describe("neighborCounts", () => {
|
42 | 43 | counts: {
|
43 | 44 | label1: 3,
|
44 | 45 | label2: 9,
|
| 46 | + label3: 9, |
45 | 47 | },
|
46 | 48 | };
|
47 | 49 | const response = createResponse({
|
@@ -116,18 +118,86 @@ describe("neighborCounts", () => {
|
116 | 118 | )
|
117 | 119 | `);
|
118 | 120 | });
|
| 121 | + |
| 122 | + it("should handle error response", async () => { |
| 123 | + const mockFetch = vi.fn().mockResolvedValue({ |
| 124 | + code: 500, |
| 125 | + detailedMessage: "Internal server error occurred", |
| 126 | + }); |
| 127 | + |
| 128 | + await expect( |
| 129 | + neighborCounts(mockFetch, { |
| 130 | + vertexIds: [createVertexId("123")], |
| 131 | + }) |
| 132 | + ).rejects.toThrow("Internal server error occurred"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should handle empty response data", async () => { |
| 136 | + const mockFetch = vi |
| 137 | + .fn() |
| 138 | + .mockResolvedValue(createGremlinResponse(createGMap({}))); |
| 139 | + |
| 140 | + const result = await neighborCounts(mockFetch, { |
| 141 | + vertexIds: [createVertexId("123")], |
| 142 | + }); |
| 143 | + |
| 144 | + expect(result.counts).toEqual([]); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should handle vertex with zero neighbors", async () => { |
| 148 | + const vertexId = createRandomVertexId(); |
| 149 | + const expected: NeighborCount = { |
| 150 | + vertexId, |
| 151 | + totalCount: 0, |
| 152 | + counts: {}, |
| 153 | + }; |
| 154 | + const response = createResponse(expected); |
| 155 | + const mockFetch = vi.fn().mockResolvedValue(response); |
| 156 | + |
| 157 | + const result = await neighborCounts(mockFetch, { |
| 158 | + vertexIds: [vertexId], |
| 159 | + }); |
| 160 | + |
| 161 | + expect(result.counts).toEqual([expected]); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should handle mixed vertex ID types", async () => { |
| 165 | + const stringId = createVertexId("string-id"); |
| 166 | + const numberId = createVertexId(42); |
| 167 | + const expected1: NeighborCount = { |
| 168 | + vertexId: stringId, |
| 169 | + totalCount: 5, |
| 170 | + counts: { type1: 5 }, |
| 171 | + }; |
| 172 | + const expected2: NeighborCount = { |
| 173 | + vertexId: numberId, // Gremlin converts number IDs to strings |
| 174 | + totalCount: 3, |
| 175 | + counts: { type2: 3 }, |
| 176 | + }; |
| 177 | + const response = createResponse(expected1, expected2); |
| 178 | + const mockFetch = vi.fn().mockResolvedValue(response); |
| 179 | + |
| 180 | + const result = await neighborCounts(mockFetch, { |
| 181 | + vertexIds: [stringId, numberId], |
| 182 | + }); |
| 183 | + |
| 184 | + expect(result.counts).toHaveLength(2); |
| 185 | + expect(result.counts).toEqual( |
| 186 | + expect.arrayContaining([ |
| 187 | + expect.objectContaining(expected1), |
| 188 | + expect.objectContaining(expected2), |
| 189 | + ]) |
| 190 | + ); |
| 191 | + }); |
119 | 192 | });
|
120 | 193 |
|
121 | 194 | function createResponse(...counts: NeighborCount[]) {
|
122 | 195 | return createGremlinResponse(
|
123 | 196 | createGMap(
|
124 |
| - counts.reduce( |
125 |
| - (prev, curr) => { |
126 |
| - prev[String(curr.vertexId)] = createGMap(curr.counts); |
127 |
| - return prev; |
128 |
| - }, |
129 |
| - {} as Record<string, any> |
130 |
| - ) |
| 197 | + counts.reduce((prev, curr) => { |
| 198 | + prev.set(curr.vertexId, createGMap(curr.counts)); |
| 199 | + return prev; |
| 200 | + }, new Map<EntityRawId, GMap>()) |
131 | 201 | )
|
132 | 202 | );
|
133 | 203 | }
|
0 commit comments