Skip to content

Commit f4ea147

Browse files
committed
test(endpoint-cache): add tests for remove/has/clear operations
1 parent b778bca commit f4ea147

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

packages/endpoint-cache/src/EndpointCache.spec.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { EndpointCache } from "./EndpointCache";
66
jest.mock("mnemonist");
77

88
describe(EndpointCache.name, () => {
9+
let endpointCache;
10+
const capacity = 100;
11+
const key = "key";
12+
913
const now = Date.now();
1014
const set = jest.fn();
1115
const get = jest.fn();
@@ -33,28 +37,23 @@ describe(EndpointCache.name, () => {
3337
has,
3438
clear,
3539
});
40+
endpointCache = new EndpointCache(capacity);
3641
});
3742

3843
afterEach(() => {
3944
jest.clearAllMocks();
4045
});
4146

4247
it("passes capacity to LRUCache", () => {
43-
const capacity = 100;
44-
new EndpointCache(capacity);
4548
expect(LRUCache).toHaveBeenCalledTimes(1);
4649
expect(LRUCache).toHaveBeenCalledWith(capacity);
4750
});
4851

4952
describe("get", () => {
50-
let endpointCache;
51-
const key = "key";
52-
5353
beforeEach(() => {
5454
has.mockReturnValue(true);
5555
get.mockReturnValue(getEndpointsWithExpiry(mockEndpoints));
5656
jest.spyOn(Date, "now").mockImplementation(() => now);
57-
endpointCache = new EndpointCache(100);
5857
});
5958

6059
const verifyHasAndGetCalls = () => {
@@ -112,12 +111,8 @@ describe(EndpointCache.name, () => {
112111
});
113112

114113
describe("set", () => {
115-
let endpointCache;
116-
const key = "key";
117-
118114
beforeEach(() => {
119115
jest.spyOn(Date, "now").mockImplementation(() => now);
120-
endpointCache = new EndpointCache(100);
121116
});
122117

123118
it("converts CachePeriodInMinutes to Expires before caching", () => {
@@ -146,4 +141,21 @@ describe(EndpointCache.name, () => {
146141
expect(set).toHaveBeenCalledWith(key, [{ Address: "address", Expires: now + 60 * 1000 }]);
147142
});
148143
});
144+
145+
it("remove", () => {
146+
endpointCache.remove(key);
147+
expect(set).toHaveBeenCalledTimes(1);
148+
expect(set).toHaveBeenCalledWith(key, []);
149+
});
150+
151+
it("has", () => {
152+
endpointCache.has(key);
153+
expect(has).toHaveBeenCalledTimes(1);
154+
expect(has).toHaveBeenCalledWith(key);
155+
});
156+
157+
it("clear", () => {
158+
endpointCache.clear();
159+
expect(clear).toHaveBeenCalledTimes(1);
160+
});
149161
});

0 commit comments

Comments
 (0)