Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Oct 11, 2024
1 parent cdc6ad1 commit 53e4041
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 156 deletions.
1 change: 0 additions & 1 deletion api/top-langs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
import { blacklist } from "../src/common/blacklist.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
Expand Down
82 changes: 0 additions & 82 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,40 +168,6 @@ describe("Test /api/", () => {
);
});

it("should have proper cache", async () => {
const { req, res } = faker({}, data_stats);

await api(req, res);

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});

it("should set proper cache", async () => {
const cache_seconds = 35000;
const { req, res } = faker({ cache_seconds }, data_stats);
await api(req, res);

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${
cache_seconds / 2
}, s-maxage=${cache_seconds}, stale-while-revalidate=${
CONSTANTS.ONE_DAY
}`,
],
]);
});

it("should set shorter cache when error", async () => {
const { req, res } = faker({}, error);
await api(req, res);
Expand All @@ -217,54 +183,6 @@ describe("Test /api/", () => {
]);
});

it("should set proper cache with clamped values", async () => {
{
let { req, res } = faker({ cache_seconds: 200000 }, data_stats);
await api(req, res);

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.ONE_DAY / 2}, s-maxage=${
CONSTANTS.ONE_DAY
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}

// note i'm using block scoped vars
{
let { req, res } = faker({ cache_seconds: 0 }, data_stats);
await api(req, res);

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}

{
let { req, res } = faker({ cache_seconds: -10000 }, data_stats);
await api(req, res);

expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}
});

it("should allow changing ring_color", async () => {
const { req, res } = faker(
{
Expand Down
25 changes: 1 addition & 24 deletions tests/gist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, CONSTANTS } from "../src/common/utils.js";
import { renderError } from "../src/common/utils.js";
import gist from "../api/gist.js";

const gist_data = {
Expand Down Expand Up @@ -170,27 +170,4 @@ 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.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});
26 changes: 1 addition & 25 deletions tests/pin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import pin from "../api/pin.js";
import { renderRepoCard } from "../src/cards/repo-card.js";
import { renderError, CONSTANTS } from "../src/common/utils.js";
import { renderError } from "../src/common/utils.js";
import { expect, it, describe, afterEach } from "@jest/globals";

const data_repo = {
Expand Down Expand Up @@ -201,28 +201,4 @@ describe("Test /api/pin", () => {
),
);
});

it("should have proper cache", async () => {
const req = {
query: {
username: "anuraghazra",
repo: "convoychat",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_user);

await pin(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});
25 changes: 1 addition & 24 deletions tests/top-langs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import topLangs from "../api/top-langs.js";
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
import { renderError, CONSTANTS } from "../src/common/utils.js";
import { renderError } from "../src/common/utils.js";
import { expect, it, describe, afterEach } from "@jest/globals";

const data_langs = {
Expand Down Expand Up @@ -208,27 +208,4 @@ describe("Test /api/top-langs", () => {
renderError("Something went wrong", "Locale not found"),
);
});

it("should have proper cache", async () => {
const req = {
query: {
username: "anuraghazra",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

await topLangs(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.SIX_HOURS / 2}, s-maxage=${
CONSTANTS.SIX_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});

0 comments on commit 53e4041

Please sign in to comment.