@@ -1,14 +1,73 @@
|
||||
import { App, router } from '@inertiajs/react';
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { afterEach, expect, test, vi } from 'vitest';
|
||||
import Legal from '../../resources/js/pages/supply/import-legal';
|
||||
import Legal, {
|
||||
revealInvalidLegalField,
|
||||
} from '../../resources/js/pages/supply/import-legal';
|
||||
|
||||
test('incomplete legal preparation reveals the first invalid field below the fixed header', () => {
|
||||
const callbacks: FrameRequestCallback[] = [];
|
||||
vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
|
||||
callbacks.push(callback);
|
||||
return callbacks.length;
|
||||
});
|
||||
const field = { focus: vi.fn(), scrollIntoView: vi.fn() };
|
||||
const form = {
|
||||
querySelector: vi.fn((selector: string) =>
|
||||
selector === 'input:invalid, select:invalid, textarea:invalid'
|
||||
? field
|
||||
: { tagName: 'FIELDSET' },
|
||||
),
|
||||
};
|
||||
try {
|
||||
revealInvalidLegalField({
|
||||
currentTarget: form,
|
||||
target: field,
|
||||
} as never);
|
||||
revealInvalidLegalField({ currentTarget: form, target: {} } as never);
|
||||
expect(callbacks).toHaveLength(1);
|
||||
expect(field.scrollIntoView).not.toHaveBeenCalled();
|
||||
callbacks[0](0);
|
||||
expect(field.focus).toHaveBeenCalledWith({ preventScroll: true });
|
||||
expect(field.scrollIntoView).toHaveBeenCalledWith({
|
||||
block: 'center',
|
||||
behavior: 'instant',
|
||||
});
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
test.each([true, false])(
|
||||
'legal shortcuts target present focusable sections without exposing management (%s)',
|
||||
(canManage) => {
|
||||
const html = renderLegal(canManage);
|
||||
const navigation = html.match(
|
||||
/<nav aria-label="Áreas da base legal de importação"[\s\S]*?<\/nav>/,
|
||||
)?.[0];
|
||||
expect(navigation).toBeDefined();
|
||||
const targets = [...navigation!.matchAll(/href="#([^"]+)"/g)].map(
|
||||
(match) => match[1],
|
||||
);
|
||||
for (const id of targets) {
|
||||
expect(html.split(`id="${id}"`)).toHaveLength(2);
|
||||
expect(html).toMatch(new RegExp(`id="${id}" tabindex="-1"`));
|
||||
}
|
||||
expect(targets.includes('legal-rule-editor')).toBe(canManage);
|
||||
expect(targets).not.toContain('legal-decision');
|
||||
expect(html).toContain('data-query-form="true"');
|
||||
if (canManage) expect(html).toContain('href="#legal-rule-editor"');
|
||||
},
|
||||
);
|
||||
|
||||
function renderLegal(
|
||||
canManage: boolean,
|
||||
withRule = true,
|
||||
withCharges = false,
|
||||
withReference = false,
|
||||
iceReference = false,
|
||||
vatReference = false,
|
||||
) {
|
||||
const data = {
|
||||
title: '<script>synthetic rule</script>',
|
||||
@@ -68,18 +127,46 @@ function renderLegal(
|
||||
? [
|
||||
{
|
||||
source_id: 8,
|
||||
code: '3506.91.20',
|
||||
description:
|
||||
'Silicone para selagem de painéis solares',
|
||||
rate_percent: '20',
|
||||
title: 'Taxa geral documental',
|
||||
official_reference: 'Lei 8/2025',
|
||||
code: iceReference
|
||||
? '8703.80.10'
|
||||
: '3506.91.20',
|
||||
description: iceReference
|
||||
? 'Automóveis unicamente eléctricos, bateria até 62 kWh'
|
||||
: 'Silicone para selagem de painéis solares',
|
||||
rate_percent: iceReference ? '5' : '20',
|
||||
rate_label: iceReference
|
||||
? 'ICE 5%'
|
||||
: undefined,
|
||||
valid_until: iceReference
|
||||
? '2027-12-31'
|
||||
: undefined,
|
||||
title: iceReference
|
||||
? 'ICE documental'
|
||||
: 'Taxa geral documental',
|
||||
official_reference: iceReference
|
||||
? 'Lei 7/2025, 2.º suplemento'
|
||||
: 'Lei 8/2025',
|
||||
official_url: null,
|
||||
citation: 'Página 2180-(4)',
|
||||
citation: iceReference
|
||||
? 'Página 2180-(22)'
|
||||
: 'Página 2180-(4)',
|
||||
valid_from: '2026-01-01',
|
||||
obligations:
|
||||
'Conferir o exemplar e o regime aplicável',
|
||||
charges: [],
|
||||
...(vatReference
|
||||
? {
|
||||
code: 'IVA_ART17',
|
||||
label: 'IVA — taxa normal; completar código e base',
|
||||
match_code: '',
|
||||
suggested_code: '',
|
||||
rate_percent: '16',
|
||||
rate_label:
|
||||
'16% — base por documentar',
|
||||
valid_from: '2023-01-01',
|
||||
valid_until: null,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
]
|
||||
: [],
|
||||
@@ -116,6 +203,24 @@ test('official reference preparation is explicit and only available to the custo
|
||||
expect(reader).not.toContain('Silicone para selagem');
|
||||
});
|
||||
|
||||
test('ICE reference labels remain distinct from general customs duty', () => {
|
||||
const html = renderLegal(true, false, false, true, true);
|
||||
expect(html).toContain('ICE 5%');
|
||||
expect(html).not.toContain('taxa geral 5%');
|
||||
expect(html).toContain('fonte, citação e período');
|
||||
expect(html).toContain('não é uma pauta consolidada');
|
||||
});
|
||||
|
||||
test('the general VAT template has a human label and leaves item classification to explicit preparation', () => {
|
||||
const post = vi.spyOn(router, 'post');
|
||||
const html = renderLegal(true, false, false, true, false, true);
|
||||
expect(html).toContain('IVA — taxa normal; completar código e base');
|
||||
expect(html).toContain('16% — base por documentar');
|
||||
expect(html).not.toContain('IVA_ART17 —');
|
||||
expect(html).not.toMatch(/<input[^>]*value="IVA_ART17"/);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('remembered legal preparation is preserved and disables replacement by official reference drafts', () => {
|
||||
vi.spyOn(router, 'restore').mockImplementation((key) =>
|
||||
key === 'legal-rule:1:1:1:data'
|
||||
|
||||
Reference in New Issue
Block a user