Skip to content

Commit ae1a012

Browse files
committed
test: cover the switch handle growing on press
Removing the 100ms delay was a user-visible fix with no test behind it, so the delay could have come back unnoticed. Asserting the size needs a testID on the view that carries it: `-handle` now names the view holding the handle's size and position, and the tinted fill inside it becomes `-handle-fill`.
1 parent f63bbd1 commit ae1a012

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/components/Switch/Switch.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,10 @@ const Switch: (props: OperableProps) => React.JSX.Element = ({
469469
]}
470470
/>
471471

472-
<Animated.View style={[styles.handle, anchorStyle, handleAnimatedStyle]}>
472+
<Animated.View
473+
testID={testID ? `${testID}-handle` : undefined}
474+
style={[styles.handle, anchorStyle, handleAnimatedStyle]}
475+
>
473476
{/* Disabled-only: opaque `surface` backdrop. The tinted fill above
474477
composites over it, reproducing the native math avoiding the PlatformColor alpha limitation. */}
475478
{isDisabled ? (
@@ -481,7 +484,7 @@ const Switch: (props: OperableProps) => React.JSX.Element = ({
481484
/>
482485
) : null}
483486
<Animated.View
484-
testID={testID ? `${testID}-handle` : undefined}
487+
testID={testID ? `${testID}-handle-fill` : undefined}
485488
style={[
486489
styles.handleFill,
487490
{ opacity: handleOpacity },

src/components/__tests__/Switch.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe('Switch focus state', () => {
168168
<Switch value onValueChange={jest.fn()} testID="switch" />
169169
);
170170

171-
expect(animatedStyle('switch-handle')).toMatchObject({
171+
expect(animatedStyle('switch-handle-fill')).toMatchObject({
172172
backgroundColor: defaultThemes.light.colors.primaryContainer,
173173
});
174174
});
@@ -241,6 +241,30 @@ describe('Switch operability', () => {
241241
});
242242
});
243243

244+
describe('Switch press feedback', () => {
245+
it('grows the handle to the pressed size and back on release', async () => {
246+
await render(<Switch value onValueChange={jest.fn()} testID="switch" />);
247+
248+
await fireEvent(screen.getByTestId('switch'), 'pressIn');
249+
await jest.runAllTimersAsync();
250+
251+
// MD3 grows the handle to 28dp while pressed, from a 24dp selected resting
252+
// size. This has to be immediate -- a delay swallows short taps.
253+
expect(animatedStyle('switch-handle')).toMatchObject({
254+
width: 28,
255+
height: 28,
256+
});
257+
258+
await fireEvent(screen.getByTestId('switch'), 'pressOut');
259+
await jest.runAllTimersAsync();
260+
261+
expect(animatedStyle('switch-handle')).toMatchObject({
262+
width: 24,
263+
height: 24,
264+
});
265+
});
266+
});
267+
244268
describe('Switch interaction', () => {
245269
it('toggles to true when off and pressed', async () => {
246270
const user = userEvent.setup();

0 commit comments

Comments
 (0)