Skip to content

Commit

Permalink
fix: improve FormData mock with properly typed Proxy implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] committed Dec 25, 2024
1 parent eef4e5a commit d9126f0
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions tests/resources/drafts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('formdata-node', () => {
const getAppendedData = () => appendedData;

const createMockFormData = () => {
const instance = {
const mockInstance: MockedFormData = {
append(key: string, value: any): void {
if (value && typeof value === 'object' && 'content' in value) {
// Handle File objects
Expand All @@ -24,16 +24,23 @@ jest.mock('formdata-node', () => {
}
},
_getAppendedData: getAppendedData,
form: null as any, // Will be set by proxy
};

Object.defineProperty(instance, 'form', {
get() {
return instance;
},
enumerable: true,
// Create a proxy to ensure all properties are available on both the instance and form
const proxy: MockedFormData = new Proxy(mockInstance, {
get(target: MockedFormData, prop: string | symbol): any {
if (prop === 'form') {
return proxy;
}
return target[prop as keyof MockedFormData];
}
});

return instance;
// Set the form property to point to the proxy itself
mockInstance.form = proxy;

return proxy;
};

const mockFormData = createMockFormData() as MockedFormData;
Expand Down

0 comments on commit d9126f0

Please sign in to comment.