Skip to content

Commit 6b457d0

Browse files
authored
tests: return cache tests which were removed in 53e4041 (#4368)
1 parent ff7e1e2 commit 6b457d0

File tree

4 files changed

+153
-3
lines changed

4 files changed

+153
-3
lines changed

tests/api.test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,40 @@ describe("Test /api/", () => {
168168
);
169169
});
170170

171+
it("should have proper cache", async () => {
172+
const { req, res } = faker({}, data_stats);
173+
174+
await api(req, res);
175+
176+
expect(res.setHeader.mock.calls).toEqual([
177+
["Content-Type", "image/svg+xml"],
178+
[
179+
"Cache-Control",
180+
`max-age=${CONSTANTS.CARD_CACHE_SECONDS}, s-maxage=${
181+
CONSTANTS.CARD_CACHE_SECONDS
182+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
183+
],
184+
]);
185+
});
186+
187+
it("should set proper cache", async () => {
188+
const cache_seconds = CONSTANTS.TWELVE_HOURS;
189+
const { req, res } = faker({ cache_seconds }, data_stats);
190+
await api(req, res);
191+
192+
expect(res.setHeader.mock.calls).toEqual([
193+
["Content-Type", "image/svg+xml"],
194+
[
195+
"Cache-Control",
196+
`max-age=${
197+
cache_seconds
198+
}, s-maxage=${cache_seconds}, stale-while-revalidate=${
199+
CONSTANTS.ONE_DAY
200+
}`,
201+
],
202+
]);
203+
});
204+
171205
it("should set shorter cache when error", async () => {
172206
const { req, res } = faker({}, error);
173207
await api(req, res);
@@ -183,6 +217,54 @@ describe("Test /api/", () => {
183217
]);
184218
});
185219

220+
it("should set proper cache with clamped values", async () => {
221+
{
222+
let { req, res } = faker({ cache_seconds: 200000 }, data_stats);
223+
await api(req, res);
224+
225+
expect(res.setHeader.mock.calls).toEqual([
226+
["Content-Type", "image/svg+xml"],
227+
[
228+
"Cache-Control",
229+
`max-age=${CONSTANTS.TWO_DAY}, s-maxage=${
230+
CONSTANTS.TWO_DAY
231+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
232+
],
233+
]);
234+
}
235+
236+
// note i'm using block scoped vars
237+
{
238+
let { req, res } = faker({ cache_seconds: 0 }, data_stats);
239+
await api(req, res);
240+
241+
expect(res.setHeader.mock.calls).toEqual([
242+
["Content-Type", "image/svg+xml"],
243+
[
244+
"Cache-Control",
245+
`max-age=${CONSTANTS.ONE_DAY}, s-maxage=${
246+
CONSTANTS.ONE_DAY
247+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
248+
],
249+
]);
250+
}
251+
252+
{
253+
let { req, res } = faker({ cache_seconds: -10000 }, data_stats);
254+
await api(req, res);
255+
256+
expect(res.setHeader.mock.calls).toEqual([
257+
["Content-Type", "image/svg+xml"],
258+
[
259+
"Cache-Control",
260+
`max-age=${CONSTANTS.TWELVE_HOURS}, s-maxage=${
261+
CONSTANTS.TWELVE_HOURS
262+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
263+
],
264+
]);
265+
}
266+
});
267+
186268
it("should allow changing ring_color", async () => {
187269
const { req, res } = faker(
188270
{

tests/gist.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import axios from "axios";
44
import MockAdapter from "axios-mock-adapter";
55
import { expect, it, describe, afterEach } from "@jest/globals";
66
import { renderGistCard } from "../src/cards/gist-card.js";
7-
import { renderError } from "../src/common/utils.js";
7+
import { CONSTANTS, renderError } from "../src/common/utils.js";
88
import gist from "../api/gist.js";
99

1010
const gist_data = {
@@ -170,4 +170,25 @@ describe("Test /api/gist", () => {
170170
renderError("Something went wrong", "Language not found"),
171171
);
172172
});
173+
174+
it("should have proper cache", async () => {
175+
const req = {
176+
query: {
177+
id: "bbfce31e0217a3689c8d961a356cb10d",
178+
},
179+
};
180+
const res = {
181+
setHeader: jest.fn(),
182+
send: jest.fn(),
183+
};
184+
mock.onPost("https://api.github.com/graphql").reply(200, gist_data);
185+
186+
await gist(req, res);
187+
188+
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
189+
expect(res.setHeader).toBeCalledWith(
190+
"Cache-Control",
191+
`max-age=${CONSTANTS.TWO_DAY}, s-maxage=${CONSTANTS.TWO_DAY}`,
192+
);
193+
});
173194
});

tests/pin.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import axios from "axios";
44
import MockAdapter from "axios-mock-adapter";
55
import pin from "../api/pin.js";
66
import { renderRepoCard } from "../src/cards/repo-card.js";
7-
import { renderError } from "../src/common/utils.js";
7+
import { CONSTANTS, renderError } from "../src/common/utils.js";
88
import { expect, it, describe, afterEach } from "@jest/globals";
99

1010
const data_repo = {
@@ -201,4 +201,28 @@ describe("Test /api/pin", () => {
201201
),
202202
);
203203
});
204+
205+
it("should have proper cache", async () => {
206+
const req = {
207+
query: {
208+
username: "anuraghazra",
209+
repo: "convoychat",
210+
},
211+
};
212+
const res = {
213+
setHeader: jest.fn(),
214+
send: jest.fn(),
215+
};
216+
mock.onPost("https://api.github.com/graphql").reply(200, data_user);
217+
218+
await pin(req, res);
219+
220+
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
221+
expect(res.setHeader).toBeCalledWith(
222+
"Cache-Control",
223+
`max-age=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, s-maxage=${
224+
CONSTANTS.PIN_CARD_CACHE_SECONDS
225+
}`,
226+
);
227+
});
204228
});

tests/top-langs.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import axios from "axios";
44
import MockAdapter from "axios-mock-adapter";
55
import topLangs from "../api/top-langs.js";
66
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
7-
import { renderError } from "../src/common/utils.js";
7+
import { CONSTANTS, renderError } from "../src/common/utils.js";
88
import { expect, it, describe, afterEach } from "@jest/globals";
99

1010
const data_langs = {
@@ -208,4 +208,27 @@ describe("Test /api/top-langs", () => {
208208
renderError("Something went wrong", "Locale not found"),
209209
);
210210
});
211+
212+
it("should have proper cache", async () => {
213+
const req = {
214+
query: {
215+
username: "anuraghazra",
216+
},
217+
};
218+
const res = {
219+
setHeader: jest.fn(),
220+
send: jest.fn(),
221+
};
222+
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);
223+
224+
await topLangs(req, res);
225+
226+
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
227+
expect(res.setHeader).toBeCalledWith(
228+
"Cache-Control",
229+
`max-age=${CONSTANTS.TOP_LANGS_CACHE_SECONDS / 2}, s-maxage=${
230+
CONSTANTS.TOP_LANGS_CACHE_SECONDS
231+
}`,
232+
);
233+
});
211234
});

0 commit comments

Comments
 (0)