Skip to content

Commit 3aac146

Browse files
Update unit tests
Signed-off-by: Prateek Kumar <[email protected]>
1 parent b719dfd commit 3aac146

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

node/tests/GlideClient.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
it,
1212
} from "@jest/globals";
1313
import {
14-
convertGlideRecordToRecord,
1514
Decoder,
1615
FlushMode,
1716
FunctionRestorePolicy,
@@ -46,6 +45,56 @@ import {
4645
waitForScriptNotBusy,
4746
} from "./TestUtilities";
4847

48+
/**
49+
* @internal
50+
* Check whether an object is a `GlideRecord` (see {@link GlideRecord}).
51+
*/
52+
export function isGlideRecord(obj?: unknown): boolean {
53+
return (
54+
Array.isArray(obj) &&
55+
obj.length > 0 &&
56+
typeof obj[0] === "object" &&
57+
"key" in obj[0] &&
58+
"value" in obj[0]
59+
);
60+
}
61+
62+
/**
63+
* @internal
64+
* Check whether an object is a `GlideRecord[]` (see {@link GlideRecord}).
65+
*/
66+
function isGlideRecordArray(obj?: unknown): boolean {
67+
return Array.isArray(obj) && obj.length > 0 && isGlideRecord(obj[0]);
68+
}
69+
70+
/**
71+
* @internal
72+
* Recursively downcast `GlideRecord` to `Record`. Use if `data` keys are always strings.
73+
*/
74+
export function convertGlideRecordToRecord<T>(
75+
data: GlideRecord<T>,
76+
): Record<string, T> {
77+
const res: Record<string, T> = {};
78+
79+
for (const pair of data) {
80+
let newVal = pair.value;
81+
82+
if (isGlideRecord(pair.value)) {
83+
newVal = convertGlideRecordToRecord(
84+
pair.value as GlideRecord<unknown>,
85+
) as T;
86+
} else if (isGlideRecordArray(pair.value)) {
87+
newVal = (pair.value as GlideRecord<unknown>[]).map(
88+
convertGlideRecordToRecord,
89+
) as T;
90+
}
91+
92+
res[pair.key as string] = newVal;
93+
}
94+
95+
return res;
96+
}
97+
4998
const TIMEOUT = 50000;
5099

51100
export declare class Script {

0 commit comments

Comments
 (0)