Skip to content

Commit 64fedcd

Browse files
authored
utils - fix: adding in keyv instance (#1377)
1 parent 1736917 commit 64fedcd

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

packages/utils/src/is-keyv-instance.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { Keyv } from "keyv";
22

33
// biome-ignore lint/suspicious/noExplicitAny: type format
44
export function isKeyvInstance(keyv: any): boolean {
5+
// Check if keyv is null or undefined
6+
if (keyv === null || keyv === undefined) {
7+
return false;
8+
}
9+
510
// Check if the object is an instance of Keyv
611
if (keyv instanceof Keyv) {
712
return true;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { Keyv } from "keyv";
2+
import { describe, expect, test } from "vitest";
3+
import { isKeyvInstance } from "../src/is-keyv-instance.js";
4+
5+
describe("isKeyvInstance", () => {
6+
test("returns true for actual Keyv instance", () => {
7+
// Arrange
8+
const keyv = new Keyv();
9+
10+
// Act & Assert
11+
expect(isKeyvInstance(keyv)).toBe(true);
12+
});
13+
14+
test("returns true for object with all Keyv methods", () => {
15+
// Arrange
16+
const keyvLike = {
17+
generateIterator: () => {},
18+
get: () => {},
19+
getMany: () => {},
20+
set: () => {},
21+
setMany: () => {},
22+
delete: () => {},
23+
deleteMany: () => {},
24+
has: () => {},
25+
hasMany: () => {},
26+
clear: () => {},
27+
disconnect: () => {},
28+
serialize: () => {},
29+
deserialize: () => {},
30+
};
31+
32+
// Act & Assert
33+
expect(isKeyvInstance(keyvLike)).toBe(true);
34+
});
35+
36+
test("returns false for object missing some Keyv methods", () => {
37+
// Arrange
38+
const notKeyvLike = {
39+
get: () => {},
40+
set: () => {},
41+
delete: () => {},
42+
// Missing other methods
43+
};
44+
45+
// Act & Assert
46+
expect(isKeyvInstance(notKeyvLike)).toBe(false);
47+
});
48+
49+
test("returns false for object with non-function Keyv properties", () => {
50+
// Arrange
51+
const notKeyvLike = {
52+
generateIterator: "not a function",
53+
get: () => {},
54+
getMany: () => {},
55+
set: () => {},
56+
setMany: () => {},
57+
delete: () => {},
58+
deleteMany: () => {},
59+
has: () => {},
60+
hasMany: () => {},
61+
clear: () => {},
62+
disconnect: () => {},
63+
serialize: () => {},
64+
deserialize: () => {},
65+
};
66+
67+
// Act & Assert
68+
expect(isKeyvInstance(notKeyvLike)).toBe(false);
69+
});
70+
71+
test("returns false for null", () => {
72+
// Act & Assert
73+
expect(isKeyvInstance(null)).toBe(false);
74+
});
75+
76+
test("returns false for undefined", () => {
77+
// Act & Assert
78+
expect(isKeyvInstance(undefined)).toBe(false);
79+
});
80+
81+
test("returns false for primitive types", () => {
82+
// Act & Assert
83+
expect(isKeyvInstance("string")).toBe(false);
84+
expect(isKeyvInstance(123)).toBe(false);
85+
expect(isKeyvInstance(true)).toBe(false);
86+
});
87+
88+
test("returns false for plain object", () => {
89+
// Arrange
90+
const plainObject = { foo: "bar" };
91+
92+
// Act & Assert
93+
expect(isKeyvInstance(plainObject)).toBe(false);
94+
});
95+
96+
test("returns false for array", () => {
97+
// Act & Assert
98+
expect(isKeyvInstance([])).toBe(false);
99+
expect(isKeyvInstance([1, 2, 3])).toBe(false);
100+
});
101+
102+
test("returns false for empty object", () => {
103+
// Act & Assert
104+
expect(isKeyvInstance({})).toBe(false);
105+
});
106+
});

0 commit comments

Comments
 (0)