test(person): replace 7 setTimeout sleeps in AddRelationshipForm with vi.waitFor
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -75,17 +75,16 @@ describe('AddRelationshipForm', () => {
|
||||
expect(cancelBtn).toBeDefined();
|
||||
cancelBtn?.click();
|
||||
|
||||
// After cancel, the form fields should be gone
|
||||
await new Promise((r) => setTimeout(r, 50));
|
||||
expect(document.querySelector('select[name="relationType"]')).toBeNull();
|
||||
await vi.waitFor(() => {
|
||||
expect(document.querySelector('select[name="relationType"]')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('calls onSubmit with the form data when the form is submitted via callback', async () => {
|
||||
it('does not invoke onSubmit before user submission', async () => {
|
||||
const onSubmit = vi.fn().mockResolvedValue(undefined);
|
||||
render(AddRelationshipForm, { props: { personId: 'p-1', onSubmit } });
|
||||
|
||||
// We can't easily trigger the submit without a person typeahead,
|
||||
// but at least verify the prop is wired (no crash).
|
||||
// Without a person selected, the form cannot be submitted by the user.
|
||||
expect(onSubmit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -93,36 +92,35 @@ describe('AddRelationshipForm', () => {
|
||||
render(AddRelationshipForm, { props: { personId: 'p-1' } });
|
||||
|
||||
await page.getByRole('button', { name: /hinzufügen/i }).click();
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
|
||||
const form = document.querySelector('form[action="?/addRelationship"]');
|
||||
expect(form).not.toBeNull();
|
||||
await vi.waitFor(() => {
|
||||
expect(document.querySelector('form[action="?/addRelationship"]')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders the callback-based form when onSubmit is provided', async () => {
|
||||
render(AddRelationshipForm, { props: { personId: 'p-1', onSubmit: async () => {} } });
|
||||
|
||||
await page.getByRole('button', { name: /hinzufügen/i }).click();
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
|
||||
// callback form has no action attribute (just onsubmit handler)
|
||||
const enhancedForm = document.querySelector('form[action="?/addRelationship"]');
|
||||
expect(enhancedForm).toBeNull();
|
||||
const fallbackForm = document.querySelector('form');
|
||||
expect(fallbackForm).not.toBeNull();
|
||||
await vi.waitFor(() => {
|
||||
// callback form has no action attribute (just onsubmit handler)
|
||||
expect(document.querySelector('form[action="?/addRelationship"]')).toBeNull();
|
||||
expect(document.querySelector('form')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows the self-error when the related person id equals personId', async () => {
|
||||
render(AddRelationshipForm, { props: { personId: 'p-self' } });
|
||||
|
||||
await page.getByRole('button', { name: /hinzufügen/i }).click();
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
|
||||
// Set the relatedPersonId hidden input to match personId
|
||||
const relInput = document.querySelector('input[name="relatedPersonId"]') as HTMLInputElement;
|
||||
const relInput = (await vi.waitFor(() => {
|
||||
const el = document.querySelector('input[name="relatedPersonId"]') as HTMLInputElement;
|
||||
expect(el).not.toBeNull();
|
||||
return el;
|
||||
})) as HTMLInputElement;
|
||||
relInput.value = 'p-self';
|
||||
relInput.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
|
||||
await expect.element(page.getByText(/selbst|self/i)).toBeVisible();
|
||||
});
|
||||
@@ -131,13 +129,12 @@ describe('AddRelationshipForm', () => {
|
||||
render(AddRelationshipForm, { props: { personId: 'p-1' } });
|
||||
|
||||
await page.getByRole('button', { name: /hinzufügen/i }).click();
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
|
||||
// The "Hinzufügen" button changed from toggle button → submit button. Find the submit one.
|
||||
const submitBtn = Array.from(
|
||||
document.querySelectorAll('button[type="submit"]')
|
||||
)[0] as HTMLButtonElement;
|
||||
expect(submitBtn.disabled).toBe(true);
|
||||
await vi.waitFor(() => {
|
||||
const submitBtn = document.querySelector('button[type="submit"]') as HTMLButtonElement | null;
|
||||
expect(submitBtn).not.toBeNull();
|
||||
expect(submitBtn!.disabled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps submit disabled when there is a yearError', async () => {
|
||||
@@ -155,10 +152,9 @@ describe('AddRelationshipForm', () => {
|
||||
relInput.value = 'p-other';
|
||||
relInput.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
const submitBtn = Array.from(
|
||||
document.querySelectorAll('button[type="submit"]')
|
||||
)[0] as HTMLButtonElement;
|
||||
expect(submitBtn.disabled).toBe(true);
|
||||
await vi.waitFor(() => {
|
||||
const submitBtn = document.querySelector('button[type="submit"]') as HTMLButtonElement;
|
||||
expect(submitBtn.disabled).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user