Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Feb 8, 2025
1 parent b26f068 commit 4ac8edc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ForwardRef, isMemo } from 'react-is';
import useMemo from './hooks/useMemo';
import isFragment from './React/isFragment';

const ReactMajorVersion = Number(version.split('.')[0]);

export const fillRef = <T>(ref: React.Ref<T>, node: T) => {
if (typeof ref === 'function') {
ref(node);
Expand Down Expand Up @@ -43,10 +45,7 @@ export const supportRef = (nodeOrComponent: any): boolean => {
}

// React 19 no need `forwardRef` anymore. So just pass if is a React element.
if (
isReactElement(nodeOrComponent) &&
version.startsWith('19.')
) {
if (isReactElement(nodeOrComponent) && ReactMajorVersion >= 19) {
return true;
}

Expand Down
18 changes: 17 additions & 1 deletion tests/ref-19.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-eval */
import React from 'react';
import { getNodeRef, useComposeRef } from '../src/ref';
import { getNodeRef, useComposeRef, supportRef } from '../src/ref';
import { render } from '@testing-library/react';

jest.mock('react', () => {
Expand Down Expand Up @@ -76,4 +76,20 @@ describe('ref: React 19', () => {
expect(outerRef.current?.className).toBe('bamboo');
expect(container.querySelector('.test-output')?.textContent).toBe('bamboo');
});

it('supportRef with not provide ref', () => {
const Empty = () => <div />;

const Checker = ({ children }: { children: React.ReactElement }) => {
return <p>{String(supportRef(children))}</p>;
};

const { container } = render(
<Checker>
<Empty />
</Checker>,
);

expect(container.querySelector('p')?.textContent).toBe('true');
});
});

0 comments on commit 4ac8edc

Please sign in to comment.