Skip to content

Commit f132d0f

Browse files
fix: improve FormData mock to handle form property access via Proxy
1 parent 8bcf5ab commit f132d0f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

tests/resources/drafts.spec.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@ jest.mock('formdata-node', () => {
1010
return {
1111
FormData: jest.fn().mockImplementation(function(this: MockedFormData) {
1212
const appendedData: Record<string, any> = {};
13-
const form = {
14-
append: (key: string, value: any, _filename?: string) => {
15-
if (value && typeof value === 'object' && 'content' in value) {
16-
// Handle File objects
17-
appendedData[key] = value.content;
18-
} else {
19-
appendedData[key] = value;
20-
}
21-
},
22-
_getAppendedData: () => appendedData,
13+
14+
this.append = (key: string, value: any, _filename?: string) => {
15+
if (value && typeof value === 'object' && 'content' in value) {
16+
// Handle File objects
17+
appendedData[key] = value.content;
18+
} else {
19+
appendedData[key] = value;
20+
}
2321
};
24-
Object.assign(this, form);
25-
(this as any).form = this;
26-
return this;
22+
23+
this._getAppendedData = () => appendedData;
24+
25+
// Create a proxy to handle both direct access and form property access
26+
return new Proxy(this, {
27+
get: (target, prop) => {
28+
if (prop === 'form') {
29+
return target;
30+
}
31+
return target[prop as keyof typeof target];
32+
}
33+
});
2734
}),
2835
File: jest.fn().mockImplementation((content: any[], name: string) => ({
2936
content,

0 commit comments

Comments
 (0)