Skip to content

Button: add a test that pins its Android ripple to the opaque label color #5367

Backlog

Description

@Doberjohn

What's missing

#5177 made TouchableRipple apply the MD3 pressed opacity (0.10) itself, so components now pass it an opaque content color. Button passes its label color as rippleColor, which is right, but no Button test pins it. IconButton, FAB and FAB.Menu got such tests in #5177. Until #5177, Button baked 0.10 into its own ripple color. Doing that again on top of the ripple's 0.10 would draw the press at about 0.01, and nothing would catch it today.

Proposed test

A describe block at the end of src/components/__tests__/Button.test.tsx. It checks what the ripple gets on Android with React Native 0.86 (nativeForegroundAndroid), so it tests the rendered result rather than an internal helper.

Imports to add: processColor from react-native, beforeEach from @jest/globals, and TouchableRipple from ../TouchableRipple/TouchableRipple.native.

// Button passes its opaque label color as the ripple color, and
// TouchableRipple applies the pressed opacity once, as the ripple's `alpha`.
describe('ripple on Android', () => {
  beforeEach(() => {
    jest.replaceProperty(Platform, 'OS', 'android');
    jest.replaceProperty(TouchableRipple, 'supported', true);
    jest.spyOn(Platform, 'Version', 'get').mockReturnValue(34);
    const constants = Platform.constants;
    jest.spyOn(Platform, 'constants', 'get').mockReturnValue({
      ...constants,
      reactNativeVersion: {
        major: 0,
        minor: 86,
        patch: 0,
        prerelease: undefined,
      },
    });
  });

  it.each([
    ['text', LightTheme.colors.primary],
    ['filled', LightTheme.colors.onPrimary],
  ] as const)(
    'draws the %s button ripple in its opaque label color at the pressed alpha',
    async (mode, labelColor) => {
      await render(
        <Button mode={mode} testID="button" onPress={() => {}}>
          Save
        </Button>
      );

      expect(screen.getByTestId('button')).toHaveProp(
        'nativeForegroundAndroid',
        expect.objectContaining({
          type: 'RippleAndroid',
          color: processColor(labelColor),
          alpha: stateOpacity.pressed,
        })
      );
    }
  );
});

It passes on main (c05552f) with lint clean, and both cases fail if Button passes its label color at 0.10 as rippleColor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions