Skip to content

Commit da06fc2

Browse files
add test cases for cache changes
1 parent a5338fa commit da06fc2

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

test/unit/middlewares/cache.test.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("Middleware | Utils | cache", function () {
1919
};
2020

2121
const response = {
22+
statusCode: 200,
2223
send: sinon.spy(),
2324
};
2425

@@ -39,14 +40,16 @@ describe("Middleware | Utils | cache", function () {
3940
expect(response.send.callCount).to.equal(2);
4041
});
4142

42-
it("should invalidate stale the response", function () {
43+
it("should invalidate stale response", function () {
4344
const cacheTestKey = "__cache__2";
4445

4546
const request = {
4647
method: "GET",
4748
originalUrl: "/test2",
4849
};
4950
const response = {
51+
on: sinon.spy(),
52+
statusCode: 200,
5053
send: sinon.spy(),
5154
};
5255

@@ -66,6 +69,9 @@ describe("Middleware | Utils | cache", function () {
6669
cacheMiddlewareForInvalidation(request, response, nextSpy);
6770
response.send(responseBody);
6871

72+
response.on.withArgs("finish").yield();
73+
74+
expect(response.on.callCount).to.equal(1);
6975
expect(nextSpy.callCount).to.equal(2);
7076
expect(response.send.callCount).to.equal(2);
7177

@@ -75,4 +81,79 @@ describe("Middleware | Utils | cache", function () {
7581
expect(nextSpy.callCount).to.equal(3);
7682
expect(response.send.callCount).to.equal(3);
7783
});
84+
85+
it("should not cache the response", function () {
86+
const cacheTestKey = "__cache__3";
87+
const request = {
88+
method: "GET",
89+
originalUrl: "/test3",
90+
};
91+
92+
const response = {
93+
statusCode: 400,
94+
send: sinon.spy(),
95+
};
96+
97+
const nextSpy = sinon.spy();
98+
99+
const cacheMiddleware = cacheResponse({ invalidationKey: cacheTestKey });
100+
101+
cacheMiddleware(request, response, nextSpy);
102+
103+
response.send(responseBody);
104+
105+
expect(nextSpy.callCount).to.equal(1);
106+
expect(response.send.callCount).to.equal(1);
107+
108+
cacheMiddleware(request, response, nextSpy);
109+
response.send(responseBody);
110+
111+
expect(nextSpy.callCount).to.equal(2);
112+
113+
expect(response.send.callCount).to.equal(2);
114+
});
115+
116+
it("should not invalidate stale the response if theres an error", function () {
117+
const cacheTestKey = "__cache__4";
118+
119+
const request = {
120+
method: "GET",
121+
originalUrl: "/test4",
122+
};
123+
const response = {
124+
on: sinon.spy(),
125+
statusCode: 200,
126+
send: sinon.spy(),
127+
};
128+
129+
const nextSpy = sinon.spy();
130+
131+
const cacheMiddlewareForCache = cacheResponse({ invalidationKey: cacheTestKey });
132+
133+
cacheMiddlewareForCache(request, response, nextSpy);
134+
135+
response.send(responseBody);
136+
137+
expect(nextSpy.callCount).to.equal(1);
138+
expect(response.send.callCount).to.equal(1);
139+
140+
response.statusCode = 400;
141+
142+
const cacheMiddlewareForInvalidation = invalidateCache({ invalidationKeys: [cacheTestKey] });
143+
144+
cacheMiddlewareForInvalidation(request, response, nextSpy);
145+
response.send(responseBody);
146+
147+
response.on.withArgs("finish").yield();
148+
149+
expect(response.on.callCount).to.equal(1);
150+
expect(nextSpy.callCount).to.equal(2);
151+
expect(response.send.callCount).to.equal(2);
152+
153+
response.statusCode = 200;
154+
cacheMiddlewareForCache(request, response, nextSpy);
155+
156+
expect(nextSpy.callCount).to.equal(2);
157+
expect(response.send.callCount).to.equal(3);
158+
});
78159
});

0 commit comments

Comments
 (0)