Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add cache unit tests #243

Merged
merged 5 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"cross-fetch": "3.1.4",
"dotenv": "10.0.0",
"emittery": "0.8.1",
"type-fest": "1.2.1"
"type-fest": "^2.12.0"
jstashh marked this conversation as resolved.
Show resolved Hide resolved
},
"size-limit": [
{
Expand Down
131 changes: 131 additions & 0 deletions src/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { PartialDeep } from "type-fest";

import { CachedFetcher } from "./cache";
import { Context } from "./context";

const contextCacheSpy = jest.spyOn(Context.prototype, "cache", "get");
const currentValueSpy = jest.spyOn(CachedFetcher.prototype as any, "currentValue", "get");
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
const fetchWithTimeoutSpy: jest.SpyInstance<Promise<PartialDeep<Response>>> = jest.spyOn(
CachedFetcher.prototype as any,
"fetchWithTimeout"
);
const nowMock = new Date(4242, 4, 2).getTime();
jest.spyOn(Date.prototype, "getTime").mockReturnValue(nowMock);

describe("CachedFetcher", () => {
let cachedFetcher: CachedFetcher<unknown[]>;

beforeEach(() => {
cachedFetcher = new CachedFetcher("path", new Context({}), 1);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("fetch", () => {
it("should return undefined when `useCache` is set to `false`", async () => {
contextCacheSpy.mockReturnValue({
useCache: false,
url: "url"
});

const actualFetch = await cachedFetcher.fetch();

expect(actualFetch).toEqual(undefined);
});

it("should return undefined when `url` is not defined", async () => {
contextCacheSpy.mockReturnValue({
useCache: true
});

const actualFetch = await cachedFetcher.fetch();

expect(actualFetch).toEqual(undefined);
});

describe("when useCache is `true` and there is a url", () => {
beforeAll(() => {
contextCacheSpy.mockReturnValue({
useCache: true,
url: "url"
});
});

it("should return cached when there is a current value", async () => {
currentValueSpy.mockReturnValueOnce({ cachedValue: true });

const actualFetch = await cachedFetcher.fetch();

expect(actualFetch).toEqual({ cachedValue: true });
});

it("should return the JSON", async () => {
const responseMock: PartialDeep<Response> = {
status: 200,
headers: {
get: _ => null
},
json: () =>
Promise.resolve({
foo: "bar"
})
};
fetchWithTimeoutSpy.mockResolvedValueOnce(responseMock);

const actualFetch = await cachedFetcher.fetch("fooParam");

expect(fetchWithTimeoutSpy).toHaveBeenCalledWith("url/v1/chains/1/path?fooParam", 5000);
expect(actualFetch).toEqual({
foo: "bar"
});

expect(cachedFetcher.expiryDate).toEqual(new Date(nowMock + 30 * 1000));
expect(cachedFetcher.cachedValue).toEqual({
foo: "bar"
});
});

it("should log warning when `fetchWithTimeout` fails", async () => {
fetchWithTimeoutSpy.mockImplementation(() => {
throw new Error("fetchWithTimeout failed!");
});

const actualFetch = await cachedFetcher.fetch("fooParam");

expect(consoleWarnSpy).toHaveBeenCalledWith("Call to cache at url/v1/chains/1/path?fooParam timed out");
expect(actualFetch).toEqual(undefined);
});

it("should log warning when `status` is not 200", async () => {
const responseMock: PartialDeep<Response> = {
status: 42,
url: "url42",
statusText: "ultimate question of life, the universe, and everything"
};
fetchWithTimeoutSpy.mockResolvedValueOnce(responseMock);

const actualFetch = await cachedFetcher.fetch("fooParam");

expect(consoleWarnSpy).toHaveBeenCalledWith(
"Call to cache failed at url42 (status 42 ultimate question of life, the universe, and everything)"
);
expect(actualFetch).toEqual(undefined);
});

it("should return when there's no JSON", async () => {
const responseMock: PartialDeep<Response> = {
status: 200,
json: () => Promise.resolve(null)
};
fetchWithTimeoutSpy.mockResolvedValueOnce(responseMock);

const actualFetch = await cachedFetcher.fetch("fooParam");

expect(actualFetch).toEqual(undefined);
});
});
});
});
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6637,11 +6637,6 @@ type-detect@4.0.8:
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==

type-fest@1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.2.1.tgz#232990aa513f3f5223abf54363975dfe3a121a2e"
integrity sha512-SbmIRuXhJs8KTneu77Ecylt9zuqL683tuiLYpTRil4H++eIhqCmx6ko6KAFem9dty8sOdnEiX7j4K1nRE628fQ==

type-fest@^0.21.3:
version "0.21.3"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
Expand All @@ -6657,6 +6652,11 @@ type-fest@^0.8.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

type-fest@^2.12.0:
version "2.12.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.12.0.tgz#ce342f58cab9114912f54b493d60ab39c3fc82b6"
integrity sha512-Qe5GRT+n/4GoqCNGGVp5Snapg1Omq3V7irBJB3EaKsp7HWDo5Gv2d/67gfNyV+d5EXD+x/RF5l1h4yJ7qNkcGA==

typedarray-to-buffer@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
Expand Down