Skip to content

Commit b778bca

Browse files
committed
test(endpoint-cache): add tests for set operation
1 parent 5f03663 commit b778bca

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,40 @@ describe(EndpointCache.name, () => {
110110
});
111111
});
112112
});
113+
114+
describe("set", () => {
115+
let endpointCache;
116+
const key = "key";
117+
118+
beforeEach(() => {
119+
jest.spyOn(Date, "now").mockImplementation(() => now);
120+
endpointCache = new EndpointCache(100);
121+
});
122+
123+
it("converts CachePeriodInMinutes to Expires before caching", () => {
124+
endpointCache.set(key, mockEndpoints);
125+
expect(set).toHaveBeenCalledTimes(1);
126+
expect(set).toHaveBeenCalledWith(
127+
key,
128+
mockEndpoints.map(({ Address, CachePeriodInMinutes }) => ({
129+
Address,
130+
Expires: now + CachePeriodInMinutes * 60 * 1000,
131+
}))
132+
);
133+
});
134+
135+
it("sets Address to empty string if not passed", () => {
136+
const mockEnpointsNoAddr = [{ CachePeriodInMinutes: 1 }];
137+
endpointCache.set(key, mockEnpointsNoAddr);
138+
expect(set).toHaveBeenCalledTimes(1);
139+
expect(set).toHaveBeenCalledWith(key, [{ Address: "", Expires: now + 60 * 1000 }]);
140+
});
141+
142+
it("sets Expires in one minute if CachePeriodInMinutes is not passed", () => {
143+
const mockEnpointsNoAddr = [{ Address: "address" }];
144+
endpointCache.set(key, mockEnpointsNoAddr);
145+
expect(set).toHaveBeenCalledTimes(1);
146+
expect(set).toHaveBeenCalledWith(key, [{ Address: "address", Expires: now + 60 * 1000 }]);
147+
});
148+
});
113149
});

0 commit comments

Comments
 (0)