Files
vibemake-pcb-studio/qa/unittests/common/jsx_runtime.test.tsx
T
likang 1fde96c05e
CI / typecheck + test + build (push) Has been cancelled
打通vibemake
2026-11-05 16:21:37 +08:00

85 lines
3.0 KiB
TypeScript

// @vitest-environment happy-dom
// SPDX-License-Identifier: GPL-3.0-or-later
import { cleanup, fireEvent, render } from '@testing-library/react';
import { useState } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { jsx, jsxs, jsxDEV } from '@ziroeda/common/jsx-runtime.js';
import { setUiLanguage } from '@ziroeda/common/i18n.js';
beforeEach(() => {
setUiLanguage('简体中文');
});
afterEach(() => {
cleanup();
vi.restoreAllMocks();
setUiLanguage('English');
});
describe('localized JSX child identity', () => {
it.each([
'jsxs',
'jsxDEV',
] as const)('%s preserves static siblings without requiring list keys', (runtime) => {
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
const props = {
children: [jsx('span', { children: 'Save' }), jsx('span', { children: 'Cancel' })],
};
const { container } = render(
runtime === 'jsxs' ? jsxs('header', props) : jsxDEV('nav', props, undefined, true),
);
expect(container.textContent).toBe('保存取消');
expect(errors).not.toHaveBeenCalled();
});
it('still warns when a dynamic list is missing keys', () => {
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
render(
jsx('ol', {
children: ['first', 'second'].map((name) => jsx('li', { children: name })),
}),
);
expect(
errors.mock.calls.some(([message]) =>
String(message).includes('Each child in a list should have a unique "key" prop'),
),
).toBe(true);
});
it('still validates dynamic lists nested among static siblings', () => {
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
render(
jsxs('ul', {
children: [
jsx('li', { children: 'fixed' }),
['first', 'second'].map((name) => jsx('li', { children: name })),
],
}),
);
expect(
errors.mock.calls.some(([message]) =>
String(message).includes('Each child in a list should have a unique "key" prop'),
),
).toBe(true);
});
it('keeps keyed row state attached to its identity after reordering', () => {
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
function Row({ code }: { code: string }): JSX.Element {
const [count, setCount] = useState(0);
return jsx('button', {
'aria-label': code,
onClick: () => setCount(count + 1),
children: `${code}: ${count}`,
});
}
const list = (codes: string[]) =>
jsx('section', { children: codes.map((code) => jsx(Row, { code }, code)) });
const view = render(list(['project-a', 'project-b']));
fireEvent.click(view.getByRole('button', { name: 'project-a' }));
view.rerender(list(['project-b', 'project-a']));
expect(view.getByRole('button', { name: 'project-a' }).textContent).toBe('project-a: 1');
expect(view.getByRole('button', { name: 'project-b' }).textContent).toBe('project-b: 0');
expect(errors).not.toHaveBeenCalled();
});
});