Fixed Tests

This commit is contained in:
Arunavo Ray
2025-07-27 19:09:56 +05:30
parent e637d573a2
commit de314cf174
16 changed files with 814 additions and 748 deletions

56
src/tests/mock-fetch.ts Normal file
View File

@@ -0,0 +1,56 @@
/**
* Mock fetch utility for tests
*/
export function createMockResponse(data: any, options: {
ok?: boolean;
status?: number;
statusText?: string;
headers?: HeadersInit;
jsonError?: Error;
} = {}) {
const {
ok = true,
status = 200,
statusText = 'OK',
headers = { 'content-type': 'application/json' },
jsonError
} = options;
const response = {
ok,
status,
statusText,
headers: new Headers(headers),
json: async () => {
if (jsonError) {
throw jsonError;
}
return data;
},
text: async () => typeof data === 'string' ? data : JSON.stringify(data),
clone: function() {
// Return a new response object with the same properties
return createMockResponse(data, { ok, status, statusText, headers, jsonError });
}
};
return response;
}
export function mockFetch(handler: (url: string, options?: RequestInit) => any) {
return async (url: string, options?: RequestInit) => {
const result = await handler(url, options);
if (result && typeof result === 'object' && !result.clone) {
// If handler returns raw response properties, convert to mock response
if ('ok' in result || 'status' in result) {
const { ok, status, statusText, headers, json, text, ...data } = result;
const responseData = json ? await json() : (text ? await text() : data);
return createMockResponse(responseData, { ok, status, statusText, headers });
}
// If handler returns data directly, wrap it in a mock response
return createMockResponse(result);
}
return result;
};
}

View File

@@ -18,8 +18,8 @@ mock.module("@/lib/db", () => {
})
})
}),
insert: () => ({
values: () => Promise.resolve()
insert: (table: any) => ({
values: (data: any) => Promise.resolve({ insertedId: "mock-id" })
}),
update: () => ({
set: () => ({
@@ -70,6 +70,14 @@ mock.module("@/lib/utils/config-encryption", () => {
};
});
// Mock the helpers module to prevent database operations
mock.module("@/lib/helpers", () => {
return {
createMirrorJob: mock(() => Promise.resolve("mock-job-id")),
// Add other helpers as needed
};
});
// Add DOM testing support if needed
// import { DOMParser } from "linkedom";
// global.DOMParser = DOMParser;