mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* move node-fetch to separate package Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix node-fetch package runtime error Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit nx dependency Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * fix test:unit nx dependency Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> * Add prepare step for node-fetch Signed-off-by: Sebastian Malton <sebastian@malton.name> * Update lock Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove dead comment Signed-off-by: Sebastian Malton <sebastian@malton.name> * Remove unnecessary fetchModuleInjectable Signed-off-by: Sebastian Malton <sebastian@malton.name> Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Signed-off-by: Sebastian Malton <sebastian@malton.name> Co-authored-by: Sebastian Malton <sebastian@malton.name>
63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
import type { Response, Headers as NodeFetchHeaders } from "@k8slens/node-fetch";
|
|
import { PassThrough } from "stream";
|
|
|
|
export const createMockResponseFromString = (url: string, data: string, statusCode = 200) => {
|
|
const res: jest.Mocked<Response> = {
|
|
buffer: jest.fn(async () => { throw new Error("buffer() is not supported"); }),
|
|
clone: jest.fn(() => res),
|
|
arrayBuffer: jest.fn(async () => { throw new Error("arrayBuffer() is not supported"); }),
|
|
blob: jest.fn(async () => { throw new Error("blob() is not supported"); }),
|
|
body: new PassThrough(),
|
|
bodyUsed: false,
|
|
headers: new Headers() as NodeFetchHeaders,
|
|
json: jest.fn(async () => JSON.parse(await res.text())),
|
|
ok: 200 <= statusCode && statusCode < 300,
|
|
redirected: 300 <= statusCode && statusCode < 400,
|
|
size: data.length,
|
|
status: statusCode,
|
|
statusText: "some-text",
|
|
text: jest.fn(async () => data),
|
|
type: "basic",
|
|
url,
|
|
formData: jest.fn(async () => { throw new Error("formData() is not supported"); }),
|
|
};
|
|
|
|
return res;
|
|
};
|
|
|
|
export const createMockResponseFromStream = (url: string, stream: NodeJS.ReadableStream, statusCode = 200) => {
|
|
const res: jest.Mocked<Response> = {
|
|
buffer: jest.fn(async () => { throw new Error("buffer() is not supported"); }),
|
|
clone: jest.fn(() => res),
|
|
arrayBuffer: jest.fn(async () => { throw new Error("arrayBuffer() is not supported"); }),
|
|
blob: jest.fn(async () => { throw new Error("blob() is not supported"); }),
|
|
body: stream,
|
|
bodyUsed: false,
|
|
headers: new Headers() as NodeFetchHeaders,
|
|
json: jest.fn(async () => JSON.parse(await res.text())),
|
|
ok: 200 <= statusCode && statusCode < 300,
|
|
redirected: 300 <= statusCode && statusCode < 400,
|
|
size: 10,
|
|
status: statusCode,
|
|
statusText: "some-text",
|
|
text: jest.fn(() => {
|
|
const chunks: Buffer[] = [];
|
|
|
|
return new Promise((resolve, reject) => {
|
|
stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
|
|
stream.on("error", (err) => reject(err));
|
|
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
});
|
|
}),
|
|
type: "basic",
|
|
url,
|
|
formData: jest.fn(async () => { throw new Error("formData() is not supported"); }),
|
|
};
|
|
|
|
return res;
|
|
};
|