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
113 changes: 1 addition & 112 deletions tests/ui/NumericBigNumberPadTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type * as NativeNavigation from '@react-navigation/native';

import React from 'react';

import findAncestorWithStyle, {getStyleProperty} from '../utils/findAncestorWithStyle';
import findAncestorWithStyle from '../utils/findAncestorWithStyle';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

jest.mock('@libs/DeviceCapabilities', () => ({
Expand Down Expand Up @@ -604,86 +604,6 @@ describe('NumericInput number pad', () => {
}
});

it('focuses the input and collapses the selection when the number pad gap area is clicked on web', async () => {
// Given an input with a selection, above the number pad
const inputRef = React.createRef<BaseTextInputRef>();
renderInputWithPad({value: '1234'}, inputRef);
await waitForBatchedUpdatesWithAct();

const input = screen.getByTestId(INPUT_TEST_ID);
fireEvent(input, 'selectionChange', {nativeEvent: {selection: {start: 0, end: 2}}});
await waitForBatchedUpdatesWithAct();

const inputElement = inputRef.current;
if (!inputElement) {
throw new Error('Numeric input ref was not assigned');
}
const focusSpy = jest.spyOn(inputElement, 'focus');

// When clicking the empty area between the keys, which bubbles up to the layout root
const preventDefault = jest.fn();
fireEvent(screen.getByTestId(PAD_TEST_ID), 'mouseDown', {nativeEvent: {target: document.createElement('div')}, preventDefault, isDefaultPrevented: () => false});
await waitForBatchedUpdatesWithAct();

// Then the event is prevented, the selection collapses, and the input is focused
expect(preventDefault).toHaveBeenCalledTimes(1);
expect(input.props.selection).toEqual({start: 2, end: 2});
expect(focusSpy).toHaveBeenCalledTimes(1);
focusSpy.mockRestore();
});

it('does not re-focus the input when the number pad gap area is clicked while input is already focused', async () => {
// Given an already focused input with a selection, above the number pad
const inputRef = React.createRef<BaseTextInputRef>();
renderInputWithPad({value: '1234'}, inputRef);
await waitForBatchedUpdatesWithAct();

const input = screen.getByTestId(INPUT_TEST_ID);
fireEvent(input, 'selectionChange', {nativeEvent: {selection: {start: 0, end: 2}}});
await waitForBatchedUpdatesWithAct();

const inputElement = inputRef.current;
if (!inputElement) {
throw new Error('Numeric input ref was not assigned');
}
const isFocusedSpy = jest.spyOn(inputElement, 'isFocused').mockReturnValue(true);
const focusSpy = jest.spyOn(inputElement, 'focus');

// When clicking the empty area between the keys while the input is already focused
const preventDefault = jest.fn();
fireEvent(screen.getByTestId(PAD_TEST_ID), 'mouseDown', {nativeEvent: {target: document.createElement('div')}, preventDefault, isDefaultPrevented: () => false});
await waitForBatchedUpdatesWithAct();

// Then focus is not called again, though selection is still collapsed
expect(preventDefault).toHaveBeenCalledTimes(1);
expect(input.props.selection).toEqual({start: 2, end: 2});
expect(focusSpy).not.toHaveBeenCalled();

isFocusedSpy.mockRestore();
focusSpy.mockRestore();
});

it('ignores mousedown events from keypad buttons to preserve selection on web', async () => {
// Given an input with a selection, above the number pad
renderInputWithPad({value: '1234'});
await waitForBatchedUpdatesWithAct();

const input = screen.getByTestId(INPUT_TEST_ID);
fireEvent(input, 'selectionChange', {nativeEvent: {selection: {start: 0, end: 2}}});
await waitForBatchedUpdatesWithAct();

// When a mousedown event bubbles up from a keypad button, which handles the press itself
const target = document.createElement('div');
target.setAttribute('role', 'button');
const preventDefault = jest.fn();
fireEvent(screen.getByTestId(PAD_TEST_ID), 'mouseDown', {nativeEvent: {target}, preventDefault, isDefaultPrevented: () => false});
await waitForBatchedUpdatesWithAct();

// Then preventDefault is not called and the selection remains unchanged
expect(preventDefault).not.toHaveBeenCalled();
expect(input.props.selection).toEqual({start: 0, end: 2});
});

it('fills the width of its column', async () => {
// Given the number pad rendered in the layout's pad column, which centers its content
renderInputWithPad();
Expand All @@ -697,35 +617,4 @@ describe('NumericInput number pad', () => {
expect(fullWidthView).not.toBe(screen.getByTestId(PAD_TEST_ID));
expect(screen.getByTestId(PAD_TEST_ID)).toContainElement(fullWidthView ?? null);
});

it('applies bottom safe-area padding to the two-column scroll view in landscape when footer is omitted', async () => {
// Given a phone in landscape mode and an input rendered without a footer
mockUseIsInLandscapeMode.mockReturnValue(true);
renderInputWithPad();
await waitForBatchedUpdatesWithAct();

// When inspecting the body scroll view content container
const bodyScrollView = screen.getByTestId(`${ROOT_TEST_ID}-body`);
const contentContainerStyle: unknown = bodyScrollView.props.contentContainerStyle;
const paddingBottom = getStyleProperty(contentContainerStyle, 'paddingBottom');

// Then it includes bottom safe-area padding directly, because there is no footer to provide it
expect(typeof paddingBottom).toBe('number');
expect(Number(paddingBottom)).toBeGreaterThan(0);
});

it('does not apply bottom safe-area padding to the two-column scroll view in landscape when footer handles it', async () => {
// Given a phone in landscape mode and an input rendered with a footer
mockUseIsInLandscapeMode.mockReturnValue(true);
renderInputWithPad({footer: <NumericInput.FlipButton />});
await waitForBatchedUpdatesWithAct();

// When inspecting the body scroll view content container
const bodyScrollView = screen.getByTestId(`${ROOT_TEST_ID}-body`);
const contentContainerStyle: unknown = bodyScrollView.props.contentContainerStyle;
const paddingBottom = getStyleProperty(contentContainerStyle, 'paddingBottom');

// Then bottom safe-area padding is not requested on the scroll view, because the footer handles it outside
expect(paddingBottom).toBeUndefined();
});
});
61 changes: 61 additions & 0 deletions tests/ui/NumericInputLayoutTabletTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {screen} from '@testing-library/react-native';

import type * as DeviceCapabilities from '@libs/DeviceCapabilities';

import type * as NativeNavigation from '@react-navigation/native';
import type DeviceInfoModule from 'react-native-device-info';

import {StyleSheet} from 'react-native';

import {BODY_TEST_ID, FOOTER_TEST_ID, ROOT_TEST_ID, renderLayout} from '../utils/NumericInputLayoutTestUtils';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

const TABLET_LANDSCAPE = {windowWidth: 1366, windowHeight: 1024};

const mockWindowDimensions = jest.fn(() => TABLET_LANDSCAPE);

jest.mock('@hooks/useWindowDimensions', () => () => mockWindowDimensions());

// The tablet check of the native landscape helper (isInLandscapeMode/index.native.ts) runs once when it is imported, so it lives in its own test file
jest.mock('react-native-device-info', () => {
const mockDeviceInfo = jest.requireActual<typeof DeviceInfoModule>('react-native-device-info/jest/react-native-device-info-mock');
return {
__esModule: true,
default: {...mockDeviceInfo, isTablet: () => true},
};
});

jest.mock('@libs/DeviceCapabilities', () => ({
...jest.requireActual<typeof DeviceCapabilities>('@libs/DeviceCapabilities'),
canUseTouchScreen: () => true,
}));

jest.mock('@react-navigation/native', () => ({
...jest.requireActual<typeof NativeNavigation>('@react-navigation/native'),
useIsFocused: jest.fn(() => true),
useNavigation: jest.fn(() => ({
navigate: jest.fn(),
addListener: jest.fn(() => jest.fn()),
})),
}));

describe('NumericInput layout on tablets', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('keeps a single scrollable column on a tablet in landscape', async () => {
// Given a tablet whose window is wider than it is tall, which only phones split into two columns
mockWindowDimensions.mockReturnValue(TABLET_LANDSCAPE);

// When the layout renders
renderLayout();
await waitForBatchedUpdatesWithAct();

// Then the root is the scroll view holding the footer, and the body stays a plain column
const root = screen.getByTestId(ROOT_TEST_ID);
expect(StyleSheet.flatten(root.props.contentContainerStyle)).toMatchObject({flexGrow: 1});
expect(root).toContainElement(screen.getByTestId(FOOTER_TEST_ID));
expect(screen.getByTestId(BODY_TEST_ID).props.contentContainerStyle).toBeUndefined();
});
});
Loading
Loading