Skip to content

Commit 79e7e20

Browse files
committed
fix test
1 parent da4c5f9 commit 79e7e20

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

apps/todo-app/app/components/__tests__/add-todo.test.tsx

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,58 @@ interface RemixFormConfig {
5757
[key: string]: unknown;
5858
}
5959

60-
vi.mock('remix-hook-form', () => ({
61-
RemixFormProvider: ({ children }: { children: ReactNode }) => children,
62-
useRemixForm: (config: RemixFormConfig) => {
63-
return {
64-
...config,
65-
getValues: (_name: string) => testInputValue,
66-
reset: vi.fn(() => {
67-
testInputValue = '';
68-
// Force re-render by dispatching a custom event
69-
const inputs = document.querySelectorAll('input[name="text"]');
70-
inputs.forEach(input => {
71-
(input as HTMLInputElement).value = '';
72-
});
73-
}),
74-
setValue: vi.fn((_name: string, value: string) => {
75-
testInputValue = value;
76-
}),
77-
register: vi.fn((name: string) => ({
78-
name,
79-
onChange: (e: ChangeEvent<HTMLInputElement>) => {
80-
testInputValue = e.target.value;
81-
},
82-
value: testInputValue
83-
})),
84-
handleSubmit: vi.fn((onValid: (data: { text: string }) => void) => (e: FormEvent) => {
85-
e.preventDefault();
86-
if (testInputValue?.trim()) {
87-
onValid({ text: testInputValue.trim() });
88-
}
89-
}),
90-
formState: { errors: {} },
91-
watch: vi.fn((_name: string) => testInputValue)
92-
};
93-
}
94-
}));
60+
vi.mock('remix-hook-form', () => {
61+
let latestConfig: RemixFormConfig | undefined;
62+
return {
63+
RemixFormProvider: ({ children }: { children: ReactNode }) => children,
64+
useRemixForm: (config: RemixFormConfig) => {
65+
latestConfig = config;
66+
return {
67+
...config,
68+
getValues: (_name: string) => testInputValue,
69+
reset: vi.fn(() => {
70+
testInputValue = '';
71+
// Force re-render by dispatching a custom event
72+
const inputs = document.querySelectorAll('input[name="text"]');
73+
inputs.forEach(input => {
74+
(input as HTMLInputElement).value = '';
75+
});
76+
}),
77+
setValue: vi.fn((_name: string, value: string) => {
78+
testInputValue = value;
79+
}),
80+
register: vi.fn((name: string) => ({
81+
name,
82+
onChange: (e: ChangeEvent<HTMLInputElement>) => {
83+
testInputValue = e.target.value;
84+
},
85+
value: testInputValue
86+
})),
87+
handleSubmit: vi.fn((arg?: unknown) => {
88+
// Support both usages:
89+
// 1) onSubmit={methods.handleSubmit} → arg is the FormEvent
90+
// 2) onSubmit={methods.handleSubmit(onValid)} → arg is the onValid callback
91+
const isEvent = arg && typeof (arg as FormEvent).preventDefault === 'function';
92+
if (isEvent) {
93+
const e = arg as FormEvent;
94+
e.preventDefault();
95+
const onValid = latestConfig?.submitHandlers?.onValid;
96+
if (onValid && testInputValue?.trim()) onValid({ text: testInputValue.trim() });
97+
return undefined;
98+
}
99+
const maybeOnValid = arg as ((data: { text: string }) => void) | undefined;
100+
return (e: FormEvent) => {
101+
e.preventDefault();
102+
const onValid = maybeOnValid || latestConfig?.submitHandlers?.onValid;
103+
if (onValid && testInputValue?.trim()) onValid({ text: testInputValue.trim() });
104+
};
105+
}),
106+
formState: { errors: {} },
107+
watch: vi.fn((_name: string) => testInputValue)
108+
};
109+
}
110+
};
111+
});
95112

96113
function renderWithRouter(ui: ReactElement) {
97114
const router = createMemoryRouter([{ path: '/', element: ui }], { initialEntries: ['/'] });

0 commit comments

Comments
 (0)