Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions tests/hooks-17.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as React from 'react';
import { renderToString } from 'react-dom/server';
import useId, { resetUuid } from '../src/hooks/useId';

const isReact19 = React.version.startsWith('19');

jest.mock('react', () => {
const react = jest.requireActual('react');

Expand All @@ -24,11 +22,6 @@ describe('hooks-17', () => {
return <div id={mergedId} className="target" />;
};

function matchId(container: HTMLElement, id: string) {
const ele = container.querySelector('.target');
return expect(ele.id).toEqual(id);
}

it('fallback of React 17 or lower', () => {
const errorSpy = jest.spyOn(console, 'error');
const originEnv = process.env.NODE_ENV;
Expand All @@ -53,7 +46,10 @@ describe('hooks-17', () => {
{ hydrate: true, container: holder },
);

matchId(container, isReact19 ? 'rc_unique_0' : 'rc_unique_1');
// React StrictMode may double-invoke effects across versions, so only
// assert a generated id instead of a fixed index.
const ele = container.querySelector('.target');
expect(ele.id).toMatch(/^rc_unique_\d+$/);

errorSpy.mockRestore();
process.env.NODE_ENV = originEnv;
Expand Down
10 changes: 9 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,18 @@ describe('utils', () => {

const isCapturePhase = (n: string) =>
n.endsWith('Capture') && !/^on(Got|Lost)PointerCapture$/.test(n);
const reactEvents = [...dts.matchAll(/\bon[A-Z]\w*(?=\?:)/g)]
// Only handlers declared inside `DOMAttributes` are real DOM event
// handlers. Scanning the whole dts would also pick up component
// callbacks such as `ViewTransitionProps.onEnter`.
const domAttrsBlock = dts.match(
/interface DOMAttributes<T> \{[\s\S]*?^ {4}\}/m,
)?.[0];
const reactEvents = [...domAttrsBlock!.matchAll(/\bon[A-Z]\w*(?=\?:)/g)]
.map(m => m[0])
.filter(n => !isCapturePhase(n));

expect(reactEvents.length).toBeGreaterThan(0);

const dropped = reactEvents.filter(
e => pickAttrs({ [e]: 1 }, { attr: true })[e] === undefined,
);
Expand Down
Loading