41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import { renderToStaticMarkup } from 'react-dom/server';
|
|
import { afterEach, expect, test, vi } from 'vitest';
|
|
import { FormErrors } from '@/components/procurement';
|
|
import { focusFormError } from '@/lib/form-error-focus';
|
|
|
|
afterEach(() => vi.unstubAllGlobals());
|
|
|
|
test('only mapped validation errors offer field links and all messages remain escaped', () => {
|
|
const html = renderToStaticMarkup(
|
|
<FormErrors
|
|
errors={{ quantity: '<Quantidade>', version: 'Actualize a versão' }}
|
|
targets={{ quantity: 'quantity-line-a' }}
|
|
/>,
|
|
);
|
|
expect(html).toContain('href="#quantity-line-a"');
|
|
expect(html).toContain('<Quantidade>');
|
|
expect(html).not.toContain('href="#version"');
|
|
expect(html).toContain('Actualize a versão');
|
|
});
|
|
|
|
test('field navigation opens collapsed ancestors before focusing and scrolling', () => {
|
|
const steps: string[] = [];
|
|
const details = {
|
|
tagName: 'DETAILS',
|
|
parentElement: null,
|
|
setAttribute: (name: string) => steps.push(name),
|
|
};
|
|
const target = {
|
|
parentElement: { tagName: 'DIV', parentElement: details },
|
|
focus: () => steps.push('focus'),
|
|
scrollIntoView: () => steps.push('scroll'),
|
|
};
|
|
vi.stubGlobal('document', {
|
|
getElementById: (id: string) => (id === 'field' ? target : null),
|
|
});
|
|
expect(focusFormError('missing')).toBe(false);
|
|
expect(steps).toEqual([]);
|
|
expect(focusFormError('field')).toBe(true);
|
|
expect(steps).toEqual(['open', 'focus', 'scroll']);
|
|
});
|