From dad87f2e78f9b8d0e0db0d2e8625941c79561dff Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Sun, 27 Aug 2023 10:55:02 +0300 Subject: [PATCH] tests: add gist endpoint proper cache header test (#3152) --- tests/gist.test.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/gist.test.js b/tests/gist.test.js index bdf95156d5f44..7327b91ea21de 100644 --- a/tests/gist.test.js +++ b/tests/gist.test.js @@ -4,7 +4,7 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import { expect, it, describe, afterEach } from "@jest/globals"; import { renderGistCard } from "../src/cards/gist-card.js"; -import { renderError } from "../src/common/utils.js"; +import { renderError, CONSTANTS } from "../src/common/utils.js"; import gist from "../api/gist.js"; const gist_data = { @@ -170,4 +170,27 @@ describe("Test /api/gist", () => { renderError("Something went wrong", "Language not found"), ); }); + + it("should have proper cache", async () => { + const req = { + query: { + id: "bbfce31e0217a3689c8d961a356cb10d", + }, + }; + const res = { + setHeader: jest.fn(), + send: jest.fn(), + }; + mock.onPost("https://api.github.com/graphql").reply(200, gist_data); + + await gist(req, res); + + expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); + expect(res.setHeader).toBeCalledWith( + "Cache-Control", + `max-age=${CONSTANTS.FOUR_HOURS / 2}, s-maxage=${ + CONSTANTS.FOUR_HOURS + }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + ); + }); });