Skip to content

refactor: remove stale tests #1392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2023
Merged
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
20 changes: 0 additions & 20 deletions src/__tests__/fireEvent.test.tsx
Original file line number Diff line number Diff line change
@@ -217,25 +217,6 @@ test('should not fire on disabled Pressable', () => {
});

test('should not fire on non-editable TextInput', () => {
const placeholder = 'Test placeholder';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';

const { getByPlaceholderText } = render(
<View>
<TextInput
editable={false}
placeholder={placeholder}
onChangeText={onChangeTextMock}
/>
</View>
);

fireEvent.changeText(getByPlaceholderText(placeholder), NEW_TEXT);
expect(onChangeTextMock).not.toHaveBeenCalled();
});

test('should not fire on non-editable host TextInput', () => {
const testID = 'my-text-input';
const onChangeTextMock = jest.fn();
const NEW_TEXT = 'New text';
@@ -245,7 +226,6 @@ test('should not fire on non-editable host TextInput', () => {
editable={false}
testID={testID}
onChangeText={onChangeTextMock}
placeholder="placeholder"
/>
);

21 changes: 7 additions & 14 deletions src/__tests__/react-native-api.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from 'react';
import { View, Text, TextInput } from 'react-native';
import { render } from '..';
import { getHostSelf } from '../helpers/component-tree';

/**
* Tests in this file are intended to give us an proactive warning that React Native behavior has
@@ -10,8 +9,6 @@ import { getHostSelf } from '../helpers/component-tree';

test('React Native API assumption: <View> renders single host element', () => {
const view = render(<View testID="test" />);
const hostView = view.getByTestId('test');
expect(getHostSelf(hostView)).toBe(hostView);

expect(view.toJSON()).toMatchInlineSnapshot(`
<View
@@ -22,9 +19,7 @@ test('React Native API assumption: <View> renders single host element', () => {

test('React Native API assumption: <Text> renders single host element', () => {
const view = render(<Text testID="test">Hello</Text>);
const compositeView = view.getByText('Hello');
const hostView = view.getByTestId('test');
expect(getHostSelf(compositeView)).toBe(hostView);
expect(view.getByText('Hello')).toBe(view.getByTestId('test'));

expect(view.toJSON()).toMatchInlineSnapshot(`
<Text
@@ -45,11 +40,9 @@ test('React Native API assumption: nested <Text> renders single host element', (
</Text>
</Text>
);
expect(getHostSelf(view.getByText(/Hello/))).toBe(view.getByTestId('test'));
expect(getHostSelf(view.getByText('Before'))).toBe(
view.getByTestId('before')
);
expect(getHostSelf(view.getByText('Deeply nested'))).toBe(
expect(view.getByText(/Hello/)).toBe(view.getByTestId('test'));
expect(view.getByText('Before')).toBe(view.getByTestId('before'));
expect(view.getByText('Deeply nested')).toBe(
view.getByTestId('deeplyNested')
);

@@ -85,9 +78,9 @@ test('React Native API assumption: <TextInput> renders single host element', ()
placeholder="Placeholder"
/>
);
const compositeView = view.getByPlaceholderText('Placeholder');
const hostView = view.getByTestId('test');
expect(getHostSelf(compositeView)).toBe(hostView);
expect(view.getByPlaceholderText('Placeholder')).toBe(
view.getByTestId('test')
);

expect(view.toJSON()).toMatchInlineSnapshot(`
<TextInput
8 changes: 0 additions & 8 deletions src/__tests__/within.test.tsx
Original file line number Diff line number Diff line change
@@ -94,11 +94,3 @@ test('within() exposes a11y queries', async () => {
test('getQueriesForElement is alias to within', () => {
expect(getQueriesForElement).toBe(within);
});

test('within allows searching for text within a composite component', () => {
const view = render(<Text testID="subject">Hello</Text>);
// view.getByTestId('subject') returns a host component, contrary to text queries returning a composite component
// we want to be sure that this doesn't interfere with the way text is searched
const hostTextQueries = within(view.getByTestId('subject'));
expect(hostTextQueries.getByText('Hello')).toBeTruthy();
});
7 changes: 3 additions & 4 deletions src/fireEvent.ts
Original file line number Diff line number Diff line change
@@ -12,11 +12,10 @@ const isTextInput = (element?: ReactTestInstance) => {
return false;
}

// We have to test if the element type is either the TextInput component
// (which would if it is a composite component) or the string
// TextInput (which would be true if it is a host component)
// We have to test if the element type is either the `TextInput` component
// (for composite component) or the string "TextInput" (for host component)
// All queries return host components but since fireEvent bubbles up
// it would trigger the parent prop without the composite component check
// it would trigger the parent prop without the composite component check.
return (
filterNodeByType(element, TextInput) ||
filterNodeByType(element, getHostComponentNames().textInput)
10 changes: 3 additions & 7 deletions src/helpers/__tests__/component-tree.test.tsx
Original file line number Diff line number Diff line change
@@ -142,17 +142,13 @@ describe('getHostSelf()', () => {
</View>
);

const compositeText = view.getByText('Text');
const compositeText = view.UNSAFE_getByType(Text);
const hostText = view.getByTestId('text');
expect(getHostSelf(compositeText)).toEqual(hostText);

const compositeTextInputByValue = view.getByDisplayValue('TextInputValue');
const compositeTextInputByPlaceholder = view.getByPlaceholderText(
'TextInputPlaceholder'
);
const compositeTextInput = view.UNSAFE_getByType(TextInput);
const hostTextInput = view.getByTestId('textInput');
expect(getHostSelf(compositeTextInputByValue)).toEqual(hostTextInput);
expect(getHostSelf(compositeTextInputByPlaceholder)).toEqual(hostTextInput);
expect(getHostSelf(compositeTextInput)).toEqual(hostTextInput);
});

it('throws on non-single host children elements for custom composite components', () => {