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
4 changes: 3 additions & 1 deletion src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,12 @@ const Checkbox = ({
: null;

// Losing the handlers means the matching hover-out or press-out never
// arrives, so the state would otherwise linger.
// arrives, so the state would otherwise linger. The focus is lost too: the
// focused Pressable is replaced, and its blur never arrives either.
React.useEffect(() => {
if (isInteractive) return;

setFocused(false);
setHovered(false);
pressedSV.value = 0;
rippleHoldSV.value = 0;
Expand Down
80 changes: 79 additions & 1 deletion src/components/__tests__/Checkbox/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { type GestureResponderEvent, Platform, Pressable } from 'react-native';
import {
type GestureResponderEvent,
Platform,
Pressable,
StyleSheet,
} from 'react-native';

import { afterEach, describe, expect, it, jest } from '@jest/globals';

import { fireEvent, render, screen, userEvent } from '../../../test-utils';
import { tokens } from '../../../theme/tokens';
import Checkbox from '../../Checkbox';
import { CheckboxTokens } from '../../Checkbox/tokens';
import TouchableRipple from '../../TouchableRipple/TouchableRipple';

afterEach(() => {
Expand Down Expand Up @@ -376,6 +383,77 @@ describe('Checkbox Space key', () => {
);
});

describe('Checkbox focus ring', () => {
const { thickness, outerOffset } = tokens.md.sys.state.focusIndicator;
const ringSize =
CheckboxTokens.stateLayerSize + 2 * (outerOffset + thickness);

/* eslint-disable testing-library/no-node-access, no-restricted-syntax --
Checkbox draws its ring as an unnamed view beside the pressable, with no
role or testID to query it by, so the outer container's children have to
be read to count it. */
const ringCount = () =>
(screen.root?.children ?? []).filter((child) => {
if (typeof child === 'string') return false;
const { width, height, borderWidth } =
StyleSheet.flatten(child.props.style) ?? {};
return (
width === ringSize && height === ringSize && borderWidth === thickness
);
}).length;
/* eslint-enable testing-library/no-node-access, no-restricted-syntax */

// A display-only checkbox is not exposed as a checkbox, so it is found by
// testID.
const Box = ({ onPress }: { onPress?: () => void }) => (
<Checkbox
status="unchecked"
onPress={onPress}
aria-label="Notify me"
testID="checkbox"
/>
);

// On Android a TouchableRipple without a handler renders a plain View, so
// the focused Pressable is destroyed and its blur never reaches JS.
const renderOnAndroid = async (onPress?: () => void) => {
jest.replaceProperty(Platform, 'OS', 'android');
jest.replaceProperty(TouchableRipple, 'supported', true);
await render(<Box onPress={onPress} />);
};

it('draws one ring on keyboard focus', async () => {
await renderOnAndroid(() => {});
await fireEvent(screen.getByTestId('checkbox'), 'focus');

expect(ringCount()).toBe(1);
});

it('drops the ring when onPress is removed', async () => {
const onPress = () => {};
await renderOnAndroid(onPress);
const pressable = screen.getByTestId('checkbox');
await fireEvent(screen.getByTestId('checkbox'), 'focus');

await screen.rerender(<Box />);

// The focused Pressable was replaced, not just left without a handler.
expect(screen.getByTestId('checkbox')).not.toBe(pressable);
expect(ringCount()).toBe(0);
});

it('shows no ring when onPress comes back until it is focused again', async () => {
const onPress = () => {};
await renderOnAndroid(onPress);
await fireEvent(screen.getByTestId('checkbox'), 'focus');

await screen.rerender(<Box />);
await screen.rerender(<Box onPress={onPress} />);

expect(ringCount()).toBe(0);
});
});

describe('Checkbox touch target', () => {
it('meets the 48dp minimum', async () => {
await render(
Expand Down
Loading