2023-09-11 12:53:31 +02:00
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
|
|
import { useRef } from 'react';
|
|
|
|
import { useOnBlur } from './useOnBlur';
|
|
|
|
|
|
|
|
function TestComponent(props: { onBlurHandler: () => void }) {
|
|
|
|
const divRef = useRef(null);
|
|
|
|
useOnBlur(divRef, props.onBlurHandler);
|
|
|
|
|
|
|
|
return (
|
2023-10-02 14:25:46 +02:00
|
|
|
<div data-testid='wrapper'>
|
|
|
|
{/* biome-ignore lint/a11y/noNoninteractiveTabindex: <explanation> */}
|
|
|
|
<div tabIndex={0} data-testid='inside' ref={divRef}>
|
2023-09-11 12:53:31 +02:00
|
|
|
Inside
|
|
|
|
</div>
|
2023-10-02 14:25:46 +02:00
|
|
|
{/* biome-ignore lint/a11y/noNoninteractiveTabindex: <explanation> */}
|
|
|
|
<div tabIndex={0} data-testid='outside'>
|
2023-09-11 12:53:31 +02:00
|
|
|
Outside
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should not call the callback when blurring within the same container', async () => {
|
|
|
|
let mockCallbackCallCount = 0;
|
|
|
|
const mockCallback = () => mockCallbackCallCount++;
|
|
|
|
|
|
|
|
render(<TestComponent onBlurHandler={mockCallback} />);
|
|
|
|
|
|
|
|
const insideDiv = screen.getByTestId('inside');
|
|
|
|
|
|
|
|
insideDiv.focus();
|
|
|
|
insideDiv.blur();
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(mockCallbackCallCount).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call the callback when blurring outside of the container', async () => {
|
|
|
|
let mockCallbackCallCount = 0;
|
|
|
|
const mockCallback = () => mockCallbackCallCount++;
|
|
|
|
|
|
|
|
render(<TestComponent onBlurHandler={mockCallback} />);
|
|
|
|
|
|
|
|
const insideDiv = screen.getByTestId('inside');
|
|
|
|
const outsideDiv = screen.getByTestId('outside');
|
|
|
|
|
|
|
|
insideDiv.focus();
|
|
|
|
outsideDiv.focus();
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(mockCallbackCallCount).toBe(1);
|
|
|
|
});
|
|
|
|
});
|