Skip to content

Commit

Permalink
feat: changes
Browse files Browse the repository at this point in the history
  • Loading branch information
prc5 committed Dec 29, 2024
1 parent 2c44009 commit 8c6780a
Show file tree
Hide file tree
Showing 38 changed files with 450 additions and 367 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
export const getJestConfig = (): Config.InitialOptions => ({
cache: false,
verbose: true,
testEnvironment: "jest-fixed-jsdom",
testEnvironment: "jsdom",
testTimeout: 1000000,
preset: "ts-jest",
testRegex: [".spec.ts"],
Expand Down
41 changes: 22 additions & 19 deletions jest.polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@
* you don't want to deal with this.
*/

const { TextDecoder, TextEncoder } = require("node:util");
const { ReadableStream, TransformStream } = require("node:stream/web");
if (typeof globalThis === "object") {
const { TextDecoder, TextEncoder } = require("node:util");
const { ReadableStream, TransformStream } = require("node:stream/web");

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
TransformStream: { value: TransformStream },
});
Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
ReadableStream: { value: ReadableStream },
TransformStream: { value: TransformStream },
});

const { Blob, File } = require("node:buffer");
const { fetch, Headers, FormData, Request, Response } = require("undici");
const { Blob, File } = require("node:buffer");
const { Headers, FormData, Request, Response } = require("undici");
const { BroadcastChannel } = require("node:worker_threads");

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
});
Object.defineProperties(globalThis, {
Blob: { value: Blob },
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request, configurable: true },
Response: { value: Response, configurable: true },
BroadcastChannel: { value: BroadcastChannel },
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"jest-watch-typeahead": "^2.2.2",
"jest-websocket-mock": "2.5.0",
"lint-staged": "^13.0.1",
"msw": "^2.6.8",
"msw": "^2.7.0",
"next": "14.2.3",
"notistack": "^2.0.5",
"prettier": "^3.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ describe("Graphql Adapter [ Browser ]", () => {
const timeoutRequest = request.setOptions({ timeout: 50 });
mockRequest(timeoutRequest, { delay: 20 });
await timeoutRequest.send();
// @ts-ignore TODO: check this
expect(instance?.timeout).toBe(50);
expect(instance!.timeout).toBe(50);

window.XMLHttpRequest = xml;
});
Expand Down
Binary file removed packages/core/__tests__/e2e/ada.client.jpg
Binary file not shown.
Empty file.
77 changes: 0 additions & 77 deletions packages/core/__tests__/e2e/client.streams.spec.ts

This file was deleted.

78 changes: 0 additions & 78 deletions packages/core/__tests__/e2e/server.streams.spec.ts

This file was deleted.

23 changes: 1 addition & 22 deletions packages/core/__tests__/features/adapter/adapter.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,7 @@ describe("Fetch Adapter [ Browser ]", () => {
expect(response).toStrictEqual(data);
expect(status).toBe(200);
expect(error).toBe(null);
expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
window.XMLHttpRequest = xml;
});

it("should allow to set options", async () => {
const xml = window.XMLHttpRequest;
let instance: null | XMLHttpRequest = null;
class ExtendedXml extends XMLHttpRequest {
constructor() {
super();
// eslint-disable-next-line @typescript-eslint/no-this-alias
instance = this;
}
}

window.XMLHttpRequest = ExtendedXml;

const timeoutRequest = request.setOptions({ timeout: 50 });
mockRequest(timeoutRequest, { delay: 20 });
await adapter(timeoutRequest, requestId);
expect(instance!.timeout).toBe(50);

expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
window.XMLHttpRequest = xml;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("Fetch Adapter [ Server ]", () => {
expect(response).toStrictEqual(data);
expect(status).toBe(200);
expect(error).toBe(null);
expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "15" } });
});

it("should make a request and return error data with status", async () => {
Expand All @@ -71,7 +71,7 @@ describe("Fetch Adapter [ Server ]", () => {
expect(response).toBe(null);
expect(status).toBe(400);
expect(error).toStrictEqual(data);
expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "19" } });
});

it("should allow to cancel request and return error", async () => {
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("Fetch Adapter [ Server ]", () => {
expect(response).toEqual(mock);
expect(error).toBeNull();
expect(status).toEqual(200);
expect(extra).toStrictEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
expect(extra).toEqual({ headers: { "content-type": "application/json", "content-length": "2" } });
});

it("should allow to calculate payload size", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jest.useFakeTimers().setSystemTime(new Date());

describe("Cache [ Garbage Collector ]", () => {
const cacheKey = "test";
const staleTime = 30;
const staleTime = 30000;
const version = "test";
const cacheTime = 10;
const cacheTime = 10000;
const cacheData: CacheValueType = {
data: null,
error: null,
Expand All @@ -30,22 +30,16 @@ describe("Cache [ Garbage Collector ]", () => {
cacheKey,
};

let lazyStorage = new Map<string, CacheValueType>();

let client = new Client({ url: "shared-base-url" });
let request = client.createRequest()({ endpoint: "shared-endpoint", cacheKey, staleTime, cacheTime });
let cache = createCache(client, {
lazyStorage: createLazyCacheAdapter(lazyStorage),
version,
});

beforeEach(async () => {
lazyStorage.clear();
lazyStorage = new Map<string, CacheValueType>();
client = new Client({ url: "shared-base-url" });
request = client.createRequest()({ endpoint: "shared-endpoint", cacheKey, staleTime, cacheTime });
cache = createCache(client, {
lazyStorage: createLazyCacheAdapter(lazyStorage),
version,
});
jest.resetAllMocks();
Expand All @@ -54,7 +48,7 @@ describe("Cache [ Garbage Collector ]", () => {

describe("when garbage collector is triggered", () => {
it("should garbage collect data from sync storage", async () => {
cache.set(request, cacheData);
cache.set(request.setCacheTime(10), cacheData);
await waitFor(() => {
expect(cache.get(cacheKey)).not.toBeDefined();
});
Expand All @@ -68,17 +62,16 @@ describe("Cache [ Garbage Collector ]", () => {
});
});
it("should schedule garbage collection on mount", async () => {
const storage = new Map();
storage.set("cacheKey", cacheData);
const storage = new Map().set("cacheKey", cacheData);

const cacheInstance = createCache(new Client({ url: "shared-base-url" }), {
storage,
version,
});

expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(1);
});
it("should schedule lazy storage garbage collection on mount", async () => {
const lazyStorage = new Map<string, CacheValueType>();
lazyStorage.set(cacheKey, cacheData);
const cacheInstance = createCache(client, {
lazyStorage: createLazyCacheAdapter(lazyStorage),
Expand All @@ -95,6 +88,7 @@ describe("Cache [ Garbage Collector ]", () => {
expect(spy).toHaveBeenCalledTimes(1);
});
it("should remove resource with not matching lazy version", async () => {
const lazyStorage = new Map<string, CacheValueType>();
const data = { ...cacheData, cacheTime: Time.MIN };
lazyStorage.set(request.cacheKey, data);
createCache(client, {
Expand All @@ -109,6 +103,7 @@ describe("Cache [ Garbage Collector ]", () => {
});
});
it("should remove resource with not matching sync version", async () => {
const lazyStorage = new Map<string, CacheValueType>();
const data = { ...cacheData, cacheTime: Time.MIN };
lazyStorage.set(request.cacheKey, data);
createCache(client, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHttpMockingServer } from "@hyper-fetch/testing";

import {
canRetryRequest,
QueueElementType,
QueueItemType,
DispatcherRequestType,
getDispatcherChangeByKey,
getDispatcherDrainedByKey,
Expand Down Expand Up @@ -111,13 +111,13 @@ describe("Dispatcher [ Utils ]", () => {
describe("When using getRequestType util", () => {
it("should return deduplicated type", async () => {
const request = client.createRequest()({ endpoint: "shared-base-endpoint", deduplicate: true });
const duplicated: QueueElementType<typeof request> = dispatcher.createStorageElement(request);
const duplicated: QueueItemType<typeof request> = dispatcher.createStorageElement(request);
const type = getRequestType(request, duplicated);
expect(type).toBe(DispatcherRequestType.DEDUPLICATED);
});
it("should return cancelable type", async () => {
const request = client.createRequest()({ endpoint: "shared-base-endpoint", cancelable: true });
const duplicated: QueueElementType<typeof request> = dispatcher.createStorageElement(request);
const duplicated: QueueItemType<typeof request> = dispatcher.createStorageElement(request);
const type = getRequestType(request, duplicated);
expect(type).toBe(DispatcherRequestType.PREVIOUS_CANCELED);
});
Expand Down
Loading

0 comments on commit 8c6780a

Please sign in to comment.