1 |
rakinar2 |
575 |
import * as cache from "@framework/cache/GlobalStore"; |
2 |
|
|
import { afterEach, describe, expect, it } from "vitest"; |
3 |
|
|
import { createApplication } from "../../mocks/application.mock"; |
4 |
|
|
|
5 |
|
|
describe("GlobalStore", () => { |
6 |
|
|
createApplication(); |
7 |
|
|
|
8 |
|
|
afterEach(() => { |
9 |
|
|
cache.clear(); |
10 |
|
|
}); |
11 |
|
|
|
12 |
|
|
it("should be able to store and retrieve data", () => { |
13 |
|
|
cache.set("test", "value"); |
14 |
|
|
expect(cache.get("test")).toBe("value"); |
15 |
|
|
}); |
16 |
|
|
|
17 |
|
|
it("should be able to store and retrieve data with dependencies", async () => { |
18 |
|
|
await cache.withDeps("test", () => "value", ["dep"]); |
19 |
|
|
const value1 = await cache.withDeps("test", () => "value2", ["dep"]); |
20 |
|
|
const value2 = await cache.withDeps("test", () => "value2", ["dep2"]); |
21 |
|
|
|
22 |
|
|
expect(value1).toBe("value"); |
23 |
|
|
expect(value2).toBe("value2"); |
24 |
|
|
}); |
25 |
|
|
|
26 |
|
|
it("dependencies work even if they are references", async () => { |
27 |
|
|
const dep = [["dep"]]; |
28 |
|
|
await cache.withDeps("test", () => "value", dep); |
29 |
|
|
const value1 = await cache.withDeps("test", () => "value2", dep); |
30 |
|
|
const value2 = await cache.withDeps("test", () => "value3", [["dep"]]); |
31 |
|
|
const value3 = await cache.withDeps("test", () => "value4", [["dep2"]]); |
32 |
|
|
expect(value1).toBe("value"); |
33 |
|
|
expect(value2).toBe("value"); |
34 |
|
|
expect(value3).toBe("value4"); |
35 |
|
|
}); |
36 |
|
|
|
37 |
|
|
it("should be able to store and retrieve data with TTL", async () => { |
38 |
|
|
await cache.set("test", "value", { ttl: 100 }); |
39 |
|
|
await new Promise(resolve => setTimeout(resolve, 200)); |
40 |
|
|
expect(cache.get("test")).toBeUndefined(); |
41 |
|
|
}); |
42 |
|
|
|
43 |
|
|
it("should be able to store and retrieve data with max hits", async () => { |
44 |
|
|
await cache.set("test1", "value", { maxHits: 1 }); |
45 |
|
|
expect(cache.get("test1")).toBe("value"); |
46 |
|
|
expect(cache.get("test1")).toBe(undefined); |
47 |
|
|
}); |
48 |
|
|
|
49 |
|
|
it("should be able to store and retrieve data with dependencies and TTL", async () => { |
50 |
|
|
await cache.withDeps("test", () => "value", ["dep"], { ttl: 100 }); |
51 |
|
|
await new Promise(resolve => setTimeout(resolve, 200)); |
52 |
|
|
expect(cache.get("test")).toBeUndefined(); |
53 |
|
|
}); |
54 |
|
|
|
55 |
|
|
it("should be able to store and retrieve data with dependencies and max hits", async () => { |
56 |
|
|
await cache.withDeps("test", () => "value", ["dep"], { maxHits: 1 }); |
57 |
|
|
expect(cache.get("test")).toBe("value"); |
58 |
|
|
expect(cache.get("test")).toBeUndefined(); |
59 |
|
|
}); |
60 |
|
|
}); |