diff --git a/README.md b/README.md
index c0e2aa50..ff6292d3 100644
--- a/README.md
+++ b/README.md
@@ -88,21 +88,29 @@ flow-typed install react-test-renderer
import { render, screen, fireEvent } from '@testing-library/react-native';
import { QuestionsBoard } from '../QuestionsBoard';
-test('form submits two answers', () => {
- const allQuestions = ['q1', 'q2'];
- const mockFn = jest.fn();
+// It is recommended to use userEvent with fake timers
+// Some events involve duration so your tests may take a long time to run.
+jest.useFakeTimers();
- render();
+test('form submits two answers', async () => {
+ const questions = ['q1', 'q2'];
+ const onSubmit = jest.fn();
+
+ const user = userEvent.setup();
+ render();
const answerInputs = screen.getAllByLabelText('answer input');
- fireEvent.changeText(answerInputs[0], 'a1');
- fireEvent.changeText(answerInputs[1], 'a2');
- fireEvent.press(screen.getByText('Submit'));
+ // simulates the user focusing on TextInput and typing text one char at a time
+ await user.type(answerInputs[0], 'a1');
+ await user.type(answerInputs[1], 'a2');
- expect(mockFn).toBeCalledWith({
- 1: { q: 'q1', a: 'a1' },
- 2: { q: 'q2', a: 'a2' },
+ // simulates the user pressing on any pressable element
+ await user.press(screen.getByRole('button', { name: 'Submit' }));
+
+ expect(onSubmit).toHaveBeenCalledWith({
+ '1': { q: 'q1', a: 'a1' },
+ '2': { q: 'q2', a: 'a2' },
});
});
```
diff --git a/src/__tests__/__snapshots__/render-debug.test.tsx.snap b/src/__tests__/__snapshots__/render-debug.test.tsx.snap
index 088df781..25106295 100644
--- a/src/__tests__/__snapshots__/render-debug.test.tsx.snap
+++ b/src/__tests__/__snapshots__/render-debug.test.tsx.snap
@@ -58,6 +58,7 @@ exports[`debug 1`] = `
onResponderTerminate={[Function onResponderTerminate]}
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
+ role="button"
>
Change freshness!
@@ -137,6 +138,7 @@ exports[`debug changing component: bananaFresh button message should now be "fre
onResponderTerminate={[Function onResponderTerminate]}
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
+ role="button"
>
Change freshness!
@@ -343,6 +345,7 @@ exports[`debug: another custom message 1`] = `
onResponderTerminate={[Function onResponderTerminate]}
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
+ role="button"
>
Change freshness!
@@ -394,7 +397,6 @@ exports[`debug: shallow 1`] = `
/>
Change freshness!
@@ -446,7 +448,6 @@ exports[`debug: shallow with message 1`] = `
/>
Change freshness!
@@ -526,6 +527,7 @@ exports[`debug: with message 1`] = `
onResponderTerminate={[Function onResponderTerminate]}
onResponderTerminationRequest={[Function onResponderTerminationRequest]}
onStartShouldSetResponder={[Function onStartShouldSetResponder]}
+ role="button"
>
Change freshness!
diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx
index b5f58ef7..379eecc4 100644
--- a/src/__tests__/act.test.tsx
+++ b/src/__tests__/act.test.tsx
@@ -1,8 +1,6 @@
import * as React from 'react';
import { Text } from 'react-native';
-import act from '../act';
-import render from '../render';
-import fireEvent from '../fire-event';
+import { act, fireEvent, render, screen } from '../';
type UseEffectProps = { callback(): void };
const UseEffect = ({ callback }: UseEffectProps) => {
@@ -26,15 +24,15 @@ test('render should trigger useEffect', () => {
test('update should trigger useEffect', () => {
const effectCallback = jest.fn();
- const { update } = render();
- update();
+ render();
+ screen.update();
expect(effectCallback).toHaveBeenCalledTimes(2);
});
test('fireEvent should trigger useState', () => {
- const { getByText } = render();
- const counter = getByText(/Total count/i);
+ render();
+ const counter = screen.getByText(/Total count/i);
expect(counter.props.children).toEqual('Total count: 0');
fireEvent.press(counter);
diff --git a/src/__tests__/fire-event-textInput.test.tsx b/src/__tests__/fire-event-textInput.test.tsx
index 6f017f3c..d7bd823a 100644
--- a/src/__tests__/fire-event-textInput.test.tsx
+++ b/src/__tests__/fire-event-textInput.test.tsx
@@ -1,6 +1,6 @@
import React from 'react';
import { Text, TextInput, TextInputProps } from 'react-native';
-import { render, fireEvent } from '..';
+import { render, fireEvent, screen } from '..';
function WrappedTextInput(props: TextInputProps) {
return ;
@@ -18,7 +18,7 @@ test('should fire only non-touch-related events on non-editable TextInput', () =
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
- const view = render(
+ render(
);
- const subject = view.getByTestId('subject');
+ const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
@@ -47,7 +47,7 @@ test('should fire only non-touch-related events on non-editable TextInput with n
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
- const view = render(
+ render(
);
- const subject = view.getByText('Nested Text');
+ const subject = screen.getByText('Nested Text');
fireEvent(subject, 'focus');
fireEvent(subject, 'onFocus');
fireEvent.changeText(subject, 'Text');
@@ -98,7 +98,7 @@ test('should fire only non-touch-related events on non-editable wrapped TextInpu
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
- const view = render(
+ render(
);
- const subject = view.getByTestId('subject');
+ const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
@@ -130,7 +130,7 @@ test('should fire only non-touch-related events on non-editable double wrapped T
const onSubmitEditing = jest.fn();
const onLayout = jest.fn();
- const view = render(
+ render(
);
- const subject = view.getByTestId('subject');
+ const subject = screen.getByTestId('subject');
fireEvent(subject, 'focus');
fireEvent.changeText(subject, 'Text');
fireEvent(subject, 'submitEditing', { nativeEvent: { text: 'Text' } });
diff --git a/src/__tests__/fire-event.test.tsx b/src/__tests__/fire-event.test.tsx
index fd71f998..fc0f6aa2 100644
--- a/src/__tests__/fire-event.test.tsx
+++ b/src/__tests__/fire-event.test.tsx
@@ -1,14 +1,14 @@
import * as React from 'react';
import {
- View,
- TouchableOpacity,
PanResponder,
Pressable,
- Text,
ScrollView,
+ Text,
TextInput,
+ TouchableOpacity,
+ View,
} from 'react-native';
-import { render, fireEvent } from '..';
+import { fireEvent, render, screen } from '..';
type OnPressComponentProps = {
onPress: () => void;
@@ -58,9 +58,9 @@ const CustomEventComponentWithCustomName = ({
describe('fireEvent', () => {
test('should invoke specified event', () => {
const onPressMock = jest.fn();
- const { getByText } = render();
+ render();
- fireEvent(getByText('Press me'), 'press');
+ fireEvent(screen.getByText('Press me'), 'press');
expect(onPressMock).toHaveBeenCalled();
});
@@ -68,19 +68,19 @@ describe('fireEvent', () => {
test('should invoke specified event on parent element', () => {
const onPressMock = jest.fn();
const text = 'New press text';
- const { getByText } = render();
+ render();
- fireEvent(getByText(text), 'press');
+ fireEvent(screen.getByText(text), 'press');
expect(onPressMock).toHaveBeenCalled();
});
test('should not fire if the press handler is not passed to children', () => {
const onPressMock = jest.fn();
- const { getByText } = render(
+ render(
// TODO: this functionality is buggy, i.e. it will fail if we wrap this component with a View.
);
- fireEvent(getByText('Without event'), 'press');
+ fireEvent(screen.getByText('Without event'), 'press');
expect(onPressMock).not.toHaveBeenCalled();
});
@@ -88,13 +88,13 @@ describe('fireEvent', () => {
const handlerMock = jest.fn();
const EVENT_DATA = 'event data';
- const { getByText } = render(
+ render(
);
- fireEvent(getByText('Custom event component'), 'customEvent', EVENT_DATA);
+ fireEvent(screen.getByText('Custom event component'), 'customEvent', EVENT_DATA);
expect(handlerMock).toHaveBeenCalledWith(EVENT_DATA);
});
@@ -109,9 +109,9 @@ test('fireEvent.press', () => {
pageY: 30,
},
};
- const { getByText } = render();
+ render();
- fireEvent.press(getByText(text), eventData);
+ fireEvent.press(screen.getByText(text), eventData);
expect(onPressMock).toHaveBeenCalledWith(eventData);
});
@@ -126,13 +126,13 @@ test('fireEvent.scroll', () => {
},
};
- const { getByText } = render(
+ render(
XD
);
- fireEvent.scroll(getByText('XD'), eventData);
+ fireEvent.scroll(screen.getByText('XD'), eventData);
expect(onScrollMock).toHaveBeenCalledWith(eventData);
});
@@ -141,13 +141,13 @@ test('fireEvent.changeText', () => {
const onChangeTextMock = jest.fn();
const CHANGE_TEXT = 'content';
- const { getByPlaceholderText } = render(
+ render(
);
- fireEvent.changeText(getByPlaceholderText('Customer placeholder'), CHANGE_TEXT);
+ fireEvent.changeText(screen.getByPlaceholderText('Customer placeholder'), CHANGE_TEXT);
expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
});
@@ -155,9 +155,9 @@ test('fireEvent.changeText', () => {
test('custom component with custom event name', () => {
const handlePress = jest.fn();
- const { getByText } = render();
+ render();
- fireEvent(getByText('Custom component'), 'handlePress');
+ fireEvent(screen.getByText('Custom component'), 'handlePress');
expect(handlePress).toHaveBeenCalled();
});
@@ -165,16 +165,16 @@ test('custom component with custom event name', () => {
test('event with multiple handler parameters', () => {
const handlePress = jest.fn();
- const { getByText } = render();
+ render();
- fireEvent(getByText('Custom component'), 'handlePress', 'param1', 'param2');
+ fireEvent(screen.getByText('Custom component'), 'handlePress', 'param1', 'param2');
expect(handlePress).toHaveBeenCalledWith('param1', 'param2');
});
test('should not fire on disabled TouchableOpacity', () => {
const handlePress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -188,7 +188,7 @@ test('should not fire on disabled TouchableOpacity', () => {
test('should not fire on disabled Pressable', () => {
const handlePress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -202,7 +202,7 @@ test('should not fire on disabled Pressable', () => {
test('should not fire inside View with pointerEvents="none"', () => {
const onPress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -217,7 +217,7 @@ test('should not fire inside View with pointerEvents="none"', () => {
test('should not fire inside View with pointerEvents="box-only"', () => {
const onPress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -232,7 +232,7 @@ test('should not fire inside View with pointerEvents="box-only"', () => {
test('should fire inside View with pointerEvents="box-none"', () => {
const onPress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -247,7 +247,7 @@ test('should fire inside View with pointerEvents="box-none"', () => {
test('should fire inside View with pointerEvents="auto"', () => {
const onPress = jest.fn();
- const screen = render(
+ render(
Trigger
@@ -262,7 +262,7 @@ test('should fire inside View with pointerEvents="auto"', () => {
test('should not fire deeply inside View with pointerEvents="box-only"', () => {
const onPress = jest.fn();
- const screen = render(
+ render(
@@ -279,9 +279,7 @@ test('should not fire deeply inside View with pointerEvents="box-only"', () => {
test('should fire non-pointer events inside View with pointerEvents="box-none"', () => {
const onTouchStart = jest.fn();
- const screen = render(
-
- );
+ render();
fireEvent(screen.getByTestId('view'), 'touchStart');
expect(onTouchStart).toHaveBeenCalled();
@@ -289,7 +287,7 @@ test('should fire non-pointer events inside View with pointerEvents="box-none"',
test('should fire non-touch events inside View with pointerEvents="box-none"', () => {
const onLayout = jest.fn();
- const screen = render();
+ render();
fireEvent(screen.getByTestId('view'), 'layout');
expect(onLayout).toHaveBeenCalled();
@@ -299,9 +297,7 @@ test('should fire non-touch events inside View with pointerEvents="box-none"', (
// the 'press' event on host View rendered by pressable.
test('should fire on Pressable with pointerEvents="box-only', () => {
const onPress = jest.fn();
- const screen = render(
-
- );
+ render();
fireEvent.press(screen.getByTestId('pressable'));
expect(onPress).toHaveBeenCalled();
@@ -310,7 +306,7 @@ test('should fire on Pressable with pointerEvents="box-only', () => {
test('should pass event up on disabled TouchableOpacity', () => {
const handleInnerPress = jest.fn();
const handleOuterPress = jest.fn();
- const screen = render(
+ render(
Inner Trigger
@@ -326,7 +322,7 @@ test('should pass event up on disabled TouchableOpacity', () => {
test('should pass event up on disabled Pressable', () => {
const handleInnerPress = jest.fn();
const handleOuterPress = jest.fn();
- const screen = render(
+ render(
Inner Trigger
@@ -353,7 +349,7 @@ const TestComponent = ({ onPress }: TestComponentProps) => {
test('is not fooled by non-native disabled prop', () => {
const handlePress = jest.fn();
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Trigger Test'));
expect(handlePress).toHaveBeenCalledTimes(1);
@@ -363,6 +359,7 @@ type TestChildTouchableComponentProps = {
onPress: () => void;
someProp: boolean;
};
+
function TestChildTouchableComponent({ onPress, someProp }: TestChildTouchableComponentProps) {
return (
@@ -376,7 +373,7 @@ function TestChildTouchableComponent({ onPress, someProp }: TestChildTouchableCo
test('is not fooled by non-responder wrapping host elements', () => {
const handlePress = jest.fn();
- const screen = render(
+ render(
@@ -387,6 +384,7 @@ test('is not fooled by non-responder wrapping host elements', () => {
});
type TestDraggableComponentProps = { onDrag: () => void };
+
function TestDraggableComponent({ onDrag }: TestDraggableComponentProps) {
const responderHandlers = PanResponder.create({
onMoveShouldSetPanResponder: (_evt, _gestureState) => true,
@@ -403,7 +401,7 @@ function TestDraggableComponent({ onDrag }: TestDraggableComponentProps) {
test('has only onMove', () => {
const handleDrag = jest.fn();
- const screen = render();
+ render();
fireEvent(screen.getByText('Trigger'), 'responderMove', {
touchHistory: { mostRecentTimeStamp: '2', touchBank: [] },
@@ -416,41 +414,33 @@ test('has only onMove', () => {
describe('native events', () => {
test('triggers onScrollBeginDrag', () => {
const onScrollBeginDragSpy = jest.fn();
- const { getByTestId } = render(
-
- );
+ render();
- fireEvent(getByTestId('test-id'), 'onScrollBeginDrag');
+ fireEvent(screen.getByTestId('test-id'), 'onScrollBeginDrag');
expect(onScrollBeginDragSpy).toHaveBeenCalled();
});
test('triggers onScrollEndDrag', () => {
const onScrollEndDragSpy = jest.fn();
- const { getByTestId } = render(
-
- );
+ render();
- fireEvent(getByTestId('test-id'), 'onScrollEndDrag');
+ fireEvent(screen.getByTestId('test-id'), 'onScrollEndDrag');
expect(onScrollEndDragSpy).toHaveBeenCalled();
});
test('triggers onMomentumScrollBegin', () => {
const onMomentumScrollBeginSpy = jest.fn();
- const { getByTestId } = render(
-
- );
+ render();
- fireEvent(getByTestId('test-id'), 'onMomentumScrollBegin');
+ fireEvent(screen.getByTestId('test-id'), 'onMomentumScrollBegin');
expect(onMomentumScrollBeginSpy).toHaveBeenCalled();
});
test('triggers onMomentumScrollEnd', () => {
const onMomentumScrollEndSpy = jest.fn();
- const { getByTestId } = render(
-
- );
+ render();
- fireEvent(getByTestId('test-id'), 'onMomentumScrollEnd');
+ fireEvent(screen.getByTestId('test-id'), 'onMomentumScrollEnd');
expect(onMomentumScrollEndSpy).toHaveBeenCalled();
});
});
diff --git a/src/__tests__/host-text-nesting.test.tsx b/src/__tests__/host-text-nesting.test.tsx
index c32b8d8e..f9a4c1c9 100644
--- a/src/__tests__/host-text-nesting.test.tsx
+++ b/src/__tests__/host-text-nesting.test.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Text, Pressable, View } from 'react-native';
-import { render, within } from '../pure';
+import { render, within, screen } from '../pure';
/**
* Our queries interact differently with composite and host elements, and some specific cases require us
@@ -12,18 +12,18 @@ import { render, within } from '../pure';
*/
describe('nested text handling', () => {
test('within same node', () => {
- const view = render(Hello);
- expect(within(view.getByTestId('subject')).getByText('Hello')).toBeTruthy();
+ render(Hello);
+ expect(within(screen.getByTestId('subject')).getByText('Hello')).toBeTruthy();
});
test('role with direct text children', () => {
- const view = render(About);
+ render(About);
- expect(view.getByRole('header', { name: 'About' })).toBeTruthy();
+ expect(screen.getByRole('header', { name: 'About' })).toBeTruthy();
});
test('nested text with child with role', () => {
- const view = render(
+ render(
About
@@ -31,11 +31,11 @@ describe('nested text handling', () => {
);
- expect(view.getByRole('header', { name: 'About' }).props.testID).toBe('child');
+ expect(screen.getByRole('header', { name: 'About' }).props.testID).toBe('child');
});
test('pressable within View, with text child', () => {
- const view = render(
+ render(
Save
@@ -43,11 +43,11 @@ describe('nested text handling', () => {
);
- expect(view.getByRole('button', { name: 'Save' }).props.testID).toBe('pressable');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('pressable');
});
test('pressable within View, with text child within view', () => {
- const view = render(
+ render(
@@ -57,21 +57,21 @@ describe('nested text handling', () => {
);
- expect(view.getByRole('button', { name: 'Save' }).props.testID).toBe('pressable');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('pressable');
});
test('Text within pressable', () => {
- const view = render(
+ render(
Save
);
- expect(view.getByText('Save').props.testID).toBe('text');
+ expect(screen.getByText('Save').props.testID).toBe('text');
});
test('Text within view within pressable', () => {
- const view = render(
+ render(
Save
@@ -79,6 +79,6 @@ describe('nested text handling', () => {
);
- expect(view.getByText('Save').props.testID).toBe('text');
+ expect(screen.getByText('Save').props.testID).toBe('text');
});
});
diff --git a/src/__tests__/questionsBoard.test.tsx b/src/__tests__/questionsBoard.test.tsx
index 7958e589..5776ebb4 100644
--- a/src/__tests__/questionsBoard.test.tsx
+++ b/src/__tests__/questionsBoard.test.tsx
@@ -1,11 +1,14 @@
import * as React from 'react';
-import { View, TouchableOpacity, Text, ScrollView, TextInput } from 'react-native';
-import { render, fireEvent } from '..';
+import { Pressable, ScrollView, Text, TextInput, View } from 'react-native';
+import { render, screen, userEvent } from '..';
type QuestionsBoardProps = {
questions: string[];
onSubmit: (obj: {}) => void;
};
+
+jest.useFakeTimers();
+
function QuestionsBoard({ questions, onSubmit }: QuestionsBoardProps) {
const [data, setData] = React.useState({});
@@ -28,28 +31,30 @@ function QuestionsBoard({ questions, onSubmit }: QuestionsBoardProps) {
);
})}
- onSubmit(data)}>
+ onSubmit(data)}>
Submit
-
+
);
}
-test('form submits two answers', () => {
- const allQuestions = ['q1', 'q2'];
- const mockFn = jest.fn();
+test('form submits two answers', async () => {
+ const questions = ['q1', 'q2'];
+ const onSubmit = jest.fn();
- const { getAllByLabelText, getByText } = render(
-
- );
+ const user = userEvent.setup();
+ render();
- const answerInputs = getAllByLabelText('answer input');
+ const answerInputs = screen.getAllByLabelText('answer input');
- fireEvent.changeText(answerInputs[0], 'a1');
- fireEvent.changeText(answerInputs[1], 'a2');
- fireEvent.press(getByText('Submit'));
+ // simulates the user focusing on TextInput and typing text one char at a time
+ await user.type(answerInputs[0], 'a1');
+ await user.type(answerInputs[1], 'a2');
- expect(mockFn).toHaveBeenCalledWith({
+ // simulates the user pressing on any pressable element
+ await user.press(screen.getByRole('button', { name: 'Submit' }));
+
+ expect(onSubmit).toHaveBeenCalledWith({
'1': { q: 'q1', a: 'a1' },
'2': { q: 'q2', a: 'a2' },
});
diff --git a/src/__tests__/react-native-api.test.tsx b/src/__tests__/react-native-api.test.tsx
index 6972b275..ab1bfcb7 100644
--- a/src/__tests__/react-native-api.test.tsx
+++ b/src/__tests__/react-native-api.test.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
-import { View, Text, TextInput, Switch, ScrollView, FlatList } from 'react-native';
-import { render } from '..';
+import { FlatList, ScrollView, Switch, Text, TextInput, View } from 'react-native';
+import { render, screen } from '..';
/**
* Tests in this file are intended to give us an proactive warning that React Native behavior has
@@ -8,9 +8,9 @@ import { render } from '..';
*/
test('React Native API assumption: renders single host element', () => {
- const view = render();
+ render();
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
@@ -18,9 +18,9 @@ test('React Native API assumption: renders single host element', () => {
});
test('React Native API assumption: renders single host element', () => {
- const view = render(Hello);
+ render(Hello);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
@@ -30,7 +30,7 @@ test('React Native API assumption: renders single host element', () => {
});
test('React Native API assumption: nested renders single host element', () => {
- const view = render(
+ render(
Before
Hello
@@ -40,7 +40,7 @@ test('React Native API assumption: nested renders single host element', (
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
@@ -64,7 +64,7 @@ test('React Native API assumption: nested renders single host element', (
});
test('React Native API assumption: renders single host element', () => {
- const view = render(
+ render(
renders single host element', ()
/>
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
renders single host element', ()
});
test('React Native API assumption: with nested Text renders single host element', () => {
- const view = render(
+ render(
Hello
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
with nested Text renders single h
});
test('React Native API assumption: renders single host element', () => {
- const view = render();
+ render();
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
renders single host element', () =>
});
test('React Native API assumption: aria-* props render on host View', () => {
- const view = render(
+ render(
{
/>
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
{
});
test('React Native API assumption: aria-* props render on host Text', () => {
- const view = render(
+ render(
{
/>
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
{
});
test('React Native API assumption: aria-* props render on host TextInput', () => {
- const view = render(
+ render(
/>
);
- expect(view.toJSON()).toMatchInlineSnapshot(`
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
});
test('ScrollView renders correctly', () => {
- const screen = render(
+ render(
@@ -288,7 +288,7 @@ test('ScrollView renders correctly', () => {
});
test('FlatList renders correctly', () => {
- const screen = render(
+ render(
{item}} />
);
diff --git a/src/__tests__/render-debug.test.tsx b/src/__tests__/render-debug.test.tsx
index a9f92778..988d491f 100644
--- a/src/__tests__/render-debug.test.tsx
+++ b/src/__tests__/render-debug.test.tsx
@@ -1,10 +1,8 @@
/* eslint-disable no-console */
import * as React from 'react';
-import { View, Text, TextInput, Pressable } from 'react-native';
+import { Pressable, Text, TextInput, View } from 'react-native';
import stripAnsi from 'strip-ansi';
-import { render, fireEvent, configure } from '..';
-
-type ConsoleLogMock = jest.Mock>;
+import { configure, fireEvent, render, screen } from '..';
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
@@ -30,14 +28,17 @@ afterEach(() => {
jest.restoreAllMocks();
});
-class MyButton extends React.Component {
- render() {
- return (
-
- {this.props.children}
-
- );
- }
+interface MyButtonProps {
+ onPress: () => void;
+ children: string;
+}
+
+function MyButton(props: MyButtonProps) {
+ return (
+
+ {props.children}
+
+ );
}
class Banana extends React.Component {
@@ -82,9 +83,7 @@ class Banana extends React.Component {
/>
-
- Change freshness!
-
+ Change freshness!
First Text
Second Text
{test}
@@ -94,50 +93,50 @@ class Banana extends React.Component {
}
test('debug', () => {
- const { debug } = render();
+ render();
- debug();
- debug('my custom message');
- debug.shallow();
- debug.shallow('my other custom message');
- debug({ message: 'another custom message' });
+ screen.debug();
+ screen.debug('my custom message');
+ screen.debug.shallow();
+ screen.debug.shallow('my other custom message');
+ screen.debug({ message: 'another custom message' });
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
expect(stripAnsi(mockCalls[1][0] + mockCalls[1][1])).toMatchSnapshot('with message');
expect(stripAnsi(mockCalls[2][0])).toMatchSnapshot('shallow');
expect(stripAnsi(mockCalls[3][0] + mockCalls[3][1])).toMatchSnapshot('shallow with message');
expect(stripAnsi(mockCalls[4][0] + mockCalls[4][1])).toMatchSnapshot('another custom message');
- const mockWarnCalls = (console.warn as any as ConsoleLogMock).mock.calls;
+ const mockWarnCalls = jest.mocked(console.warn).mock.calls;
expect(mockWarnCalls[0]).toEqual([
'Using debug("message") is deprecated and will be removed in future release, please use debug({ message; "message" }) instead.',
]);
});
test('debug changing component', () => {
- const { UNSAFE_getByProps, debug } = render();
- fireEvent.press(UNSAFE_getByProps({ type: 'primary' }));
+ render();
+ fireEvent.press(screen.getByRole('button', { name: 'Change freshness!' }));
- debug();
+ screen.debug();
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot(
'bananaFresh button message should now be "fresh"'
);
});
test('debug with only children prop', () => {
- const { debug } = render();
- debug({ mapProps: () => ({}) });
+ render();
+ screen.debug({ mapProps: () => ({}) });
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug with only prop whose value is bananaChef', () => {
- const { debug } = render();
- debug({
+ render();
+ screen.debug({
mapProps: (props) => {
const filterProps: Record = {};
Object.keys(props).forEach((key) => {
@@ -149,51 +148,51 @@ test('debug with only prop whose value is bananaChef', () => {
},
});
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug with only props from TextInput components', () => {
- const { debug } = render();
- debug({
+ render();
+ screen.debug({
mapProps: (props, node) => (node.type === 'TextInput' ? props : {}),
});
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug should use debugOptions from config when no option is specified', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
- const { debug } = render(
+ render(
hello
);
- debug();
+ screen.debug();
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('filtering out props through mapProps option should not modify component', () => {
- const { debug, getByTestId } = render();
- debug({ mapProps: () => ({}) });
+ render();
+ screen.debug({ mapProps: () => ({}) });
- expect(getByTestId('viewTestID')).toBeTruthy();
+ expect(screen.getByTestId('viewTestID')).toBeTruthy();
});
test('debug should use given options over config debugOptions', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
- const { debug } = render(
+ render(
hello
);
- debug({ mapProps: (props) => props });
+ screen.debug({ mapProps: (props) => props });
- const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
+ const mockCalls = jest.mocked(console.log).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
diff --git a/src/__tests__/render-string-validation.test.tsx b/src/__tests__/render-string-validation.test.tsx
index a0be4267..1ff6b2c2 100644
--- a/src/__tests__/render-string-validation.test.tsx
+++ b/src/__tests__/render-string-validation.test.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
import { View, Text, Pressable } from 'react-native';
-import { render, fireEvent } from '..';
+import { render, fireEvent, screen } from '..';
// eslint-disable-next-line no-console
const originalConsoleError = console.error;
@@ -34,11 +34,11 @@ test('should throw when rendering a string outside a text component', () => {
});
test('should throw an error when rerendering with text outside of Text component', () => {
- const { rerender } = render(, {
+ render(, {
unstable_validateStringsRenderedWithinText: true,
});
- expect(() => rerender(hello)).toThrow(
+ expect(() => screen.rerender(hello)).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "hello" string within a component.`
);
});
@@ -58,11 +58,11 @@ const InvalidTextAfterPress = () => {
};
test('should throw an error when strings are rendered outside Text', () => {
- const { getByText } = render(, {
+ render(, {
unstable_validateStringsRenderedWithinText: true,
});
- expect(() => fireEvent.press(getByText('Show text'))).toThrow(
+ expect(() => fireEvent.press(screen.getByText('Show text'))).toThrow(
`${VALIDATION_ERROR}. Detected attempt to render "text rendered outside text component" string within a component.`
);
});
diff --git a/src/__tests__/render.test.tsx b/src/__tests__/render.test.tsx
index e6ca0369..0c09b390 100644
--- a/src/__tests__/render.test.tsx
+++ b/src/__tests__/render.test.tsx
@@ -1,8 +1,8 @@
/* eslint-disable no-console */
import * as React from 'react';
-import { View, Text, TextInput, Pressable } from 'react-native';
+import { Pressable, Text, TextInput, View } from 'react-native';
import { getConfig, resetToDefaults } from '../config';
-import { render, screen, fireEvent, RenderAPI } from '..';
+import { fireEvent, render, RenderAPI, screen } from '..';
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
@@ -75,57 +75,57 @@ class Banana extends React.Component {
}
test('UNSAFE_getAllByType, UNSAFE_queryAllByType', () => {
- const { UNSAFE_getAllByType, UNSAFE_queryAllByType } = render();
- const [text, status, button] = UNSAFE_getAllByType(Text);
+ render();
+ const [text, status, button] = screen.UNSAFE_getAllByType(Text);
const InExistent = () => null;
expect(text.props.children).toBe('Is the banana fresh?');
expect(status.props.children).toBe('not fresh');
expect(button.props.children).toBe('Change freshness!');
- expect(() => UNSAFE_getAllByType(InExistent)).toThrow('No instances found');
+ expect(() => screen.UNSAFE_getAllByType(InExistent)).toThrow('No instances found');
- expect(UNSAFE_queryAllByType(Text)[1]).toBe(status);
- expect(UNSAFE_queryAllByType(InExistent)).toHaveLength(0);
+ expect(screen.UNSAFE_queryAllByType(Text)[1]).toBe(status);
+ expect(screen.UNSAFE_queryAllByType(InExistent)).toHaveLength(0);
});
test('UNSAFE_getByProps, UNSAFE_queryByProps', () => {
- const { UNSAFE_getByProps, UNSAFE_queryByProps } = render();
- const primaryType = UNSAFE_getByProps({ type: 'primary' });
+ render();
+ const primaryType = screen.UNSAFE_getByProps({ type: 'primary' });
expect(primaryType.props.children).toBe('Change freshness!');
- expect(() => UNSAFE_getByProps({ type: 'inexistent' })).toThrow('No instances found');
+ expect(() => screen.UNSAFE_getByProps({ type: 'inexistent' })).toThrow('No instances found');
- expect(UNSAFE_queryByProps({ type: 'primary' })).toBe(primaryType);
- expect(UNSAFE_queryByProps({ type: 'inexistent' })).toBeNull();
+ expect(screen.UNSAFE_queryByProps({ type: 'primary' })).toBe(primaryType);
+ expect(screen.UNSAFE_queryByProps({ type: 'inexistent' })).toBeNull();
});
test('UNSAFE_getAllByProp, UNSAFE_queryAllByProps', () => {
- const { UNSAFE_getAllByProps, UNSAFE_queryAllByProps } = render();
- const primaryTypes = UNSAFE_getAllByProps({ type: 'primary' });
+ render();
+ const primaryTypes = screen.UNSAFE_getAllByProps({ type: 'primary' });
expect(primaryTypes).toHaveLength(1);
- expect(() => UNSAFE_getAllByProps({ type: 'inexistent' })).toThrow('No instances found');
+ expect(() => screen.UNSAFE_getAllByProps({ type: 'inexistent' })).toThrow('No instances found');
- expect(UNSAFE_queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
- expect(UNSAFE_queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
+ expect(screen.UNSAFE_queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
+ expect(screen.UNSAFE_queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
});
test('update', () => {
const fn = jest.fn();
- const { getByText, update, rerender } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
+ fireEvent.press(screen.getByText('Change freshness!'));
- update();
- rerender();
+ screen.update();
+ screen.rerender();
expect(fn).toHaveBeenCalledTimes(3);
});
test('unmount', () => {
const fn = jest.fn();
- const { unmount } = render();
- unmount();
+ render();
+ screen.unmount();
expect(fn).toHaveBeenCalled();
});
@@ -136,16 +136,16 @@ test('unmount should handle cleanup functions', () => {
return null;
};
- const { unmount } = render();
+ render();
- unmount();
+ screen.unmount();
expect(cleanup).toHaveBeenCalledTimes(1);
});
test('toJSON renders host output', () => {
- const { toJSON } = render(press me);
- expect(toJSON()).toMatchSnapshot();
+ render(press me);
+ expect(screen.toJSON()).toMatchSnapshot();
});
test('renders options.wrapper around node', () => {
@@ -154,12 +154,12 @@ test('renders options.wrapper around node', () => {
{children}
);
- const { toJSON, getByTestId } = render(, {
+ render(, {
wrapper: WrapperComponent,
});
- expect(getByTestId('wrapper')).toBeTruthy();
- expect(toJSON()).toMatchInlineSnapshot(`
+ expect(screen.getByTestId('wrapper')).toBeTruthy();
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
@@ -176,14 +176,14 @@ test('renders options.wrapper around updated node', () => {
{children}
);
- const { toJSON, getByTestId, rerender } = render(, {
+ render(, {
wrapper: WrapperComponent,
});
- rerender();
+ screen.rerender();
- expect(getByTestId('wrapper')).toBeTruthy();
- expect(toJSON()).toMatchInlineSnapshot(`
+ expect(screen.getByTestId('wrapper')).toBeTruthy();
+ expect(screen.toJSON()).toMatchInlineSnapshot(`
@@ -197,29 +197,24 @@ test('renders options.wrapper around updated node', () => {
});
test('returns host root', () => {
- const { root } = render();
+ render();
- expect(root).toBeDefined();
- expect(root.type).toBe('View');
- expect(root.props.testID).toBe('inner');
+ expect(screen.root).toBeDefined();
+ expect(screen.root.type).toBe('View');
+ expect(screen.root.props.testID).toBe('inner');
});
test('returns composite UNSAFE_root', () => {
- const { UNSAFE_root } = render();
+ render();
- expect(UNSAFE_root).toBeDefined();
- expect(UNSAFE_root.type).toBe(View);
- expect(UNSAFE_root.props.testID).toBe('inner');
+ expect(screen.UNSAFE_root).toBeDefined();
+ expect(screen.UNSAFE_root.type).toBe(View);
+ expect(screen.UNSAFE_root.props.testID).toBe('inner');
});
test('container displays deprecation', () => {
- const view = render();
+ render();
- expect(() => (view as any).container).toThrowErrorMatchingInlineSnapshot(`
- "'container' property has been renamed to 'UNSAFE_root'.
-
- Consider using 'root' property which returns root host element."
- `);
expect(() => (screen as any).container).toThrowErrorMatchingInlineSnapshot(`
"'container' property has been renamed to 'UNSAFE_root'.
diff --git a/src/__tests__/wait-for-element-to-be-removed.test.tsx b/src/__tests__/wait-for-element-to-be-removed.test.tsx
index dd09f79a..09586a2a 100644
--- a/src/__tests__/wait-for-element-to-be-removed.test.tsx
+++ b/src/__tests__/wait-for-element-to-be-removed.test.tsx
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
-import { View, Text, TouchableOpacity } from 'react-native';
-import { render, fireEvent, waitForElementToBeRemoved } from '..';
+import { Text, TouchableOpacity, View } from 'react-native';
+import { fireEvent, render, screen, waitForElementToBeRemoved } from '..';
const TestSetup = ({ shouldUseDelay = true }) => {
const [isAdded, setIsAdded] = useState(true);
@@ -29,7 +29,7 @@ afterEach(() => {
});
test('waits when using getBy query', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
const element = screen.getByText('Observed Element');
@@ -41,7 +41,7 @@ test('waits when using getBy query', async () => {
});
test('waits when using getAllBy query', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
const elements = screen.getAllByText('Observed Element');
@@ -53,7 +53,7 @@ test('waits when using getAllBy query', async () => {
});
test('waits when using queryBy query', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
const element = screen.getByText('Observed Element');
@@ -65,7 +65,7 @@ test('waits when using queryBy query', async () => {
});
test('waits when using queryAllBy query', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
const elements = screen.getAllByText('Observed Element');
@@ -77,7 +77,7 @@ test('waits when using queryAllBy query', async () => {
});
test('checks if elements exist at start', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
expect(screen.queryByText('Observed Element')).toBeNull();
@@ -90,7 +90,7 @@ test('checks if elements exist at start', async () => {
});
test('waits until timeout', async () => {
- const screen = render();
+ render();
fireEvent.press(screen.getByText('Remove Element'));
expect(screen.getByText('Observed Element')).toBeTruthy();
diff --git a/src/__tests__/wait-for.test.tsx b/src/__tests__/wait-for.test.tsx
index c10c317b..a42b991d 100644
--- a/src/__tests__/wait-for.test.tsx
+++ b/src/__tests__/wait-for.test.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Text, TouchableOpacity, View, Pressable } from 'react-native';
-import { fireEvent, render, waitFor, configure } from '..';
+import { fireEvent, render, waitFor, configure, screen } from '..';
class Banana extends React.Component {
changeFresh = () => {
@@ -37,51 +37,51 @@ afterEach(() => {
});
test('waits for element until it stops throwing', async () => {
- const { getByText, queryByText } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
+ fireEvent.press(screen.getByText('Change freshness!'));
- expect(queryByText('Fresh')).toBeNull();
+ expect(screen.queryByText('Fresh')).toBeNull();
- const freshBananaText = await waitFor(() => getByText('Fresh'));
+ const freshBananaText = await waitFor(() => screen.getByText('Fresh'));
expect(freshBananaText.props.children).toBe('Fresh');
});
test('waits for element until timeout is met', async () => {
- const { getByText } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
+ fireEvent.press(screen.getByText('Change freshness!'));
- await expect(waitFor(() => getByText('Fresh'), { timeout: 100 })).rejects.toThrow();
+ await expect(waitFor(() => screen.getByText('Fresh'), { timeout: 100 })).rejects.toThrow();
// Async action ends after 300ms and we only waited 100ms, so we need to wait
// for the remaining async actions to finish
- await waitFor(() => getByText('Fresh'));
+ await waitFor(() => screen.getByText('Fresh'));
});
test('waitFor defaults to asyncWaitTimeout config option', async () => {
configure({ asyncUtilTimeout: 100 });
- const { getByText } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
- await expect(waitFor(() => getByText('Fresh'))).rejects.toThrow();
+ fireEvent.press(screen.getByText('Change freshness!'));
+ await expect(waitFor(() => screen.getByText('Fresh'))).rejects.toThrow();
// Async action ends after 300ms and we only waited 100ms, so we need to wait
// for the remaining async actions to finish
- await waitFor(() => getByText('Fresh'), { timeout: 1000 });
+ await waitFor(() => screen.getByText('Fresh'), { timeout: 1000 });
});
test('waitFor timeout option takes precendence over `asyncWaitTimeout` config option', async () => {
configure({ asyncUtilTimeout: 2000 });
- const { getByText } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
- await expect(waitFor(() => getByText('Fresh'), { timeout: 100 })).rejects.toThrow();
+ fireEvent.press(screen.getByText('Change freshness!'));
+ await expect(waitFor(() => screen.getByText('Fresh'), { timeout: 100 })).rejects.toThrow();
// Async action ends after 300ms and we only waited 100ms, so we need to wait
// for the remaining async actions to finish
- await waitFor(() => getByText('Fresh'));
+ await waitFor(() => screen.getByText('Fresh'));
});
test('waits for element with custom interval', async () => {
@@ -124,9 +124,9 @@ const Comp = ({ onPress }: { onPress: () => void }) => {
test('waits for async event with fireEvent', async () => {
const spy = jest.fn();
- const { getByText } = render();
+ render();
- fireEvent.press(getByText('Trigger'));
+ fireEvent.press(screen.getByText('Trigger'));
await waitFor(() => {
expect(spy).toHaveBeenCalled();
@@ -137,13 +137,13 @@ test.each([false, true])(
'waits for element until it stops throwing using fake timers (legacyFakeTimers = %s)',
async (legacyFakeTimers) => {
jest.useFakeTimers({ legacyFakeTimers });
- const { getByText, queryByText } = render();
+ render();
- fireEvent.press(getByText('Change freshness!'));
- expect(queryByText('Fresh')).toBeNull();
+ fireEvent.press(screen.getByText('Change freshness!'));
+ expect(screen.queryByText('Fresh')).toBeNull();
jest.advanceTimersByTime(300);
- const freshBananaText = await waitFor(() => getByText('Fresh'));
+ const freshBananaText = await waitFor(() => screen.getByText('Fresh'));
expect(freshBananaText.props.children).toBe('Fresh');
}
@@ -293,18 +293,18 @@ test.each([
}
const onPress = jest.fn();
- const view = render();
+ render();
// Required: this `waitFor` will succeed on first check, because the "root" view is there
// since the initial mount.
- await waitFor(() => view.getByTestId('root'));
+ await waitFor(() => screen.getByTestId('root'));
// This `waitFor` will also succeed on first check, because the promise that sets the
// `color` state to "red" resolves right after the previous `await waitFor` statement.
- await waitFor(() => view.getByText('red'));
+ await waitFor(() => screen.getByText('red'));
// Check that the `onPress` callback is called with the already-updated value of `syncedColor`.
- fireEvent.press(view.getByText('Trigger'));
+ fireEvent.press(screen.getByText('Trigger'));
expect(onPress).toHaveBeenCalledWith('red');
}
);
diff --git a/src/helpers/__tests__/accessiblity.test.tsx b/src/helpers/__tests__/accessiblity.test.tsx
index 502d6e3b..a2efc577 100644
--- a/src/helpers/__tests__/accessiblity.test.tsx
+++ b/src/helpers/__tests__/accessiblity.test.tsx
@@ -1,6 +1,6 @@
import React from 'react';
import { View, Text, TextInput, Pressable, Switch, TouchableOpacity } from 'react-native';
-import { render, isHiddenFromAccessibility, isInaccessible } from '../..';
+import { render, isHiddenFromAccessibility, isInaccessible, screen } from '../..';
import { isAccessibilityElement } from '../accessiblity';
describe('isHiddenFromAccessibility', () => {
@@ -35,10 +35,10 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects elements with aria-hidden prop', () => {
- const view = render();
+ render();
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -46,14 +46,14 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects nested elements with aria-hidden prop', () => {
- const view = render(
+ render(
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -61,10 +61,10 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects elements with accessibilityElementsHidden prop', () => {
- const view = render();
+ render();
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -72,14 +72,14 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects nested elements with accessibilityElementsHidden prop', () => {
- const view = render(
+ render(
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -87,7 +87,7 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects deeply nested elements with accessibilityElementsHidden prop', () => {
- const view = render(
+ render(
@@ -98,7 +98,7 @@ describe('isHiddenFromAccessibility', () => {
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -106,10 +106,10 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects elements with importantForAccessibility="no-hide-descendants" prop', () => {
- const view = render();
+ render();
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -117,14 +117,14 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects nested elements with importantForAccessibility="no-hide-descendants" prop', () => {
- const view = render(
+ render(
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -132,10 +132,10 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects elements with display=none', () => {
- const view = render();
+ render();
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -143,14 +143,14 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects nested elements with display=none', () => {
- const view = render(
+ render(
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -158,7 +158,7 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects deeply nested elements with display=none', () => {
- const view = render(
+ render(
@@ -169,7 +169,7 @@ describe('isHiddenFromAccessibility', () => {
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -177,7 +177,7 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects elements with display=none with complex style', () => {
- const view = render(
+ render(
{
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -193,10 +193,10 @@ describe('isHiddenFromAccessibility', () => {
});
test('is not trigged by opacity = 0', () => {
- const view = render();
+ render();
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -204,7 +204,7 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects siblings of element with accessibilityViewIsModal prop', () => {
- const view = render(
+ render(
@@ -212,7 +212,7 @@ describe('isHiddenFromAccessibility', () => {
);
expect(
isHiddenFromAccessibility(
- view.getByTestId('subject', {
+ screen.getByTestId('subject', {
includeHiddenElements: true,
})
)
@@ -220,7 +220,7 @@ describe('isHiddenFromAccessibility', () => {
});
test('detects deeply nested siblings of element with accessibilityViewIsModal prop', () => {
- const view = render(
+ render(
@@ -231,38 +231,38 @@ describe('isHiddenFromAccessibility', () => {
);
expect(
- isHiddenFromAccessibility(view.getByTestId('subject', { includeHiddenElements: true }))
+ isHiddenFromAccessibility(screen.getByTestId('subject', { includeHiddenElements: true }))
).toBe(true);
});
test('detects siblings of element with "aria-modal" prop', () => {
- const view = render(
+ render(
);
expect(
- isHiddenFromAccessibility(view.getByTestId('subject', { includeHiddenElements: true }))
+ isHiddenFromAccessibility(screen.getByTestId('subject', { includeHiddenElements: true }))
).toBe(true);
});
test('is not triggered for element with accessibilityViewIsModal prop', () => {
- const view = render();
- expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
+ render();
+ expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
});
test('is not triggered for child of element with accessibilityViewIsModal prop', () => {
- const view = render(
+ render(
);
- expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
+ expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
});
test('is not triggered for descendent of element with accessibilityViewIsModal prop', () => {
- const view = render(
+ render(
@@ -271,7 +271,7 @@ describe('isHiddenFromAccessibility', () => {
);
- expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
+ expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
});
test('has isInaccessible alias', () => {
@@ -280,39 +280,39 @@ describe('isHiddenFromAccessibility', () => {
});
test('is not triggered for element with "aria-modal" prop', () => {
- const view = render();
- expect(isHiddenFromAccessibility(view.getByTestId('subject'))).toBe(false);
+ render();
+ expect(isHiddenFromAccessibility(screen.getByTestId('subject'))).toBe(false);
});
describe('isAccessibilityElement', () => {
test('matches View component properly', () => {
- const { getByTestId } = render(
+ render(
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeFalsy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('matches TextInput component properly', () => {
- const { getByTestId } = render(
+ render(
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('matches Text component properly', () => {
- const { getByTestId } = render(
+ render(
Default
@@ -323,48 +323,48 @@ describe('isAccessibilityElement', () => {
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('matches Switch component properly', () => {
- const { getByTestId } = render(
+ render(
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('matches Pressable component properly', () => {
- const { getByTestId } = render(
+ render(
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('matches TouchableOpacity component properly', () => {
- const { getByTestId } = render(
+ render(
);
- expect(isAccessibilityElement(getByTestId('default'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('true'))).toBeTruthy();
- expect(isAccessibilityElement(getByTestId('false'))).toBeFalsy();
+ expect(isAccessibilityElement(screen.getByTestId('default'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('true'))).toBeTruthy();
+ expect(isAccessibilityElement(screen.getByTestId('false'))).toBeFalsy();
});
test('returns false when given null', () => {
diff --git a/src/helpers/__tests__/component-tree.test.tsx b/src/helpers/__tests__/component-tree.test.tsx
index 32d2290f..c749ea6f 100644
--- a/src/helpers/__tests__/component-tree.test.tsx
+++ b/src/helpers/__tests__/component-tree.test.tsx
@@ -1,6 +1,6 @@
import React from 'react';
-import { View, Text, TextInput } from 'react-native';
-import { render } from '../..';
+import { Text, TextInput, View } from 'react-native';
+import { render, screen } from '../..';
import {
getHostChildren,
getHostParent,
@@ -25,7 +25,7 @@ function MultipleHostChildren() {
describe('getHostParent()', () => {
it('returns host parent for host component', () => {
- const view = render(
+ render(
@@ -34,11 +34,11 @@ describe('getHostParent()', () => {
);
- const hostParent = getHostParent(view.getByTestId('subject'));
- expect(hostParent).toBe(view.getByTestId('parent'));
+ const hostParent = getHostParent(screen.getByTestId('subject'));
+ expect(hostParent).toBe(screen.getByTestId('parent'));
const hostGrandparent = getHostParent(hostParent);
- expect(hostGrandparent).toBe(view.getByTestId('grandparent'));
+ expect(hostGrandparent).toBe(screen.getByTestId('grandparent'));
expect(getHostParent(hostGrandparent)).toBe(null);
});
@@ -48,22 +48,22 @@ describe('getHostParent()', () => {
});
it('returns host parent for composite component', () => {
- const view = render(
+ render(
);
- const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren);
+ const compositeComponent = screen.UNSAFE_getByType(MultipleHostChildren);
const hostParent = getHostParent(compositeComponent);
- expect(hostParent).toBe(view.getByTestId('parent'));
+ expect(hostParent).toBe(screen.getByTestId('parent'));
});
});
describe('getHostChildren()', () => {
it('returns host children for host component', () => {
- const view = render(
+ render(
@@ -72,21 +72,21 @@ describe('getHostChildren()', () => {
);
- const hostSubject = view.getByTestId('subject');
+ const hostSubject = screen.getByTestId('subject');
expect(getHostChildren(hostSubject)).toEqual([]);
- const hostSibling = view.getByTestId('sibling');
+ const hostSibling = screen.getByTestId('sibling');
expect(getHostChildren(hostSibling)).toEqual([]);
- const hostParent = view.getByTestId('parent');
+ const hostParent = screen.getByTestId('parent');
expect(getHostChildren(hostParent)).toEqual([hostSubject, hostSibling]);
- const hostGrandparent = view.getByTestId('grandparent');
+ const hostGrandparent = screen.getByTestId('grandparent');
expect(getHostChildren(hostGrandparent)).toEqual([hostParent]);
});
it('returns host children for composite component', () => {
- const view = render(
+ render(
@@ -94,19 +94,19 @@ describe('getHostChildren()', () => {
);
- expect(getHostChildren(view.getByTestId('parent'))).toEqual([
- view.getByTestId('child1'),
- view.getByTestId('child2'),
- view.getByTestId('child3'),
- view.getByTestId('subject'),
- view.getByTestId('sibling'),
+ expect(getHostChildren(screen.getByTestId('parent'))).toEqual([
+ screen.getByTestId('child1'),
+ screen.getByTestId('child2'),
+ screen.getByTestId('child3'),
+ screen.getByTestId('subject'),
+ screen.getByTestId('sibling'),
]);
});
});
describe('getHostSelves()', () => {
it('returns passed element for host components', () => {
- const view = render(
+ render(
@@ -115,21 +115,21 @@ describe('getHostSelves()', () => {
);
- const hostSubject = view.getByTestId('subject');
+ const hostSubject = screen.getByTestId('subject');
expect(getHostSelves(hostSubject)).toEqual([hostSubject]);
- const hostSibling = view.getByTestId('sibling');
+ const hostSibling = screen.getByTestId('sibling');
expect(getHostSelves(hostSibling)).toEqual([hostSibling]);
- const hostParent = view.getByTestId('parent');
+ const hostParent = screen.getByTestId('parent');
expect(getHostSelves(hostParent)).toEqual([hostParent]);
- const hostGrandparent = view.getByTestId('grandparent');
+ const hostGrandparent = screen.getByTestId('grandparent');
expect(getHostSelves(hostGrandparent)).toEqual([hostGrandparent]);
});
test('returns single host element for React Native composite components', () => {
- const view = render(
+ render(
Text
{
);
- const compositeText = view.getByText('Text');
- const hostText = view.getByTestId('text');
+ const compositeText = screen.getByText('Text');
+ const hostText = screen.getByTestId('text');
expect(getHostSelves(compositeText)).toEqual([hostText]);
- const compositeTextInputByValue = view.getByDisplayValue('TextInputValue');
- const compositeTextInputByPlaceholder = view.getByPlaceholderText('TextInputPlaceholder');
+ const compositeTextInputByValue = screen.getByDisplayValue('TextInputValue');
+ const compositeTextInputByPlaceholder = screen.getByPlaceholderText('TextInputPlaceholder');
- const hostTextInput = view.getByTestId('textInput');
+ const hostTextInput = screen.getByTestId('textInput');
expect(getHostSelves(compositeTextInputByValue)).toEqual([hostTextInput]);
expect(getHostSelves(compositeTextInputByPlaceholder)).toEqual([hostTextInput]);
});
test('returns host children for custom composite components', () => {
- const view = render(
+ render(
@@ -161,20 +161,20 @@ describe('getHostSelves()', () => {
);
- const zeroCompositeComponent = view.UNSAFE_getByType(ZeroHostChildren);
+ const zeroCompositeComponent = screen.UNSAFE_getByType(ZeroHostChildren);
expect(getHostSelves(zeroCompositeComponent)).toEqual([]);
- const multipleCompositeComponent = view.UNSAFE_getByType(MultipleHostChildren);
- const hostChild1 = view.getByTestId('child1');
- const hostChild2 = view.getByTestId('child2');
- const hostChild3 = view.getByTestId('child3');
+ const multipleCompositeComponent = screen.UNSAFE_getByType(MultipleHostChildren);
+ const hostChild1 = screen.getByTestId('child1');
+ const hostChild2 = screen.getByTestId('child2');
+ const hostChild3 = screen.getByTestId('child3');
expect(getHostSelves(multipleCompositeComponent)).toEqual([hostChild1, hostChild2, hostChild3]);
});
});
describe('getHostSiblings()', () => {
it('returns host siblings for host component', () => {
- const view = render(
+ render(
@@ -185,18 +185,18 @@ describe('getHostSiblings()', () => {
);
- const hostSiblings = getHostSiblings(view.getByTestId('subject'));
+ const hostSiblings = getHostSiblings(screen.getByTestId('subject'));
expect(hostSiblings).toEqual([
- view.getByTestId('siblingBefore'),
- view.getByTestId('siblingAfter'),
- view.getByTestId('child1'),
- view.getByTestId('child2'),
- view.getByTestId('child3'),
+ screen.getByTestId('siblingBefore'),
+ screen.getByTestId('siblingAfter'),
+ screen.getByTestId('child1'),
+ screen.getByTestId('child2'),
+ screen.getByTestId('child3'),
]);
});
it('returns host siblings for composite component', () => {
- const view = render(
+ render(
@@ -207,19 +207,19 @@ describe('getHostSiblings()', () => {
);
- const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren);
+ const compositeComponent = screen.UNSAFE_getByType(MultipleHostChildren);
const hostSiblings = getHostSiblings(compositeComponent);
expect(hostSiblings).toEqual([
- view.getByTestId('siblingBefore'),
- view.getByTestId('subject'),
- view.getByTestId('siblingAfter'),
+ screen.getByTestId('siblingBefore'),
+ screen.getByTestId('subject'),
+ screen.getByTestId('siblingAfter'),
]);
});
});
describe('getUnsafeRootElement()', () => {
it('returns UNSAFE_root for mounted view', () => {
- const screen = render(
+ render(
diff --git a/src/helpers/__tests__/text-content.test.tsx b/src/helpers/__tests__/text-content.test.tsx
index 76a9bd56..1c8a3f8b 100644
--- a/src/helpers/__tests__/text-content.test.tsx
+++ b/src/helpers/__tests__/text-content.test.tsx
@@ -1,11 +1,11 @@
import * as React from 'react';
import { Text } from 'react-native';
-import render from '../../render';
+import { render, screen } from '../../';
import { getTextContent } from '../text-content';
test('getTextContent with simple content', () => {
- const view = render(Hello world);
- expect(getTextContent(view.root)).toBe('Hello world');
+ render(Hello world);
+ expect(getTextContent(screen.root)).toBe('Hello world');
});
test('getTextContent with null element', () => {
@@ -13,37 +13,37 @@ test('getTextContent with null element', () => {
});
test('getTextContent with single nested content', () => {
- const view = render(
+ render(
Hello world
);
- expect(getTextContent(view.root)).toBe('Hello world');
+ expect(getTextContent(screen.root)).toBe('Hello world');
});
test('getTextContent with multiple nested content', () => {
- const view = render(
+ render(
Hello world
);
- expect(getTextContent(view.root)).toBe('Hello world');
+ expect(getTextContent(screen.root)).toBe('Hello world');
});
test('getTextContent with multiple number content', () => {
- const view = render(
+ render(
Hello world {100}
);
- expect(getTextContent(view.root)).toBe('Hello world 100');
+ expect(getTextContent(screen.root)).toBe('Hello world 100');
});
test('getTextContent with multiple boolean content', () => {
- const view = render(
+ render(
Hello{false} {true}world
);
- expect(getTextContent(view.root)).toBe('Hello world');
+ expect(getTextContent(screen.root)).toBe('Hello world');
});
diff --git a/src/matchers/__tests__/to-be-disabled.test.tsx b/src/matchers/__tests__/to-be-disabled.test.tsx
index 2a8ba44a..36554d7a 100644
--- a/src/matchers/__tests__/to-be-disabled.test.tsx
+++ b/src/matchers/__tests__/to-be-disabled.test.tsx
@@ -10,11 +10,11 @@ import {
Text,
View,
} from 'react-native';
-import { render } from '../..';
+import { render, screen } from '../..';
import '../extend-expect';
test('toBeDisabled()/toBeEnabled() supports basic case', () => {
- const screen = render(
+ render(
@@ -88,7 +88,7 @@ test('toBeDisabled()/toBeEnabled() supports basic case', () => {
});
test('toBeDisabled()/toBeEnabled() supports Pressable with "disabled" prop', () => {
- const screen = render(
+ render(
Button
@@ -158,7 +158,7 @@ test.each([
['TouchableWithoutFeedback', TouchableWithoutFeedback],
['TouchableNativeFeedback', TouchableNativeFeedback],
] as const)('toBeDisabled()/toBeEnabled() supports %s with "disabled" prop', (_, Component) => {
- const screen = render(
+ render(
// @ts-expect-error disabled prop is not available on all Touchables
Button
@@ -190,7 +190,7 @@ test.each([
] as const)(
'toBeDisabled()/toBeEnabled() supports %s with "aria-disabled" prop',
(_, Component) => {
- const screen = render(
+ render(
// @ts-expect-error too generic for typescript
Hello
@@ -217,7 +217,7 @@ test.each([
] as const)(
'toBeDisabled()/toBeEnabled() supports %s with "accessibilityState.disabled" prop',
(_, Component) => {
- const screen = render(
+ render(
// @ts-expect-error disabled prop is not available on all Touchables
Hello
@@ -233,7 +233,7 @@ test.each([
);
test('toBeDisabled()/toBeEnabled() supports "editable" prop on TextInput', () => {
- const screen = render(
+ render(
@@ -251,7 +251,7 @@ test('toBeDisabled()/toBeEnabled() supports "editable" prop on TextInput', () =>
});
test('toBeDisabled()/toBeEnabled() supports "disabled" prop on Button', () => {
- const screen = render(
+ render(
diff --git a/src/matchers/__tests__/to-have-style.test.tsx b/src/matchers/__tests__/to-have-style.test.tsx
index e3f59c61..44e321ed 100644
--- a/src/matchers/__tests__/to-have-style.test.tsx
+++ b/src/matchers/__tests__/to-have-style.test.tsx
@@ -1,6 +1,6 @@
import React from 'react';
import { StyleSheet, View, Pressable } from 'react-native';
-import { render } from '../..';
+import { render, screen } from '../..';
import '../extend-expect';
const styles = StyleSheet.create({
@@ -8,7 +8,7 @@ const styles = StyleSheet.create({
});
test('toHaveStyle() handles basic cases', () => {
- const screen = render(
+ render(
{
});
test('toHaveStyle error messages', () => {
- const screen = render(
+ render(
{
});
test('toHaveStyle() supports missing "style" prop', () => {
- const screen = render();
+ render();
const view = screen.getByTestId('view');
expect(view).not.toHaveStyle({ fontWeight: 'bold' });
});
test('toHaveStyle() supports undefined "transform" style', () => {
- const screen = render(
+ render(
{
});
test('toHaveStyle() supports Pressable with function "style" prop', () => {
- const screen = render( ({ backgroundColor: 'blue' })} />);
+ render( ({ backgroundColor: 'blue' })} />);
expect(screen.getByTestId('view')).toHaveStyle({ backgroundColor: 'blue' });
});
diff --git a/src/matchers/__tests__/utils.test.tsx b/src/matchers/__tests__/utils.test.tsx
index f73eb3ce..31cfdade 100644
--- a/src/matchers/__tests__/utils.test.tsx
+++ b/src/matchers/__tests__/utils.test.tsx
@@ -1,7 +1,7 @@
import React from 'react';
import { View } from 'react-native';
-import { render } from '../..';
-import { formatElement, checkHostElement } from '../utils';
+import { render, screen } from '../..';
+import { checkHostElement, formatElement } from '../utils';
function fakeMatcher() {
// Do nothing.
@@ -12,7 +12,7 @@ test('formatElement', () => {
});
test('checkHostElement allows host element', () => {
- const screen = render();
+ render();
expect(() => {
// @ts-expect-error
@@ -21,7 +21,7 @@ test('checkHostElement allows host element', () => {
});
test('checkHostElement allows rejects composite element', () => {
- const screen = render();
+ render();
expect(() => {
// @ts-expect-error
diff --git a/src/queries/__tests__/accessibility-state.test.tsx b/src/queries/__tests__/accessibility-state.test.tsx
index c4ac14e7..b11f5b21 100644
--- a/src/queries/__tests__/accessibility-state.test.tsx
+++ b/src/queries/__tests__/accessibility-state.test.tsx
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import * as React from 'react';
import { View, Text, Pressable, TouchableOpacity } from 'react-native';
-import { render } from '../..';
+import { render, screen } from '../..';
type ConsoleLogMock = jest.Mock;
@@ -29,213 +29,215 @@ const Section = () => (
);
test('getByA11yState, queryByA11yState, findByA11yState', async () => {
- const { getByA11yState, queryByA11yState, findByA11yState } = render();
+ render();
- expect(getByA11yState({ selected: true }).props.accessibilityState).toEqual({
+ expect(screen.getByA11yState({ selected: true }).props.accessibilityState).toEqual({
selected: true,
expanded: false,
});
- expect(queryByA11yState({ selected: true })?.props.accessibilityState).toEqual({
+ expect(screen.queryByA11yState({ selected: true })?.props.accessibilityState).toEqual({
selected: true,
expanded: false,
});
- expect(() => getByA11yState({ disabled: true })).toThrow(
+ expect(() => screen.getByA11yState({ disabled: true })).toThrow(
'Unable to find an element with disabled state: true'
);
- expect(queryByA11yState({ disabled: true })).toEqual(null);
+ expect(screen.queryByA11yState({ disabled: true })).toEqual(null);
- expect(() => getByA11yState({ expanded: false })).toThrow(
+ expect(() => screen.getByA11yState({ expanded: false })).toThrow(
'Found multiple elements with expanded state: false'
);
- expect(() => queryByA11yState({ expanded: false })).toThrow(
+ expect(() => screen.queryByA11yState({ expanded: false })).toThrow(
'Found multiple elements with expanded state: false'
);
- const asyncButton = await findByA11yState({ selected: true });
+ const asyncButton = await screen.findByA11yState({ selected: true });
expect(asyncButton.props.accessibilityState).toEqual({
selected: true,
expanded: false,
});
- await expect(findByA11yState({ disabled: true })).rejects.toThrow(
+ await expect(screen.findByA11yState({ disabled: true })).rejects.toThrow(
'Unable to find an element with disabled state: true'
);
- await expect(findByA11yState({ expanded: false })).rejects.toThrow(
+ await expect(screen.findByA11yState({ expanded: false })).rejects.toThrow(
'Found multiple elements with expanded state: false'
);
});
test('getAllByA11yState, queryAllByA11yState, findAllByA11yState', async () => {
- const { getAllByA11yState, queryAllByA11yState, findAllByA11yState } = render();
+ render();
- expect(getAllByA11yState({ selected: true })).toHaveLength(1);
- expect(queryAllByA11yState({ selected: true })).toHaveLength(1);
+ expect(screen.getAllByA11yState({ selected: true })).toHaveLength(1);
+ expect(screen.queryAllByA11yState({ selected: true })).toHaveLength(1);
- expect(() => getAllByA11yState({ disabled: true })).toThrow(
+ expect(() => screen.getAllByA11yState({ disabled: true })).toThrow(
'Unable to find an element with disabled state: true'
);
- expect(queryAllByA11yState({ disabled: true })).toEqual([]);
+ expect(screen.queryAllByA11yState({ disabled: true })).toEqual([]);
- expect(getAllByA11yState({ expanded: false })).toHaveLength(2);
- expect(queryAllByA11yState({ expanded: false })).toHaveLength(2);
+ expect(screen.getAllByA11yState({ expanded: false })).toHaveLength(2);
+ expect(screen.queryAllByA11yState({ expanded: false })).toHaveLength(2);
- await expect(findAllByA11yState({ selected: true })).resolves.toHaveLength(1);
- await expect(findAllByA11yState({ disabled: true })).rejects.toThrow(
+ await expect(screen.findAllByA11yState({ selected: true })).resolves.toHaveLength(1);
+ await expect(screen.findAllByA11yState({ disabled: true })).rejects.toThrow(
'Unable to find an element with disabled state: true'
);
- await expect(findAllByA11yState({ expanded: false })).resolves.toHaveLength(2);
+ await expect(screen.findAllByA11yState({ expanded: false })).resolves.toHaveLength(2);
});
describe('checked state matching', () => {
it('handles true', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ checked: true })).toBeTruthy();
- expect(view.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
- expect(view.queryByA11yState({ checked: false })).toBeFalsy();
+ expect(screen.getByA11yState({ checked: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
});
it('handles mixed', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ checked: 'mixed' })).toBeTruthy();
- expect(view.queryByA11yState({ checked: true })).toBeFalsy();
- expect(view.queryByA11yState({ checked: false })).toBeFalsy();
+ expect(screen.getByA11yState({ checked: 'mixed' })).toBeTruthy();
+ expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
});
it('handles false', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ checked: false })).toBeTruthy();
- expect(view.queryByA11yState({ checked: true })).toBeFalsy();
- expect(view.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
+ expect(screen.getByA11yState({ checked: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
});
it('handles default', () => {
- const view = render();
+ render();
- expect(view.queryByA11yState({ checked: false })).toBeFalsy();
- expect(view.queryByA11yState({ checked: true })).toBeFalsy();
- expect(view.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: false })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: true })).toBeFalsy();
+ expect(screen.queryByA11yState({ checked: 'mixed' })).toBeFalsy();
});
});
describe('expanded state matching', () => {
it('handles true', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ expanded: true })).toBeTruthy();
- expect(view.queryByA11yState({ expanded: false })).toBeFalsy();
+ expect(screen.getByA11yState({ expanded: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ expanded: false })).toBeFalsy();
});
it('handles false', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ expanded: false })).toBeTruthy();
- expect(view.queryByA11yState({ expanded: true })).toBeFalsy();
+ expect(screen.getByA11yState({ expanded: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ expanded: true })).toBeFalsy();
});
it('handles default', () => {
- const view = render();
+ render();
- expect(view.queryByA11yState({ expanded: false })).toBeFalsy();
- expect(view.queryByA11yState({ expanded: true })).toBeFalsy();
+ expect(screen.queryByA11yState({ expanded: false })).toBeFalsy();
+ expect(screen.queryByA11yState({ expanded: true })).toBeFalsy();
});
});
describe('disabled state matching', () => {
it('handles true', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ disabled: true })).toBeTruthy();
- expect(view.queryByA11yState({ disabled: false })).toBeFalsy();
+ expect(screen.getByA11yState({ disabled: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ disabled: false })).toBeFalsy();
});
it('handles false', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ disabled: false })).toBeTruthy();
- expect(view.queryByA11yState({ disabled: true })).toBeFalsy();
+ expect(screen.getByA11yState({ disabled: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ disabled: true })).toBeFalsy();
});
it('handles default', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ disabled: false })).toBeTruthy();
- expect(view.queryByA11yState({ disabled: true })).toBeFalsy();
+ expect(screen.getByA11yState({ disabled: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ disabled: true })).toBeFalsy();
});
});
describe('busy state matching', () => {
it('handles true', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ busy: true })).toBeTruthy();
- expect(view.queryByA11yState({ busy: false })).toBeFalsy();
+ expect(screen.getByA11yState({ busy: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ busy: false })).toBeFalsy();
});
it('handles false', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ busy: false })).toBeTruthy();
- expect(view.queryByA11yState({ busy: true })).toBeFalsy();
+ expect(screen.getByA11yState({ busy: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ busy: true })).toBeFalsy();
});
it('handles default', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ busy: false })).toBeTruthy();
- expect(view.queryByA11yState({ busy: true })).toBeFalsy();
+ expect(screen.getByA11yState({ busy: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ busy: true })).toBeFalsy();
});
});
describe('selected state matching', () => {
it('handles true', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ selected: true })).toBeTruthy();
- expect(view.queryByA11yState({ selected: false })).toBeFalsy();
+ expect(screen.getByA11yState({ selected: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ selected: false })).toBeFalsy();
});
it('handles false', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ selected: false })).toBeTruthy();
- expect(view.queryByA11yState({ selected: true })).toBeFalsy();
+ expect(screen.getByA11yState({ selected: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ selected: true })).toBeFalsy();
});
it('handles default', () => {
- const view = render();
+ render();
- expect(view.getByA11yState({ selected: false })).toBeTruthy();
- expect(view.queryByA11yState({ selected: true })).toBeFalsy();
+ expect(screen.getByA11yState({ selected: false })).toBeTruthy();
+ expect(screen.queryByA11yState({ selected: true })).toBeFalsy();
});
});
test('*ByA11yState on Pressable with "disabled" prop', () => {
- const view = render();
- expect(view.getByA11yState({ disabled: true })).toBeTruthy();
- expect(view.queryByA11yState({ disabled: false })).toBeFalsy();
+ render();
+ expect(screen.getByA11yState({ disabled: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ disabled: false })).toBeFalsy();
});
test('*ByA11yState on TouchableOpacity with "disabled" prop', () => {
- const view = render();
- expect(view.getByA11yState({ disabled: true })).toBeTruthy();
- expect(view.queryByA11yState({ disabled: false })).toBeFalsy();
+ render();
+ expect(screen.getByA11yState({ disabled: true })).toBeTruthy();
+ expect(screen.queryByA11yState({ disabled: false })).toBeFalsy();
});
test('byA11yState queries support hidden option', () => {
- const { getByA11yState, queryByA11yState } = render(
+ render(
Hidden from accessibility
);
- expect(getByA11yState({ expanded: false }, { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByA11yState({ expanded: false }, { includeHiddenElements: true })).toBeTruthy();
- expect(queryByA11yState({ expanded: false })).toBeFalsy();
- expect(queryByA11yState({ expanded: false }, { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByA11yState({ expanded: false }, { includeHiddenElements: false }))
+ expect(screen.queryByA11yState({ expanded: false })).toBeFalsy();
+ expect(
+ screen.queryByA11yState({ expanded: false }, { includeHiddenElements: false })
+ ).toBeFalsy();
+ expect(() => screen.getByA11yState({ expanded: false }, { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with expanded state: false
@@ -261,44 +263,44 @@ test('byA11yState queries support hidden option', () => {
test('*ByA11yState deprecation warnings', async () => {
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
- const view = render();
+ render();
- view.getByA11yState({ disabled: true });
+ screen.getByA11yState({ disabled: true });
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
"getByA11yState(...) is deprecated and will be removed in the future.
Use getByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.getAllByA11yState({ disabled: true });
+ screen.getAllByA11yState({ disabled: true });
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
"getAllByA11yState(...) is deprecated and will be removed in the future.
Use getAllByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.queryByA11yState({ disabled: true });
+ screen.queryByA11yState({ disabled: true });
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
"queryByA11yState(...) is deprecated and will be removed in the future.
Use queryByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.queryAllByA11yState({ disabled: true });
+ screen.queryAllByA11yState({ disabled: true });
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
"queryAllByA11yState(...) is deprecated and will be removed in the future.
Use queryAllByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- await view.findByA11yState({ disabled: true });
+ await screen.findByA11yState({ disabled: true });
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
"findByA11yState(...) is deprecated and will be removed in the future.
Use findByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- await view.findAllByA11yState({ disabled: true });
+ await screen.findAllByA11yState({ disabled: true });
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
"findAllByA11yState(...) is deprecated and will be removed in the future.
@@ -308,44 +310,44 @@ test('*ByA11yState deprecation warnings', async () => {
test('*ByAccessibilityState deprecation warnings', async () => {
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
- const view = render();
+ render();
- view.getByAccessibilityState({ disabled: true });
+ screen.getByAccessibilityState({ disabled: true });
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
"getByAccessibilityState(...) is deprecated and will be removed in the future.
Use getByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.getAllByAccessibilityState({ disabled: true });
+ screen.getAllByAccessibilityState({ disabled: true });
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
"getAllByAccessibilityState(...) is deprecated and will be removed in the future.
Use getAllByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.queryByAccessibilityState({ disabled: true });
+ screen.queryByAccessibilityState({ disabled: true });
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
"queryByAccessibilityState(...) is deprecated and will be removed in the future.
Use queryByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- view.queryAllByAccessibilityState({ disabled: true });
+ screen.queryAllByAccessibilityState({ disabled: true });
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
"queryAllByAccessibilityState(...) is deprecated and will be removed in the future.
Use queryAllByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- await view.findByAccessibilityState({ disabled: true });
+ await screen.findByAccessibilityState({ disabled: true });
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
"findByAccessibilityState(...) is deprecated and will be removed in the future.
Use findByRole(role, { disabled, selected, checked, busy, expanded }) query or built-in Jest matchers: toBeDisabled(), toBeSelected(), toBeChecked(), toBeBusy(), and toBeExpanded() instead."
`);
- await view.findAllByAccessibilityState({ disabled: true });
+ await screen.findAllByAccessibilityState({ disabled: true });
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
"findAllByAccessibilityState(...) is deprecated and will be removed in the future.
@@ -354,13 +356,13 @@ test('*ByAccessibilityState deprecation warnings', async () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render(
+ render(
null}>
Some text
);
- expect(() => view.getByA11yState({ checked: true })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByA11yState({ checked: true })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with checked state: true
"
`);
- expect(() => view.getAllByA11yState({ checked: true })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByA11yState({ checked: true })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with checked state: true
"
`);
- await expect(view.findByA11yState({ checked: true })).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByA11yState({ checked: true })).rejects
+ .toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with checked state: true
"
`);
- await expect(view.findAllByA11yState({ checked: true })).rejects
+ await expect(screen.findAllByA11yState({ checked: true })).rejects
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with checked state: true
@@ -420,19 +423,19 @@ test('error message renders the element tree, preserving only helpful props', as
describe('aria-disabled prop', () => {
test('supports aria-disabled={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ disabled: true })).toBeTruthy();
expect(screen.queryByAccessibilityState({ disabled: false })).toBeNull();
});
test('supports aria-disabled={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ disabled: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ disabled: true })).toBeNull();
});
test('supports default aria-disabled prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ disabled: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ disabled: true })).toBeNull();
});
@@ -440,19 +443,19 @@ describe('aria-disabled prop', () => {
describe('aria-selected prop', () => {
test('supports aria-selected={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ selected: true })).toBeTruthy();
expect(screen.queryByAccessibilityState({ selected: false })).toBeNull();
});
test('supports aria-selected={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ selected: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ selected: true })).toBeNull();
});
test('supports default aria-selected prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ selected: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ selected: true })).toBeNull();
});
@@ -460,28 +463,28 @@ describe('aria-selected prop', () => {
describe('aria-checked prop', () => {
test('supports aria-checked={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ checked: true })).toBeTruthy();
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();
expect(screen.queryByAccessibilityState({ checked: 'mixed' })).toBeNull();
});
test('supports aria-checked={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ checked: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
expect(screen.queryByAccessibilityState({ checked: 'mixed' })).toBeNull();
});
test('supports aria-checked="mixed prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ checked: 'mixed' })).toBeTruthy();
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();
});
test('supports default aria-selected prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({})).toBeTruthy();
expect(screen.queryByAccessibilityState({ checked: true })).toBeNull();
expect(screen.queryByAccessibilityState({ checked: false })).toBeNull();
@@ -491,19 +494,19 @@ describe('aria-checked prop', () => {
describe('aria-busy prop', () => {
test('supports aria-busy={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ busy: true })).toBeTruthy();
expect(screen.queryByAccessibilityState({ busy: false })).toBeNull();
});
test('supports aria-busy={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ busy: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ busy: true })).toBeNull();
});
test('supports default aria-busy prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ busy: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ busy: true })).toBeNull();
});
@@ -511,19 +514,20 @@ describe('aria-busy prop', () => {
describe('aria-expanded prop', () => {
test('supports aria-expanded={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ expanded: true })).toBeTruthy();
expect(screen.queryByAccessibilityState({ expanded: false })).toBeNull();
});
test('supports aria-expanded={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityState({ expanded: false })).toBeTruthy();
expect(screen.queryByAccessibilityState({ expanded: true })).toBeNull();
});
test('supports default aria-expanded prop', () => {
- const screen = render();
+ render();
+ render();
expect(screen.getByAccessibilityState({})).toBeTruthy();
expect(screen.queryByAccessibilityState({ expanded: true })).toBeNull();
expect(screen.queryByAccessibilityState({ expanded: false })).toBeNull();
diff --git a/src/queries/__tests__/accessibility-value.test.tsx b/src/queries/__tests__/accessibility-value.test.tsx
index cd096463..8de43af5 100644
--- a/src/queries/__tests__/accessibility-value.test.tsx
+++ b/src/queries/__tests__/accessibility-value.test.tsx
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
import * as React from 'react';
-import { View, Text, TouchableOpacity } from 'react-native';
-import { render } from '../..';
+import { Text, TouchableOpacity, View } from 'react-native';
+import { render, screen } from '../..';
type ConsoleLogMock = jest.Mock;
@@ -29,69 +29,75 @@ const Section = () => (
);
test('getByA11yValue, queryByA11yValue, findByA11yValue', async () => {
- const { getByA11yValue, queryByA11yValue, findByA11yValue } = render();
+ render();
- expect(getByA11yValue({ min: 40 }).props.accessibilityValue).toEqual({
+ expect(screen.getByA11yValue({ min: 40 }).props.accessibilityValue).toEqual({
min: 40,
max: 60,
});
- expect(queryByA11yValue({ min: 40 })?.props.accessibilityValue).toEqual({
+ expect(screen.queryByA11yValue({ min: 40 })?.props.accessibilityValue).toEqual({
min: 40,
max: 60,
});
- expect(() => getByA11yValue({ min: 50 })).toThrow('Unable to find an element with min value: 50');
- expect(queryByA11yValue({ min: 50 })).toEqual(null);
+ expect(() => screen.getByA11yValue({ min: 50 })).toThrow(
+ 'Unable to find an element with min value: 50'
+ );
+ expect(screen.queryByA11yValue({ min: 50 })).toEqual(null);
- expect(() => getByA11yValue({ max: 60 })).toThrow('Found multiple elements with max value: 60');
- expect(() => queryByA11yValue({ max: 60 })).toThrow('Found multiple elements with max value: 60');
+ expect(() => screen.getByA11yValue({ max: 60 })).toThrow(
+ 'Found multiple elements with max value: 60'
+ );
+ expect(() => screen.queryByA11yValue({ max: 60 })).toThrow(
+ 'Found multiple elements with max value: 60'
+ );
- const asyncElement = await findByA11yValue({ min: 40 });
+ const asyncElement = await screen.findByA11yValue({ min: 40 });
expect(asyncElement.props.accessibilityValue).toEqual({
min: 40,
max: 60,
});
- await expect(findByA11yValue({ min: 50 })).rejects.toThrow(
+ await expect(screen.findByA11yValue({ min: 50 })).rejects.toThrow(
'Unable to find an element with min value: 50'
);
- await expect(findByA11yValue({ max: 60 })).rejects.toThrow(
+ await expect(screen.findByA11yValue({ max: 60 })).rejects.toThrow(
'Found multiple elements with max value: 60'
);
});
test('getAllByA11yValue, queryAllByA11yValue, findAllByA11yValue', async () => {
- const { getAllByA11yValue, queryAllByA11yValue, findAllByA11yValue } = render();
+ render();
- expect(getAllByA11yValue({ min: 40 })).toHaveLength(1);
- expect(queryAllByA11yValue({ min: 40 })).toHaveLength(1);
+ expect(screen.getAllByA11yValue({ min: 40 })).toHaveLength(1);
+ expect(screen.queryAllByA11yValue({ min: 40 })).toHaveLength(1);
- expect(() => getAllByA11yValue({ min: 50 })).toThrow(
+ expect(() => screen.getAllByA11yValue({ min: 50 })).toThrow(
'Unable to find an element with min value: 50'
);
- expect(queryAllByA11yValue({ min: 50 })).toEqual([]);
+ expect(screen.queryAllByA11yValue({ min: 50 })).toEqual([]);
- expect(queryAllByA11yValue({ max: 60 })).toHaveLength(2);
- expect(getAllByA11yValue({ max: 60 })).toHaveLength(2);
+ expect(screen.queryAllByA11yValue({ max: 60 })).toHaveLength(2);
+ expect(screen.getAllByA11yValue({ max: 60 })).toHaveLength(2);
- await expect(findAllByA11yValue({ min: 40 })).resolves.toHaveLength(1);
- await expect(findAllByA11yValue({ min: 50 })).rejects.toThrow(
+ await expect(screen.findAllByA11yValue({ min: 40 })).resolves.toHaveLength(1);
+ await expect(screen.findAllByA11yValue({ min: 50 })).rejects.toThrow(
'Unable to find an element with min value: 50'
);
- await expect(findAllByA11yValue({ max: 60 })).resolves.toHaveLength(2);
+ await expect(screen.findAllByA11yValue({ max: 60 })).resolves.toHaveLength(2);
});
test('byA11yValue queries support hidden option', () => {
- const { getByA11yValue, queryByA11yValue } = render(
+ render(
Hidden from accessibility
);
- expect(getByA11yValue({ max: 10 }, { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByA11yValue({ max: 10 }, { includeHiddenElements: true })).toBeTruthy();
- expect(queryByA11yValue({ max: 10 })).toBeFalsy();
- expect(queryByA11yValue({ max: 10 }, { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByA11yValue({ max: 10 }, { includeHiddenElements: false }))
+ expect(screen.queryByA11yValue({ max: 10 })).toBeFalsy();
+ expect(screen.queryByA11yValue({ max: 10 }, { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByA11yValue({ max: 10 }, { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with max value: 10
@@ -113,23 +119,24 @@ test('byA11yValue queries support hidden option', () => {
});
test('byA11yValue error messages', () => {
- const { getByA11yValue } = render();
- expect(() => getByA11yValue({ min: 10, max: 10 })).toThrowErrorMatchingInlineSnapshot(`
+ render();
+ expect(() => screen.getByA11yValue({ min: 10, max: 10 })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 10, max value: 10
"
`);
- expect(() => getByA11yValue({ max: 20, now: 5 })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByA11yValue({ max: 20, now: 5 })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with max value: 20, now value: 5
"
`);
- expect(() => getByA11yValue({ min: 1, max: 2, now: 3 })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByA11yValue({ min: 1, max: 2, now: 3 }))
+ .toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1, max value: 2, now value: 3
"
`);
- expect(() => getByA11yValue({ min: 1, max: 2, now: 3, text: /foo/i }))
+ expect(() => screen.getByA11yValue({ min: 1, max: 2, now: 3, text: /foo/i }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1, max value: 2, now value: 3, text value: /foo/i
@@ -139,44 +146,44 @@ test('byA11yValue error messages', () => {
test('*ByA11yValue deprecation warnings', async () => {
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
- const view = render();
+ render();
- view.getByA11yValue({ min: 10 });
+ screen.getByA11yValue({ min: 10 });
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
"getByA11yValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or getByRole(role, { value: ... }) query instead."
`);
- view.getAllByA11yValue({ min: 10 });
+ screen.getAllByA11yValue({ min: 10 });
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
"getAllByA11yValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or getAllByRole(role, { value: ... }) query instead."
`);
- view.queryByA11yValue({ min: 10 });
+ screen.queryByA11yValue({ min: 10 });
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
"queryByA11yValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or queryByRole(role, { value: ... }) query instead."
`);
- view.queryAllByA11yValue({ min: 10 });
+ screen.queryAllByA11yValue({ min: 10 });
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
"queryAllByA11yValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or queryAllByRole(role, { value: ... }) query instead."
`);
- await view.findByA11yValue({ min: 10 });
+ await screen.findByA11yValue({ min: 10 });
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
"findByA11yValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or findByRole(role, { value: ... }) query instead."
`);
- await view.findAllByA11yValue({ min: 10 });
+ await screen.findAllByA11yValue({ min: 10 });
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
"findAllByA11yValue(...) is deprecated and will be removed in the future.
@@ -186,44 +193,44 @@ test('*ByA11yValue deprecation warnings', async () => {
test('*ByAccessibilityValue deprecation warnings', async () => {
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
- const view = render();
+ render();
- view.getByAccessibilityValue({ min: 10 });
+ screen.getByAccessibilityValue({ min: 10 });
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
"getByAccessibilityValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or getByRole(role, { value: ... }) query instead."
`);
- view.getAllByAccessibilityValue({ min: 10 });
+ screen.getAllByAccessibilityValue({ min: 10 });
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
"getAllByAccessibilityValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or getAllByRole(role, { value: ... }) query instead."
`);
- view.queryByAccessibilityValue({ min: 10 });
+ screen.queryByAccessibilityValue({ min: 10 });
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
"queryByAccessibilityValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or queryByRole(role, { value: ... }) query instead."
`);
- view.queryAllByAccessibilityValue({ min: 10 });
+ screen.queryAllByAccessibilityValue({ min: 10 });
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
"queryAllByAccessibilityValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or queryAllByRole(role, { value: ... }) query instead."
`);
- await view.findByAccessibilityValue({ min: 10 });
+ await screen.findByAccessibilityValue({ min: 10 });
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
"findByAccessibilityValue(...) is deprecated and will be removed in the future.
Use toHaveAccessibilityValue(...) built-in Jest matcher or findByRole(role, { value: ... }) query instead."
`);
- await view.findAllByAccessibilityValue({ min: 10 });
+ await screen.findAllByAccessibilityValue({ min: 10 });
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
"findAllByAccessibilityValue(...) is deprecated and will be removed in the future.
@@ -232,9 +239,9 @@ test('*ByAccessibilityValue deprecation warnings', async () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByA11yValue({ min: 1 })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByA11yValue({ min: 1 })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1
"
`);
- expect(() => view.getAllByA11yValue({ min: 1 })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByA11yValue({ min: 1 })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1
"
`);
- await expect(view.findByA11yValue({ min: 1 })).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByA11yValue({ min: 1 })).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1
"
`);
- await expect(view.findAllByA11yValue({ min: 1 })).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByA11yValue({ min: 1 })).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with min value: 1
{
test('supports "aria-valuemax"', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityValue({ max: 10 })).toBeTruthy();
});
test('supports "aria-valuemin"', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityValue({ min: 20 })).toBeTruthy();
});
test('supports "aria-valuenow"', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityValue({ now: 30 })).toBeTruthy();
});
test('supports "aria-valuetext"', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityValue({ text: 'Hello World' })).toBeTruthy();
expect(screen.getByAccessibilityValue({ text: /hello/i })).toBeTruthy();
});
test('supports multiple "aria-value*" props', () => {
- const screen = render();
+ render();
expect(screen.getByAccessibilityValue({ now: 50, min: 0, max: 100 })).toBeTruthy();
});
});
diff --git a/src/queries/__tests__/display-value.test.tsx b/src/queries/__tests__/display-value.test.tsx
index 9a04cde9..d9e4b289 100644
--- a/src/queries/__tests__/display-value.test.tsx
+++ b/src/queries/__tests__/display-value.test.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
import { TextInput, View } from 'react-native';
-import { render } from '../..';
+import { render, screen } from '../..';
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
@@ -29,28 +29,28 @@ const Banana = () => (
);
test('getByDisplayValue, queryByDisplayValue', () => {
- const { getByDisplayValue, queryByDisplayValue } = render();
- const input = getByDisplayValue(/custom/i);
+ render();
+ const input = screen.getByDisplayValue(/custom/i);
expect(input.props.value).toBe(INPUT_FRESHNESS);
- const sameInput = getByDisplayValue(INPUT_FRESHNESS);
+ const sameInput = screen.getByDisplayValue(INPUT_FRESHNESS);
expect(sameInput.props.value).toBe(INPUT_FRESHNESS);
- expect(() => getByDisplayValue('no value')).toThrow(
+ expect(() => screen.getByDisplayValue('no value')).toThrow(
'Unable to find an element with displayValue: no value'
);
- expect(queryByDisplayValue(/custom/i)).toBe(input);
- expect(queryByDisplayValue('no value')).toBeNull();
- expect(() => queryByDisplayValue(/fresh/i)).toThrow(
+ expect(screen.queryByDisplayValue(/custom/i)).toBe(input);
+ expect(screen.queryByDisplayValue('no value')).toBeNull();
+ expect(() => screen.queryByDisplayValue(/fresh/i)).toThrow(
'Found multiple elements with display value: /fresh/i'
);
});
test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => {
- const { getByDisplayValue, queryByDisplayValue } = render();
- expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrowErrorMatchingInlineSnapshot(`
+ render();
+ expect(() => screen.getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: What did you inspect?
@@ -74,9 +74,9 @@ test('getByDisplayValue, queryByDisplayValue get element by default value only w
/>
"
`);
- expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull();
+ expect(screen.queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull();
- expect(() => getByDisplayValue('hello')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByDisplayValue('hello')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: hello
@@ -100,35 +100,35 @@ test('getByDisplayValue, queryByDisplayValue get element by default value only w
/>
"
`);
- expect(queryByDisplayValue('hello')).toBeNull();
+ expect(screen.queryByDisplayValue('hello')).toBeNull();
- expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
- expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
+ expect(screen.getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
+ expect(screen.queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy();
});
test('getAllByDisplayValue, queryAllByDisplayValue', () => {
- const { getAllByDisplayValue, queryAllByDisplayValue } = render();
- const inputs = getAllByDisplayValue(/fresh/i);
+ render();
+ const inputs = screen.getAllByDisplayValue(/fresh/i);
expect(inputs).toHaveLength(2);
- expect(() => getAllByDisplayValue('no value')).toThrow(
+ expect(() => screen.getAllByDisplayValue('no value')).toThrow(
'Unable to find an element with displayValue: no value'
);
- expect(queryAllByDisplayValue(/fresh/i)).toEqual(inputs);
- expect(queryAllByDisplayValue('no value')).toHaveLength(0);
+ expect(screen.queryAllByDisplayValue(/fresh/i)).toEqual(inputs);
+ expect(screen.queryAllByDisplayValue('no value')).toHaveLength(0);
});
test('findBy queries work asynchronously', async () => {
const options = { timeout: 10 }; // Short timeout so that this test runs quickly
- const { rerender, findByDisplayValue, findAllByDisplayValue } = render();
+ render();
- await expect(findByDisplayValue('Display Value', {}, options)).rejects.toBeTruthy();
- await expect(findAllByDisplayValue('Display Value', {}, options)).rejects.toBeTruthy();
+ await expect(screen.findByDisplayValue('Display Value', {}, options)).rejects.toBeTruthy();
+ await expect(screen.findAllByDisplayValue('Display Value', {}, options)).rejects.toBeTruthy();
setTimeout(
() =>
- rerender(
+ screen.rerender(
@@ -136,20 +136,18 @@ test('findBy queries work asynchronously', async () => {
20
);
- await expect(findByDisplayValue('Display Value')).resolves.toBeTruthy();
- await expect(findAllByDisplayValue('Display Value')).resolves.toHaveLength(1);
+ await expect(screen.findByDisplayValue('Display Value')).resolves.toBeTruthy();
+ await expect(screen.findAllByDisplayValue('Display Value')).resolves.toHaveLength(1);
}, 20000);
test('byDisplayValue queries support hidden option', () => {
- const { getByDisplayValue, queryByDisplayValue } = render(
-
- );
+ render();
- expect(getByDisplayValue('hidden', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByDisplayValue('hidden', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByDisplayValue('hidden')).toBeFalsy();
- expect(queryByDisplayValue('hidden', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByDisplayValue('hidden', { includeHiddenElements: false }))
+ expect(screen.queryByDisplayValue('hidden')).toBeFalsy();
+ expect(screen.queryByDisplayValue('hidden', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByDisplayValue('hidden', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: hidden
@@ -165,15 +163,15 @@ test('byDisplayValue queries support hidden option', () => {
});
test('byDisplayValue should return host component', () => {
- const { getByDisplayValue } = render();
+ render();
- expect(getByDisplayValue('value').type).toBe('TextInput');
+ expect(screen.getByDisplayValue('value').type).toBe('TextInput');
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByDisplayValue('2')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByDisplayValue('2')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: 2
"
`);
- expect(() => view.getAllByDisplayValue('2')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByDisplayValue('2')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: 2
"
`);
- await expect(view.findByDisplayValue('2')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByDisplayValue('2')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: 2
"
`);
- await expect(view.findAllByDisplayValue('2')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByDisplayValue('2')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with displayValue: 2
(
);
test('getByA11yHint, queryByA11yHint, findByA11yHint', async () => {
- const { getByA11yHint, queryByA11yHint, findByA11yHint } = render();
+ render();
- expect(getByA11yHint(BUTTON_HINT).props.accessibilityHint).toEqual(BUTTON_HINT);
- const button = queryByA11yHint(BUTTON_HINT);
+ expect(screen.getByA11yHint(BUTTON_HINT).props.accessibilityHint).toEqual(BUTTON_HINT);
+ const button = screen.queryByA11yHint(BUTTON_HINT);
expect(button?.props.accessibilityHint).toEqual(BUTTON_HINT);
- expect(() => getByA11yHint(NO_MATCHES_TEXT)).toThrow(getNoInstancesFoundMessage(NO_MATCHES_TEXT));
- expect(queryByA11yHint(NO_MATCHES_TEXT)).toBeNull();
+ expect(() => screen.getByA11yHint(NO_MATCHES_TEXT)).toThrow(
+ getNoInstancesFoundMessage(NO_MATCHES_TEXT)
+ );
+ expect(screen.queryByA11yHint(NO_MATCHES_TEXT)).toBeNull();
- expect(() => getByA11yHint(TEXT_HINT)).toThrow(getMultipleInstancesFoundMessage(TEXT_HINT));
- expect(() => queryByA11yHint(TEXT_HINT)).toThrow(getMultipleInstancesFoundMessage(TEXT_HINT));
+ expect(() => screen.getByA11yHint(TEXT_HINT)).toThrow(
+ getMultipleInstancesFoundMessage(TEXT_HINT)
+ );
+ expect(() => screen.queryByA11yHint(TEXT_HINT)).toThrow(
+ getMultipleInstancesFoundMessage(TEXT_HINT)
+ );
- const asyncButton = await findByA11yHint(BUTTON_HINT);
+ const asyncButton = await screen.findByA11yHint(BUTTON_HINT);
expect(asyncButton.props.accessibilityHint).toEqual(BUTTON_HINT);
- await expect(findByA11yHint(NO_MATCHES_TEXT)).rejects.toThrow(
+ await expect(screen.findByA11yHint(NO_MATCHES_TEXT)).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- await expect(findByA11yHint(TEXT_HINT)).rejects.toThrow(
+ await expect(screen.findByA11yHint(TEXT_HINT)).rejects.toThrow(
getMultipleInstancesFoundMessage(TEXT_HINT)
);
});
test('getAllByA11yHint, queryAllByA11yHint, findAllByA11yHint', async () => {
- const { getAllByA11yHint, queryAllByA11yHint, findAllByA11yHint } = render();
+ render();
- expect(getAllByA11yHint(TEXT_HINT)).toHaveLength(2);
- expect(queryAllByA11yHint(TEXT_HINT)).toHaveLength(2);
+ expect(screen.getAllByA11yHint(TEXT_HINT)).toHaveLength(2);
+ expect(screen.queryAllByA11yHint(TEXT_HINT)).toHaveLength(2);
- expect(() => getAllByA11yHint(NO_MATCHES_TEXT)).toThrow(
+ expect(() => screen.getAllByA11yHint(NO_MATCHES_TEXT)).toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- expect(queryAllByA11yHint(NO_MATCHES_TEXT)).toEqual([]);
+ expect(screen.queryAllByA11yHint(NO_MATCHES_TEXT)).toEqual([]);
- await expect(findAllByA11yHint(TEXT_HINT)).resolves.toHaveLength(2);
- await expect(findAllByA11yHint(NO_MATCHES_TEXT)).rejects.toThrow(
+ await expect(screen.findAllByA11yHint(TEXT_HINT)).resolves.toHaveLength(2);
+ await expect(screen.findAllByA11yHint(NO_MATCHES_TEXT)).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
});
test('getByHintText, getByHintText', () => {
- const { getByHintText, getAllByHintText } = render(
+ render(
);
- expect(getByHintText('id', { exact: false })).toBeTruthy();
- expect(getAllByHintText('test', { exact: false })).toHaveLength(2);
+ expect(screen.getByHintText('id', { exact: false })).toBeTruthy();
+ expect(screen.getAllByHintText('test', { exact: false })).toHaveLength(2);
});
test('getByHintText, getByHintText and exact = true', () => {
- const { queryByHintText, getAllByHintText } = render(
+ render(
);
- expect(queryByHintText('id', { exact: true })).toBeNull();
- expect(getAllByHintText('test', { exact: true })).toHaveLength(1);
+ expect(screen.queryByHintText('id', { exact: true })).toBeNull();
+ expect(screen.getAllByHintText('test', { exact: true })).toHaveLength(1);
});
test('byHintText queries support hidden option', () => {
- const { getByHintText, queryByHintText } = render(
+ render(
Hidden from accessiblity
);
- expect(getByHintText('hidden', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByHintText('hidden', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByHintText('hidden')).toBeFalsy();
- expect(queryByHintText('hidden', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByHintText('hidden', { includeHiddenElements: false }))
+ expect(screen.queryByHintText('hidden')).toBeFalsy();
+ expect(screen.queryByHintText('hidden', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByHintText('hidden', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibilityHint: hidden
@@ -124,9 +130,9 @@ test('byHintText queries support hidden option', () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByHintText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByHintText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibilityHint: FOO
"
`);
- expect(() => view.getAllByHintText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByHintText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibilityHint: FOO
"
`);
- await expect(view.findByHintText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByHintText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibilityHint: FOO
"
`);
- await expect(view.findAllByHintText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByHintText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibilityHint: FOO
(
);
test('getByLabelText, queryByLabelText, findByLabelText', async () => {
- const { getByLabelText, queryByLabelText, findByLabelText } = render();
+ render();
- expect(getByLabelText(BUTTON_LABEL).props.accessibilityLabel).toEqual(BUTTON_LABEL);
- const button = queryByLabelText(/button/g);
+ expect(screen.getByLabelText(BUTTON_LABEL).props.accessibilityLabel).toEqual(BUTTON_LABEL);
+ const button = screen.queryByLabelText(/button/g);
expect(button?.props.accessibilityLabel).toEqual(BUTTON_LABEL);
- expect(() => getByLabelText(NO_MATCHES_TEXT)).toThrow(
+ expect(() => screen.getByLabelText(NO_MATCHES_TEXT)).toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- expect(queryByLabelText(NO_MATCHES_TEXT)).toBeNull();
+ expect(screen.queryByLabelText(NO_MATCHES_TEXT)).toBeNull();
- expect(() => getByLabelText(TEXT_LABEL)).toThrow(getMultipleInstancesFoundMessage(TEXT_LABEL));
- expect(() => queryByLabelText(TEXT_LABEL)).toThrow(getMultipleInstancesFoundMessage(TEXT_LABEL));
+ expect(() => screen.getByLabelText(TEXT_LABEL)).toThrow(
+ getMultipleInstancesFoundMessage(TEXT_LABEL)
+ );
+ expect(() => screen.queryByLabelText(TEXT_LABEL)).toThrow(
+ getMultipleInstancesFoundMessage(TEXT_LABEL)
+ );
- const asyncButton = await findByLabelText(BUTTON_LABEL);
+ const asyncButton = await screen.findByLabelText(BUTTON_LABEL);
expect(asyncButton.props.accessibilityLabel).toEqual(BUTTON_LABEL);
- await expect(findByLabelText(NO_MATCHES_TEXT)).rejects.toThrow(
+ await expect(screen.findByLabelText(NO_MATCHES_TEXT)).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- await expect(findByLabelText(TEXT_LABEL)).rejects.toThrow(
+ await expect(screen.findByLabelText(TEXT_LABEL)).rejects.toThrow(
getMultipleInstancesFoundMessage(TEXT_LABEL)
);
});
test('getAllByLabelText, queryAllByLabelText, findAllByLabelText', async () => {
- const { getAllByLabelText, queryAllByLabelText, findAllByLabelText } = render();
+ render();
- expect(getAllByLabelText(TEXT_LABEL)).toHaveLength(2);
- expect(queryAllByLabelText(/cool/g)).toHaveLength(3);
+ expect(screen.getAllByLabelText(TEXT_LABEL)).toHaveLength(2);
+ expect(screen.queryAllByLabelText(/cool/g)).toHaveLength(3);
- expect(() => getAllByLabelText(NO_MATCHES_TEXT)).toThrow(
+ expect(() => screen.getAllByLabelText(NO_MATCHES_TEXT)).toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- expect(queryAllByLabelText(NO_MATCHES_TEXT)).toEqual([]);
+ expect(screen.queryAllByLabelText(NO_MATCHES_TEXT)).toEqual([]);
- await expect(findAllByLabelText(TEXT_LABEL)).resolves.toHaveLength(2);
- await expect(findAllByLabelText(NO_MATCHES_TEXT)).rejects.toThrow(
+ await expect(screen.findAllByLabelText(TEXT_LABEL)).resolves.toHaveLength(2);
+ await expect(screen.findAllByLabelText(NO_MATCHES_TEXT)).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
});
test('getAllByLabelText, queryAllByLabelText, findAllByLabelText with exact as false', async () => {
- const { getAllByLabelText, queryAllByLabelText, findAllByLabelText } = render();
+ render();
- expect(getAllByLabelText(TEXT_LABEL, { exact: false })).toHaveLength(2);
- expect(queryAllByLabelText(/cool/g, { exact: false })).toHaveLength(3);
+ expect(screen.getAllByLabelText(TEXT_LABEL, { exact: false })).toHaveLength(2);
+ expect(screen.queryAllByLabelText(/cool/g, { exact: false })).toHaveLength(3);
- expect(() => getAllByLabelText(NO_MATCHES_TEXT, { exact: false })).toThrow(
+ expect(() => screen.getAllByLabelText(NO_MATCHES_TEXT, { exact: false })).toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- expect(queryAllByLabelText(NO_MATCHES_TEXT, { exact: false })).toEqual([]);
+ expect(screen.queryAllByLabelText(NO_MATCHES_TEXT, { exact: false })).toEqual([]);
- await expect(findAllByLabelText(TEXT_LABEL, { exact: false })).resolves.toHaveLength(2);
- await expect(findAllByLabelText(NO_MATCHES_TEXT, { exact: false })).rejects.toThrow(
+ await expect(screen.findAllByLabelText(TEXT_LABEL, { exact: false })).resolves.toHaveLength(2);
+ await expect(screen.findAllByLabelText(NO_MATCHES_TEXT, { exact: false })).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
});
@@ -110,28 +114,28 @@ describe('findBy options deprecations', () => {
test('findByText queries warn on deprecated use of WaitForOptions', async () => {
const options = { timeout: 10 };
// mock implementation to avoid warning in the test suite
- const view = render();
- await expect(view.findByLabelText('Some Text', options)).rejects.toBeTruthy();
+ render();
+ await expect(screen.findByLabelText('Some Text', options)).rejects.toBeTruthy();
- setTimeout(() => view.rerender(), 20);
- await expect(view.findByLabelText('Some Text')).resolves.toBeTruthy();
+ setTimeout(() => screen.rerender(), 20);
+ await expect(screen.findByLabelText('Some Text')).resolves.toBeTruthy();
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Use of option "timeout"'));
}, 20000);
});
test('byLabelText queries support hidden option', () => {
- const { getByLabelText, queryByLabelText } = render(
+ render(
Hidden from accessibility
);
- expect(getByLabelText('hidden', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByLabelText('hidden', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByLabelText('hidden')).toBeFalsy();
- expect(queryByLabelText('hidden', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByLabelText('hidden', { includeHiddenElements: false }))
+ expect(screen.queryByLabelText('hidden')).toBeFalsy();
+ expect(screen.queryByLabelText('hidden', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByLabelText('hidden', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibility label: hidden
@@ -149,7 +153,7 @@ test('byLabelText queries support hidden option', () => {
});
test('getByLabelText supports aria-label', () => {
- const screen = render(
+ render(
<>
@@ -165,19 +169,19 @@ test('getByLabelText supports aria-label', () => {
});
test('getByLabelText supports accessibilityLabelledBy', () => {
- const { getByLabelText, getByTestId } = render(
+ render(
<>
Label for input
>
);
- expect(getByLabelText('Label for input')).toBe(getByTestId('textInput'));
- expect(getByLabelText(/input/)).toBe(getByTestId('textInput'));
+ expect(screen.getByLabelText('Label for input')).toBe(screen.getByTestId('textInput'));
+ expect(screen.getByLabelText(/input/)).toBe(screen.getByTestId('textInput'));
});
test('getByLabelText supports nested accessibilityLabelledBy', () => {
- const { getByLabelText, getByTestId } = render(
+ render(
<>
Label for input
@@ -186,12 +190,12 @@ test('getByLabelText supports nested accessibilityLabelledBy', () => {
>
);
- expect(getByLabelText('Label for input')).toBe(getByTestId('textInput'));
- expect(getByLabelText(/input/)).toBe(getByTestId('textInput'));
+ expect(screen.getByLabelText('Label for input')).toBe(screen.getByTestId('textInput'));
+ expect(screen.getByLabelText(/input/)).toBe(screen.getByTestId('textInput'));
});
test('getByLabelText supports aria-labelledby', () => {
- const screen = render(
+ render(
<>
Text Label
@@ -203,7 +207,7 @@ test('getByLabelText supports aria-labelledby', () => {
});
test('getByLabelText supports nested aria-labelledby', () => {
- const screen = render(
+ render(
<>
Nested Text Label
@@ -217,9 +221,9 @@ test('getByLabelText supports nested aria-labelledby', () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByLabelText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByLabelText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibility label: FOO
"
`);
- expect(() => view.getAllByLabelText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByLabelText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibility label: FOO
"
`);
- await expect(view.findByLabelText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByLabelText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibility label: FOO
"
`);
- await expect(view.findAllByLabelText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByLabelText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with accessibility label: FOO
{
test('includes element tree on error with less-helpful props stripped', () => {
- const { getByText } = render( null}>Some text);
+ render( null}>Some text);
- expect(() => getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
@@ -16,7 +16,7 @@ describe('printing element tree', () => {
});
test('prints helpful props but not others', () => {
- const { getByText } = render(
+ render(
{
);
- expect(() => getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
});
test('prints tree and filters props with getBy, getAllBy, findBy, findAllBy', async () => {
- const view = render();
+ render();
- expect(() => view.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
/>"
`);
- expect(() => view.getAllByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
/>"
`);
- await expect(view.findByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
/>"
`);
- await expect(view.findAllByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
});
test('only appends element tree on last failure with findBy', async () => {
- const { findByText } = render();
+ render();
jest.spyOn(screen, 'toJSON');
- await expect(findByText(/foo/)).rejects.toThrow();
+ await expect(screen.findByText(/foo/)).rejects.toThrow();
expect(screen.toJSON).toHaveBeenCalledTimes(1);
});
test('onTimeout with findBy receives error without element tree', async () => {
- const { findByText } = render();
+ render();
const onTimeout = jest.fn((_: Error) => new Error('Replacement error'));
await expect(() =>
- findByText(/foo/, undefined, { onTimeout })
+ screen.findByText(/foo/, undefined, { onTimeout })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Replacement error"`);
expect(onTimeout).toHaveBeenCalledTimes(1);
@@ -151,12 +151,12 @@ describe('printing element tree', () => {
});
test('onTimeout with findAllBy receives error without element tree', async () => {
- const { findAllByText } = render();
+ render();
const onTimeout = jest.fn((_: Error) => new Error('Replacement error'));
await expect(() =>
- findAllByText(/foo/, undefined, { onTimeout })
+ screen.findAllByText(/foo/, undefined, { onTimeout })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Replacement error"`);
expect(onTimeout).toHaveBeenCalledTimes(1);
@@ -167,7 +167,7 @@ describe('printing element tree', () => {
});
test('does not strip display: none from "style" prop, but does strip other styles', () => {
- const { getByText } = render(
+ render(
{
);
- expect(() => getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
@@ -198,13 +198,13 @@ describe('printing element tree', () => {
});
test('strips undefined values from accessibilityState', () => {
- const { getByText } = render(
+ render(
);
- expect(() => getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
});
test('strips undefined values from accessibilityValue', () => {
- const { getByText } = render(
+ render(
);
- expect(() => getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
});
test('does not render element tree when toJSON() returns null', () => {
- const view = render();
+ render();
jest.spyOn(screen, 'toJSON').mockImplementation(() => null);
- expect(() => view.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(
`"Unable to find an element with text: /foo/"`
);
});
diff --git a/src/queries/__tests__/placeholder-text.test.tsx b/src/queries/__tests__/placeholder-text.test.tsx
index 37850a36..c45980f0 100644
--- a/src/queries/__tests__/placeholder-text.test.tsx
+++ b/src/queries/__tests__/placeholder-text.test.tsx
@@ -1,6 +1,6 @@
import * as React from 'react';
-import { View, TextInput } from 'react-native';
-import { render } from '../..';
+import { TextInput, View } from 'react-native';
+import { render, screen } from '../..';
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
@@ -25,48 +25,46 @@ const Banana = () => (
);
test('getByPlaceholderText, queryByPlaceholderText', () => {
- const { getByPlaceholderText, queryByPlaceholderText } = render();
- const input = getByPlaceholderText(/custom/i);
+ render();
+ const input = screen.getByPlaceholderText(/custom/i);
expect(input.props.placeholder).toBe(PLACEHOLDER_FRESHNESS);
- const sameInput = getByPlaceholderText(PLACEHOLDER_FRESHNESS);
+ const sameInput = screen.getByPlaceholderText(PLACEHOLDER_FRESHNESS);
expect(sameInput.props.placeholder).toBe(PLACEHOLDER_FRESHNESS);
- expect(() => getByPlaceholderText('no placeholder')).toThrow(
+ expect(() => screen.getByPlaceholderText('no placeholder')).toThrow(
'Unable to find an element with placeholder: no placeholder'
);
- expect(queryByPlaceholderText(/add/i)).toBe(input);
- expect(queryByPlaceholderText('no placeholder')).toBeNull();
- expect(() => queryByPlaceholderText(/fresh/)).toThrow(
+ expect(screen.queryByPlaceholderText(/add/i)).toBe(input);
+ expect(screen.queryByPlaceholderText('no placeholder')).toBeNull();
+ expect(() => screen.queryByPlaceholderText(/fresh/)).toThrow(
'Found multiple elements with placeholder: /fresh/ '
);
});
test('getAllByPlaceholderText, queryAllByPlaceholderText', () => {
- const { getAllByPlaceholderText, queryAllByPlaceholderText } = render();
- const inputs = getAllByPlaceholderText(/fresh/i);
+ render();
+ const inputs = screen.getAllByPlaceholderText(/fresh/i);
expect(inputs).toHaveLength(2);
- expect(() => getAllByPlaceholderText('no placeholder')).toThrow(
+ expect(() => screen.getAllByPlaceholderText('no placeholder')).toThrow(
'Unable to find an element with placeholder: no placeholder'
);
- expect(queryAllByPlaceholderText(/fresh/i)).toEqual(inputs);
- expect(queryAllByPlaceholderText('no placeholder')).toHaveLength(0);
+ expect(screen.queryAllByPlaceholderText(/fresh/i)).toEqual(inputs);
+ expect(screen.queryAllByPlaceholderText('no placeholder')).toHaveLength(0);
});
test('byPlaceholderText queries support hidden option', () => {
- const { getByPlaceholderText, queryByPlaceholderText } = render(
-
- );
+ render();
- expect(getByPlaceholderText('hidden', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByPlaceholderText('hidden', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByPlaceholderText('hidden')).toBeFalsy();
- expect(queryByPlaceholderText('hidden', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByPlaceholderText('hidden', { includeHiddenElements: false }))
+ expect(screen.queryByPlaceholderText('hidden')).toBeFalsy();
+ expect(screen.queryByPlaceholderText('hidden', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByPlaceholderText('hidden', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with placeholder: hidden
@@ -82,15 +80,15 @@ test('byPlaceholderText queries support hidden option', () => {
});
test('byPlaceHolderText should return host component', () => {
- const { getByPlaceholderText } = render();
+ render();
- expect(getByPlaceholderText('placeholder').type).toBe('TextInput');
+ expect(screen.getByPlaceholderText('placeholder').type).toBe('TextInput');
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByPlaceholderText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByPlaceholderText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with placeholder: FOO
"
`);
- expect(() => view.getAllByPlaceholderText('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByPlaceholderText('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with placeholder: FOO
"
`);
- await expect(view.findByPlaceholderText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByPlaceholderText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with placeholder: FOO
"
`);
- await expect(view.findAllByPlaceholderText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByPlaceholderText('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with placeholder: FOO
{
test('matches using all value props', () => {
- const { getByRole, queryByRole } = render(
+ render(
{
);
expect(
- getByRole('adjustable', {
+ screen.getByRole('adjustable', {
value: { min: 0, max: 100, now: 50, text: '50%' },
})
).toBeTruthy();
expect(
- queryByRole('adjustable', {
+ screen.queryByRole('adjustable', {
value: { min: 1, max: 100, now: 50, text: '50%' },
})
).toBeFalsy();
expect(
- queryByRole('adjustable', {
+ screen.queryByRole('adjustable', {
value: { min: 0, max: 99, now: 50, text: '50%' },
})
).toBeFalsy();
expect(
- queryByRole('adjustable', {
+ screen.queryByRole('adjustable', {
value: { min: 0, max: 100, now: 45, text: '50%' },
})
).toBeFalsy();
expect(
- queryByRole('adjustable', {
+ screen.queryByRole('adjustable', {
value: { min: 0, max: 100, now: 50, text: '55%' },
})
).toBeFalsy();
});
test('matches using single value', () => {
- const { getByRole, queryByRole } = render(
+ render(
{
/>
);
- expect(getByRole('adjustable', { value: { min: 10 } })).toBeTruthy();
- expect(getByRole('adjustable', { value: { max: 20 } })).toBeTruthy();
- expect(getByRole('adjustable', { value: { now: 12 } })).toBeTruthy();
- expect(getByRole('adjustable', { value: { text: 'Hello' } })).toBeTruthy();
- expect(getByRole('adjustable', { value: { text: /hello/i } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { value: { min: 10 } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { value: { max: 20 } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { value: { now: 12 } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { value: { text: 'Hello' } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { value: { text: /hello/i } })).toBeTruthy();
- expect(queryByRole('adjustable', { value: { min: 11 } })).toBeFalsy();
- expect(queryByRole('adjustable', { value: { max: 19 } })).toBeFalsy();
- expect(queryByRole('adjustable', { value: { now: 15 } })).toBeFalsy();
- expect(queryByRole('adjustable', { value: { text: 'No' } })).toBeFalsy();
- expect(queryByRole('adjustable', { value: { text: /no/ } })).toBeFalsy();
+ expect(screen.queryByRole('adjustable', { value: { min: 11 } })).toBeFalsy();
+ expect(screen.queryByRole('adjustable', { value: { max: 19 } })).toBeFalsy();
+ expect(screen.queryByRole('adjustable', { value: { now: 15 } })).toBeFalsy();
+ expect(screen.queryByRole('adjustable', { value: { text: 'No' } })).toBeFalsy();
+ expect(screen.queryByRole('adjustable', { value: { text: /no/ } })).toBeFalsy();
});
test('matches using single value and other options', () => {
- const { getByRole } = render(
+ render(
{
);
- expect(getByRole('adjustable', { name: 'Hello', value: { min: 10 } })).toBeTruthy();
- expect(getByRole('adjustable', { disabled: true, value: { min: 10 } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { name: 'Hello', value: { min: 10 } })).toBeTruthy();
+ expect(screen.getByRole('adjustable', { disabled: true, value: { min: 10 } })).toBeTruthy();
- expect(() => getByRole('adjustable', { name: 'Hello', value: { min: 5 } }))
+ expect(() => screen.getByRole('adjustable', { name: 'Hello', value: { min: 5 } }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "adjustable", name: "Hello", min value: 5
@@ -98,7 +98,7 @@ describe('accessibility value', () => {
Hello
"
`);
- expect(() => getByRole('adjustable', { name: 'World', value: { min: 10 } }))
+ expect(() => screen.getByRole('adjustable', { name: 'World', value: { min: 10 } }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "adjustable", name: "World", min value: 10
@@ -121,7 +121,7 @@ describe('accessibility value', () => {
Hello
"
`);
- expect(() => getByRole('adjustable', { name: 'Hello', value: { min: 5 } }))
+ expect(() => screen.getByRole('adjustable', { name: 'Hello', value: { min: 5 } }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "adjustable", name: "Hello", min value: 5
@@ -144,7 +144,7 @@ describe('accessibility value', () => {
Hello
"
`);
- expect(() => getByRole('adjustable', { selected: true, value: { min: 10 } }))
+ expect(() => screen.getByRole('adjustable', { selected: true, value: { min: 10 } }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "adjustable", selected state: true, min value: 10
@@ -170,25 +170,25 @@ describe('accessibility value', () => {
});
test('supports "aria-valuemax" prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('slider', { value: { max: 10 } })).toBeTruthy();
expect(screen.queryByRole('slider', { value: { max: 20 } })).toBeNull();
});
test('supports "aria-valuemin" prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('slider', { value: { min: 20 } })).toBeTruthy();
expect(screen.queryByRole('slider', { value: { min: 30 } })).toBeNull();
});
test('supports "aria-valuenow" prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('slider', { value: { now: 30 } })).toBeTruthy();
expect(screen.queryByRole('slider', { value: { now: 10 } })).toBeNull();
});
test('supports "aria-valuetext" prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('slider', { value: { text: 'Hello World' } })).toBeTruthy();
expect(screen.getByRole('slider', { value: { text: /hello/i } })).toBeTruthy();
expect(screen.queryByRole('slider', { value: { text: 'Hello' } })).toBeNull();
@@ -196,7 +196,7 @@ describe('accessibility value', () => {
});
test('supports multiple "aria-value*" props', () => {
- const screen = render(
+ render(
);
expect(screen.getByRole('slider', { value: { now: 50, min: 0, max: 100 } })).toBeTruthy();
diff --git a/src/queries/__tests__/role.test.tsx b/src/queries/__tests__/role.test.tsx
index c339f472..1a54445d 100644
--- a/src/queries/__tests__/role.test.tsx
+++ b/src/queries/__tests__/role.test.tsx
@@ -1,14 +1,14 @@
import * as React from 'react';
import {
- TouchableOpacity,
- TouchableWithoutFeedback,
+ Button as RNButton,
+ Pressable,
Text,
TextInput,
+ TouchableOpacity,
+ TouchableWithoutFeedback,
View,
- Pressable,
- Button as RNButton,
} from 'react-native';
-import { render } from '../..';
+import { render, screen } from '../..';
const TEXT_LABEL = 'cool text';
@@ -41,44 +41,48 @@ const Section = () => (
);
test('getByRole, queryByRole, findByRole', async () => {
- const { getByRole, queryByRole, findByRole } = render();
+ render();
- expect(getByRole('button').props.accessibilityRole).toEqual('button');
- const button = queryByRole(/button/g);
+ expect(screen.getByRole('button').props.accessibilityRole).toEqual('button');
+ const button = screen.queryByRole(/button/g);
expect(button?.props.accessibilityRole).toEqual('button');
- expect(() => getByRole(NO_MATCHES_TEXT)).toThrow(getNoInstancesFoundMessage(NO_MATCHES_TEXT));
-
- expect(queryByRole(NO_MATCHES_TEXT)).toBeNull();
-
- expect(() => getByRole('link')).toThrow(getMultipleInstancesFoundMessage('link'));
- expect(() => queryByRole('link')).toThrow(getMultipleInstancesFoundMessage('link'));
-
- const asyncButton = await findByRole('button');
- expect(asyncButton.props.accessibilityRole).toEqual('button');
- await expect(findByRole(NO_MATCHES_TEXT)).rejects.toThrow(
+ expect(() => screen.getByRole(NO_MATCHES_TEXT)).toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
- await expect(findByRole('link')).rejects.toThrow(getMultipleInstancesFoundMessage('link'));
+
+ expect(screen.queryByRole(NO_MATCHES_TEXT)).toBeNull();
+
+ expect(() => screen.getByRole('link')).toThrow(getMultipleInstancesFoundMessage('link'));
+ expect(() => screen.queryByRole('link')).toThrow(getMultipleInstancesFoundMessage('link'));
+
+ const asyncButton = await screen.findByRole('button');
+ expect(asyncButton.props.accessibilityRole).toEqual('button');
+ await expect(screen.findByRole(NO_MATCHES_TEXT)).rejects.toThrow(
+ getNoInstancesFoundMessage(NO_MATCHES_TEXT)
+ );
+ await expect(screen.findByRole('link')).rejects.toThrow(getMultipleInstancesFoundMessage('link'));
});
test('getAllByRole, queryAllByRole, findAllByRole', async () => {
- const { getAllByRole, queryAllByRole, findAllByRole } = render();
+ render();
- expect(getAllByRole('link')).toHaveLength(2);
- expect(queryAllByRole(/ink/g)).toHaveLength(2);
+ expect(screen.getAllByRole('link')).toHaveLength(2);
+ expect(screen.queryAllByRole(/ink/g)).toHaveLength(2);
- expect(() => getAllByRole(NO_MATCHES_TEXT)).toThrow(getNoInstancesFoundMessage(NO_MATCHES_TEXT));
- expect(queryAllByRole(NO_MATCHES_TEXT)).toEqual([]);
+ expect(() => screen.getAllByRole(NO_MATCHES_TEXT)).toThrow(
+ getNoInstancesFoundMessage(NO_MATCHES_TEXT)
+ );
+ expect(screen.queryAllByRole(NO_MATCHES_TEXT)).toEqual([]);
- await expect(findAllByRole('link')).resolves.toHaveLength(2);
- await expect(findAllByRole(NO_MATCHES_TEXT)).rejects.toThrow(
+ await expect(screen.findAllByRole('link')).resolves.toHaveLength(2);
+ await expect(screen.findAllByRole(NO_MATCHES_TEXT)).rejects.toThrow(
getNoInstancesFoundMessage(NO_MATCHES_TEXT)
);
});
test('supports role prop', () => {
- const screen = render(
+ render(
<>
@@ -102,34 +106,34 @@ test('supports role prop', () => {
});
test('supports default View component "none" role', () => {
- const screen = render();
+ render();
expect(screen.getByRole('none').props.testID).toBe('view');
});
test('supports default Text component "text" role', () => {
- const screen = render();
+ render();
expect(screen.getByRole('text').props.testID).toBe('text');
});
test('supports default TextInput component "none" role', () => {
- const screen = render();
+ render();
expect(screen.getByRole('none').props.testID).toBe('text-input');
});
describe('supports name option', () => {
test('returns an element that has the corresponding role and a children with the name', () => {
- const { getByRole } = render(
+ render(
Save
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element that has the corresponding role when several children include the name', () => {
- const { getByRole } = render(
+ render(
Save
Save
@@ -137,22 +141,22 @@ describe('supports name option', () => {
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element that has the corresponding role and a children with a matching accessibilityLabel', () => {
- const { getByRole } = render(
+ render(
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element that has the corresponding role and a matching accessibilityLabel', () => {
- const { getByRole } = render(
+ render(
{
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element that has the corresponding role and a children with a matching aria-label', () => {
- const { getByRole } = render(
+ render(
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element that has the corresponding role and a matching aria-label', () => {
- const { getByRole } = render(
+ render(
{
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
+ expect(screen.getByRole('button', { name: 'Save' }).props.testID).toBe('target-button');
});
test('returns an element when the direct child is text', () => {
- const { getByRole, getByTestId } = render(
+ render(
About
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('header', { name: 'About' })).toBe(getByTestId('target-header'));
- expect(getByRole('header', { name: 'About' }).props.testID).toBe('target-header');
+ expect(screen.getByRole('header', { name: 'About' })).toBe(screen.getByTestId('target-header'));
+ expect(screen.getByRole('header', { name: 'About' }).props.testID).toBe('target-header');
});
test('returns an element with nested Text as children', () => {
- const { getByRole, getByTestId } = render(
+ render(
About
);
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('header', { name: 'About' })).toBe(getByTestId('parent'));
- expect(getByRole('header', { name: 'About' }).props.testID).toBe('parent');
+ expect(screen.getByRole('header', { name: 'About' })).toBe(screen.getByTestId('parent'));
+ expect(screen.getByRole('header', { name: 'About' }).props.testID).toBe('parent');
});
test('returns a header with an accessibilityLabel', () => {
- const { getByRole, getByTestId } = render(
-
- );
+ render();
// assert on the testId to be sure that the returned element is the one with the accessibilityRole
- expect(getByRole('header', { name: 'About' })).toBe(getByTestId('target-header'));
- expect(getByRole('header', { name: 'About' }).props.testID).toBe('target-header');
+ expect(screen.getByRole('header', { name: 'About' })).toBe(screen.getByTestId('target-header'));
+ expect(screen.getByRole('header', { name: 'About' }).props.testID).toBe('target-header');
});
});
describe('supports accessibility states', () => {
describe('disabled', () => {
test('returns a disabled element when required', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('button', { disabled: true })).toBeTruthy();
- expect(queryByRole('button', { disabled: false })).toBe(null);
+ expect(screen.getByRole('button', { disabled: true })).toBeTruthy();
+ expect(screen.queryByRole('button', { disabled: false })).toBe(null);
});
test('returns the correct element when only one matches all the requirements', () => {
- const { getByRole } = render(
+ render(
<>
{
>
);
- expect(getByRole('button', { name: 'Save', disabled: true }).props.testID).toBe('correct');
+ expect(
+ screen.getByRole('button', {
+ name: 'Save',
+ disabled: true,
+ }).props.testID
+ ).toBe('correct');
});
test('returns an implicitly enabled element', () => {
- const { getByRole, queryByRole } = render(
-
- );
+ render();
- expect(getByRole('button', { disabled: false })).toBeTruthy();
- expect(queryByRole('button', { disabled: true })).toBe(null);
+ expect(screen.getByRole('button', { disabled: false })).toBeTruthy();
+ expect(screen.queryByRole('button', { disabled: true })).toBe(null);
});
test('returns an explicitly enabled element', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('button', { disabled: false })).toBeTruthy();
- expect(queryByRole('button', { disabled: true })).toBe(null);
+ expect(screen.getByRole('button', { disabled: false })).toBeTruthy();
+ expect(screen.queryByRole('button', { disabled: true })).toBe(null);
});
test('does not return disabled elements when querying for non disabled', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('button', { disabled: false })).toBe(null);
+ expect(screen.queryByRole('button', { disabled: false })).toBe(null);
});
test('returns elements using the built-in disabled prop', () => {
- const { getByRole } = render(
+ render(
<>
Pressable
@@ -301,32 +306,32 @@ describe('supports accessibility states', () => {
>
);
- expect(getByRole('button', { name: 'Pressable', disabled: true })).toBeTruthy();
+ expect(screen.getByRole('button', { name: 'Pressable', disabled: true })).toBeTruthy();
expect(
- getByRole('button', {
+ screen.getByRole('button', {
name: 'TouchableWithoutFeedback',
disabled: true,
})
).toBeTruthy();
- expect(getByRole('button', { name: 'RNButton', disabled: true })).toBeTruthy();
+ expect(screen.getByRole('button', { name: 'RNButton', disabled: true })).toBeTruthy();
});
test('supports aria-disabled={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { disabled: true })).toBeTruthy();
expect(screen.queryByRole('button', { disabled: false })).toBeNull();
});
test('supports aria-disabled={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { disabled: false })).toBeTruthy();
expect(screen.queryByRole('button', { disabled: true })).toBeNull();
});
test('supports default aria-disabled prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { disabled: false })).toBeTruthy();
expect(screen.queryByRole('button', { disabled: true })).toBeNull();
});
@@ -334,16 +339,14 @@ describe('supports accessibility states', () => {
describe('selected', () => {
test('returns a selected element when required', () => {
- const { getByRole, queryByRole } = render(
-
- );
+ render();
- expect(getByRole('tab', { selected: true })).toBeTruthy();
- expect(queryByRole('tab', { selected: false })).toBe(null);
+ expect(screen.getByRole('tab', { selected: true })).toBeTruthy();
+ expect(screen.queryByRole('tab', { selected: false })).toBe(null);
});
test('returns the correct element when only one matches all the requirements', () => {
- const { getByRole } = render(
+ render(
<>
{
>
);
- expect(getByRole('tab', { name: 'Save', selected: true }).props.testID).toBe('correct');
+ expect(screen.getByRole('tab', { name: 'Save', selected: true }).props.testID).toBe(
+ 'correct'
+ );
});
test('returns an implicitly non selected element', () => {
- const { getByRole, queryByRole } = render(
-
- );
+ render();
- expect(getByRole('tab', { selected: false })).toBeTruthy();
- expect(queryByRole('tab', { selected: true })).toBe(null);
+ expect(screen.getByRole('tab', { selected: false })).toBeTruthy();
+ expect(screen.queryByRole('tab', { selected: true })).toBe(null);
});
test('returns an explicitly non selected element', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('tab', { selected: false })).toBeTruthy();
- expect(queryByRole('tab', { selected: true })).toBe(null);
+ expect(screen.getByRole('tab', { selected: false })).toBeTruthy();
+ expect(screen.queryByRole('tab', { selected: true })).toBe(null);
});
test('does not return selected elements when querying for non selected', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('tab', { selected: false })).toBe(null);
+ expect(screen.queryByRole('tab', { selected: false })).toBe(null);
});
test('supports aria-selected={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { selected: true })).toBeTruthy();
expect(screen.queryByRole('button', { selected: false })).toBeNull();
});
test('supports aria-selected={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { selected: false })).toBeTruthy();
expect(screen.queryByRole('button', { selected: true })).toBeNull();
});
test('supports default aria-selected prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { selected: false })).toBeTruthy();
expect(screen.queryByRole('button', { selected: true })).toBeNull();
});
@@ -414,35 +417,35 @@ describe('supports accessibility states', () => {
describe('checked', () => {
test('returns a checked element when required', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('checkbox', { checked: true })).toBeTruthy();
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
- expect(queryByRole('checkbox', { checked: 'mixed' })).toBe(null);
+ expect(screen.getByRole('checkbox', { checked: true })).toBeTruthy();
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: 'mixed' })).toBe(null);
});
it('returns `mixed` checkboxes', () => {
- const { queryByRole, getByRole } = render(
+ render(
);
- expect(getByRole('checkbox', { checked: 'mixed' })).toBeTruthy();
- expect(queryByRole('checkbox', { checked: true })).toBe(null);
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.getByRole('checkbox', { checked: 'mixed' })).toBeTruthy();
+ expect(screen.queryByRole('checkbox', { checked: true })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
});
it('does not return mixed checkboxes when querying for checked: true', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
});
test('returns the correct element when only one matches all the requirements', () => {
- const { getByRole } = render(
+ render(
<>
{
>
);
- expect(getByRole('checkbox', { name: 'Save', checked: true }).props.testID).toBe('correct');
+ expect(
+ screen.getByRole('checkbox', {
+ name: 'Save',
+ checked: true,
+ }).props.testID
+ ).toBe('correct');
});
test('does not return return as non checked an element with checked: undefined', () => {
- const { queryByRole } = render(
-
- );
+ render();
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
});
test('returns an explicitly non checked element', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('checkbox', { checked: false })).toBeTruthy();
- expect(queryByRole('checkbox', { checked: true })).toBe(null);
+ expect(screen.getByRole('checkbox', { checked: false })).toBeTruthy();
+ expect(screen.queryByRole('checkbox', { checked: true })).toBe(null);
});
test('does not return checked elements when querying for non checked', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
});
test('does not return mixed elements when querying for non checked', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('checkbox', { checked: false })).toBe(null);
+ expect(screen.queryByRole('checkbox', { checked: false })).toBe(null);
});
test('supports aria-checked={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { checked: true })).toBeTruthy();
expect(screen.queryByRole('button', { checked: false })).toBeNull();
expect(screen.queryByRole('button', { checked: 'mixed' })).toBeNull();
});
test('supports aria-checked={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { checked: false })).toBeTruthy();
expect(screen.queryByRole('button', { checked: true })).toBeNull();
expect(screen.queryByRole('button', { checked: 'mixed' })).toBeNull();
});
test('supports aria-checked="mixed prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { checked: 'mixed' })).toBeTruthy();
expect(screen.queryByRole('button', { checked: true })).toBeNull();
expect(screen.queryByRole('button', { checked: false })).toBeNull();
});
test('supports default aria-selected prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button')).toBeTruthy();
expect(screen.queryByRole('button', { checked: true })).toBeNull();
expect(screen.queryByRole('button', { checked: false })).toBeNull();
@@ -534,16 +540,14 @@ describe('supports accessibility states', () => {
describe('busy', () => {
test('returns a busy element when required', () => {
- const { getByRole, queryByRole } = render(
-
- );
+ render();
- expect(getByRole('button', { busy: true })).toBeTruthy();
- expect(queryByRole('button', { busy: false })).toBe(null);
+ expect(screen.getByRole('button', { busy: true })).toBeTruthy();
+ expect(screen.queryByRole('button', { busy: false })).toBe(null);
});
test('returns the correct element when only one matches all the requirements', () => {
- const { getByRole } = render(
+ render(
<>
{
>
);
- expect(getByRole('button', { name: 'Save', busy: true }).props.testID).toBe('correct');
+ expect(screen.getByRole('button', { name: 'Save', busy: true }).props.testID).toBe('correct');
});
test('returns an implicitly non busy element', () => {
- const { getByRole, queryByRole } = render(
-
- );
+ render();
- expect(getByRole('button', { busy: false })).toBeTruthy();
- expect(queryByRole('button', { busy: true })).toBe(null);
+ expect(screen.getByRole('button', { busy: false })).toBeTruthy();
+ expect(screen.queryByRole('button', { busy: true })).toBe(null);
});
test('returns an explicitly non busy element', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('button', { busy: false })).toBeTruthy();
- expect(queryByRole('button', { busy: true })).toBe(null);
+ expect(screen.getByRole('button', { busy: false })).toBeTruthy();
+ expect(screen.queryByRole('button', { busy: true })).toBe(null);
});
test('does not return busy elements when querying for non busy', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('button', { selected: false })).toBe(null);
+ expect(screen.queryByRole('button', { selected: false })).toBe(null);
});
test('supports aria-busy={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { busy: true })).toBeTruthy();
expect(screen.queryByRole('button', { busy: false })).toBeNull();
});
test('supports aria-busy={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { busy: false })).toBeTruthy();
expect(screen.queryByRole('button', { busy: true })).toBeNull();
});
test('supports default aria-busy prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { busy: false })).toBeTruthy();
expect(screen.queryByRole('button', { busy: true })).toBeNull();
});
@@ -614,16 +616,16 @@ describe('supports accessibility states', () => {
describe('expanded', () => {
test('returns a expanded element when required', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('button', { expanded: true })).toBeTruthy();
- expect(queryByRole('button', { expanded: false })).toBe(null);
+ expect(screen.getByRole('button', { expanded: true })).toBeTruthy();
+ expect(screen.queryByRole('button', { expanded: false })).toBe(null);
});
test('returns the correct element when only one matches all the requirements', () => {
- const { getByRole } = render(
+ render(
<>
{
>
);
- expect(getByRole('button', { name: 'Save', expanded: true }).props.testID).toBe('correct');
+ expect(
+ screen.getByRole('button', {
+ name: 'Save',
+ expanded: true,
+ }).props.testID
+ ).toBe('correct');
});
test('does not return return as non expanded an element with expanded: undefined', () => {
- const { queryByRole } = render(
-
- );
+ render();
- expect(queryByRole('button', { expanded: false })).toBe(null);
+ expect(screen.queryByRole('button', { expanded: false })).toBe(null);
});
test('returns an explicitly non expanded element', () => {
- const { getByRole, queryByRole } = render(
+ render(
);
- expect(getByRole('button', { expanded: false })).toBeTruthy();
- expect(queryByRole('button', { expanded: true })).toBe(null);
+ expect(screen.getByRole('button', { expanded: false })).toBeTruthy();
+ expect(screen.queryByRole('button', { expanded: true })).toBe(null);
});
test('does not return expanded elements when querying for non expanded', () => {
- const { queryByRole } = render(
+ render(
);
- expect(queryByRole('button', { expanded: false })).toBe(null);
+ expect(screen.queryByRole('button', { expanded: false })).toBe(null);
});
test('supports aria-expanded={true} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { expanded: true })).toBeTruthy();
expect(screen.queryByRole('button', { expanded: false })).toBeNull();
});
test('supports aria-expanded={false} prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button', { expanded: false })).toBeTruthy();
expect(screen.queryByRole('button', { expanded: true })).toBeNull();
});
test('supports default aria-expanded prop', () => {
- const screen = render();
+ render();
expect(screen.getByRole('button')).toBeTruthy();
expect(screen.queryByRole('button', { expanded: true })).toBeNull();
expect(screen.queryByRole('button', { expanded: false })).toBeNull();
@@ -693,7 +698,7 @@ describe('supports accessibility states', () => {
});
test('ignores non queried accessibilityState', () => {
- const { getByRole, queryByRole } = render(
+ render(
{
);
expect(
- getByRole('button', {
+ screen.getByRole('button', {
name: 'Save',
disabled: true,
})
).toBeTruthy();
expect(
- queryByRole('button', {
+ screen.queryByRole('button', {
name: 'Save',
disabled: false,
})
@@ -721,7 +726,7 @@ describe('supports accessibility states', () => {
});
test('matches an element combining all the options', () => {
- const { getByRole } = render(
+ render(
{
);
expect(
- getByRole('button', {
+ screen.getByRole('button', {
name: 'Save',
disabled: true,
selected: true,
@@ -751,9 +756,9 @@ describe('supports accessibility states', () => {
describe('error messages', () => {
test('gives a descriptive error message when querying with a role', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('button')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByRole('button')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button"
"
@@ -761,9 +766,9 @@ describe('error messages', () => {
});
test('gives a descriptive error message when querying with a role and a name', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('button', { name: 'Save' })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByRole('button', { name: 'Save' })).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button", name: "Save"
"
@@ -771,9 +776,9 @@ describe('error messages', () => {
});
test('gives a descriptive error message when querying with a role, a name and accessibility state', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('button', { name: 'Save', disabled: true }))
+ expect(() => screen.getByRole('button', { name: 'Save', disabled: true }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button", name: "Save", disabled state: true
@@ -782,9 +787,9 @@ describe('error messages', () => {
});
test('gives a descriptive error message when querying with a role, a name and several accessibility state', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('button', { name: 'Save', disabled: true, selected: true }))
+ expect(() => screen.getByRole('button', { name: 'Save', disabled: true, selected: true }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button", name: "Save", disabled state: true, selected state: true
@@ -793,9 +798,10 @@ describe('error messages', () => {
});
test('gives a descriptive error message when querying with a role and an accessibility state', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('button', { disabled: true })).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByRole('button', { disabled: true }))
+ .toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button", disabled state: true
"
@@ -803,9 +809,9 @@ describe('error messages', () => {
});
test('gives a descriptive error message when querying with a role and an accessibility value', () => {
- const { getByRole } = render();
+ render();
- expect(() => getByRole('adjustable', { value: { min: 1 } }))
+ expect(() => screen.getByRole('adjustable', { value: { min: 1 } }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "adjustable", min value: 1
@@ -813,7 +819,7 @@ describe('error messages', () => {
`);
expect(() =>
- getByRole('adjustable', {
+ screen.getByRole('adjustable', {
value: { min: 1, max: 2, now: 1, text: /hello/ },
})
).toThrowErrorMatchingInlineSnapshot(`
@@ -825,17 +831,17 @@ describe('error messages', () => {
});
test('byRole queries support hidden option', () => {
- const { getByRole, queryByRole } = render(
+ render(
Hidden from accessibility
);
- expect(getByRole('button', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByRole('button', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByRole('button')).toBeFalsy();
- expect(queryByRole('button', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByRole('button', { includeHiddenElements: false }))
+ expect(screen.queryByRole('button')).toBeFalsy();
+ expect(screen.queryByRole('button', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByRole('button', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "button"
@@ -857,37 +863,37 @@ test('byRole queries support hidden option', () => {
describe('matches only accessible elements', () => {
test('matches elements with accessible={true}', () => {
- const { queryByRole } = render(
+ render(
Action
);
- expect(queryByRole('menu', { name: 'Action' })).toBeTruthy();
+ expect(screen.queryByRole('menu', { name: 'Action' })).toBeTruthy();
});
test('ignores elements with accessible={false}', () => {
- const { queryByRole } = render(
+ render(
Action
);
- expect(queryByRole('button', { name: 'Action' })).toBeFalsy();
+ expect(screen.queryByRole('button', { name: 'Action' })).toBeFalsy();
});
test('ignores elements with accessible={undefined} and that are implicitely not accessible', () => {
- const { queryByRole } = render(
+ render(
Action
);
- expect(queryByRole('menu', { name: 'Action' })).toBeFalsy();
+ expect(screen.queryByRole('menu', { name: 'Action' })).toBeFalsy();
});
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByRole('link')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByRole('link')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "link"
"
`);
- expect(() => view.getAllByRole('link')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByRole('link')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "link"
"
`);
- await expect(view.findByRole('link')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByRole('link')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "link"
"
`);
- await expect(view.findAllByRole('link')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByRole('link')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with role: "link"
(
const MyComponent = (_props: { testID?: string }) => My Component;
test('getByTestId returns only native elements', () => {
- const { getByTestId, getAllByTestId } = render(
+ render(
Text
@@ -35,26 +35,26 @@ test('getByTestId returns only native elements', () => {
);
- expect(getByTestId('text')).toBeTruthy();
- expect(getByTestId('textInput')).toBeTruthy();
- expect(getByTestId('view')).toBeTruthy();
- expect(getByTestId('button')).toBeTruthy();
+ expect(screen.getByTestId('text')).toBeTruthy();
+ expect(screen.getByTestId('textInput')).toBeTruthy();
+ expect(screen.getByTestId('view')).toBeTruthy();
+ expect(screen.getByTestId('button')).toBeTruthy();
- expect(getAllByTestId('text')).toHaveLength(1);
- expect(getAllByTestId('textInput')).toHaveLength(1);
- expect(getAllByTestId('view')).toHaveLength(1);
- expect(getAllByTestId('button')).toHaveLength(1);
+ expect(screen.getAllByTestId('text')).toHaveLength(1);
+ expect(screen.getAllByTestId('textInput')).toHaveLength(1);
+ expect(screen.getAllByTestId('view')).toHaveLength(1);
+ expect(screen.getAllByTestId('button')).toHaveLength(1);
- expect(() => getByTestId('myComponent')).toThrow(
+ expect(() => screen.getByTestId('myComponent')).toThrow(
'Unable to find an element with testID: myComponent'
);
- expect(() => getAllByTestId('myComponent')).toThrow(
+ expect(() => screen.getAllByTestId('myComponent')).toThrow(
'Unable to find an element with testID: myComponent'
);
});
test('supports a regex matcher', () => {
- const { getByTestId, getAllByTestId } = render(
+ render(
Text
@@ -64,58 +64,58 @@ test('supports a regex matcher', () => {
);
- expect(getByTestId(/view/)).toBeTruthy();
- expect(getAllByTestId(/text/)).toHaveLength(2);
+ expect(screen.getByTestId(/view/)).toBeTruthy();
+ expect(screen.getAllByTestId(/text/)).toHaveLength(2);
});
test('getByTestId, queryByTestId', () => {
- const { getByTestId, queryByTestId } = render();
- const component = getByTestId('bananaFresh');
+ render();
+ const component = screen.getByTestId('bananaFresh');
expect(component.props.children).toBe('not fresh');
- expect(() => getByTestId('InExistent')).toThrow(
+ expect(() => screen.getByTestId('InExistent')).toThrow(
'Unable to find an element with testID: InExistent'
);
- expect(getByTestId('bananaFresh')).toBe(component);
- expect(queryByTestId('InExistent')).toBeNull();
+ expect(screen.getByTestId('bananaFresh')).toBe(component);
+ expect(screen.queryByTestId('InExistent')).toBeNull();
- expect(() => getByTestId('duplicateText')).toThrow(
+ expect(() => screen.getByTestId('duplicateText')).toThrow(
'Found multiple elements with testID: duplicateText'
);
- expect(() => queryByTestId('duplicateText')).toThrow(
+ expect(() => screen.queryByTestId('duplicateText')).toThrow(
'Found multiple elements with testID: duplicateText'
);
});
test('getAllByTestId, queryAllByTestId', () => {
- const { getAllByTestId, queryAllByTestId } = render();
- const textElements = getAllByTestId('duplicateText');
+ render();
+ const textElements = screen.getAllByTestId('duplicateText');
expect(textElements.length).toBe(2);
expect(textElements[0].props.children).toBe('First Text');
expect(textElements[1].props.children).toBe('Second Text');
- expect(() => getAllByTestId('nonExistentTestId')).toThrow(
+ expect(() => screen.getAllByTestId('nonExistentTestId')).toThrow(
'Unable to find an element with testID: nonExistentTestId'
);
- const queriedTextElements = queryAllByTestId('duplicateText');
+ const queriedTextElements = screen.queryAllByTestId('duplicateText');
expect(queriedTextElements.length).toBe(2);
expect(queriedTextElements[0]).toBe(textElements[0]);
expect(queriedTextElements[1]).toBe(textElements[1]);
- expect(queryAllByTestId('nonExistentTestId')).toHaveLength(0);
+ expect(screen.queryAllByTestId('nonExistentTestId')).toHaveLength(0);
});
test('findByTestId and findAllByTestId work asynchronously', async () => {
const options = { timeout: 10 }; // Short timeout so that this test runs quickly
- const { rerender, findByTestId, findAllByTestId } = render();
- await expect(findByTestId('aTestId', {}, options)).rejects.toBeTruthy();
- await expect(findAllByTestId('aTestId', {}, options)).rejects.toBeTruthy();
+ render();
+ await expect(screen.findByTestId('aTestId', {}, options)).rejects.toBeTruthy();
+ await expect(screen.findAllByTestId('aTestId', {}, options)).rejects.toBeTruthy();
setTimeout(
() =>
- rerender(
+ screen.rerender(
Some Text
@@ -125,22 +125,22 @@ test('findByTestId and findAllByTestId work asynchronously', async () => {
20
);
- await expect(findByTestId('aTestId')).resolves.toBeTruthy();
- await expect(findAllByTestId('aTestId')).resolves.toHaveLength(1);
+ await expect(screen.findByTestId('aTestId')).resolves.toBeTruthy();
+ await expect(screen.findAllByTestId('aTestId')).resolves.toHaveLength(1);
}, 20000);
test('byTestId queries support hidden option', () => {
- const { getByTestId, queryByTestId } = render(
+ render(
Hidden from accessibility
);
- expect(getByTestId('hidden', { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByTestId('hidden', { includeHiddenElements: true })).toBeTruthy();
- expect(queryByTestId('hidden')).toBeFalsy();
- expect(queryByTestId('hidden', { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByTestId('hidden', { includeHiddenElements: false }))
+ expect(screen.queryByTestId('hidden')).toBeFalsy();
+ expect(screen.queryByTestId('hidden', { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByTestId('hidden', { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: hidden
@@ -158,9 +158,9 @@ test('byTestId queries support hidden option', () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByTestId('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByTestId('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: FOO
"
`);
- expect(() => view.getAllByTestId('FOO')).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByTestId('FOO')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: FOO
"
`);
- await expect(view.findByTestId('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByTestId('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: FOO
"
`);
- await expect(view.findAllByTestId('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByTestId('FOO')).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with testID: FOO
{
- const { getByText } = render(Hello World);
- expect(getByText('Hello World').props.testID).toBe('text');
+ render(Hello World);
+ expect(screen.getByText('Hello World').props.testID).toBe('text');
});
test('byText matches inner nested text', () => {
- const { getByText } = render(
+ render(
Hello World
);
- expect(getByText('Hello World').props.testID).toBe('inner');
+ expect(screen.getByText('Hello World').props.testID).toBe('inner');
});
test('byText matches accross multiple texts', () => {
- const { getByText } = render(
+ render(
Hello World
);
- expect(getByText('Hello World').props.testID).toBe('outer');
+ expect(screen.getByText('Hello World').props.testID).toBe('outer');
});
type MyButtonProps = {
@@ -53,25 +53,27 @@ const Banana = () => {
type ChildrenProps = { children: React.ReactNode };
test('getByText, queryByText', () => {
- const { getByText, queryByText } = render();
- const button = getByText(/change/i);
+ render();
+ const button = screen.getByText(/change/i);
expect(button.props.children).toBe('Change freshness!');
- const sameButton = getByText('not fresh');
+ const sameButton = screen.getByText('not fresh');
expect(sameButton.props.children).toBe('not fresh');
- expect(() => getByText('InExistent')).toThrow('Unable to find an element with text: InExistent');
+ expect(() => screen.getByText('InExistent')).toThrow(
+ 'Unable to find an element with text: InExistent'
+ );
- const zeroText = getByText('0');
+ const zeroText = screen.getByText('0');
- expect(queryByText(/change/i)).toBe(button);
- expect(queryByText('InExistent')).toBeNull();
- expect(() => queryByText(/fresh/)).toThrow('Found multiple elements with text: /fresh/');
- expect(queryByText('0')).toBe(zeroText);
+ expect(screen.queryByText(/change/i)).toBe(button);
+ expect(screen.queryByText('InExistent')).toBeNull();
+ expect(() => screen.queryByText(/fresh/)).toThrow('Found multiple elements with text: /fresh/');
+ expect(screen.queryByText('0')).toBe(zeroText);
});
-test('getByText, queryByText with children as Array', () => {
+test('getByText, screen.queryByText with children as Array', () => {
type BananaCounterProps = { numBananas: number };
const BananaCounter = ({ numBananas }: BananaCounterProps) => (
There are {numBananas} bananas in the bunch
@@ -85,34 +87,34 @@ test('getByText, queryByText with children as Array', () => {
);
- const { getByText } = render();
+ render();
- const threeBananaBunch = getByText('There are 3 bananas in the bunch');
+ const threeBananaBunch = screen.getByText('There are 3 bananas in the bunch');
expect(threeBananaBunch.props.children).toEqual(['There are ', 3, ' bananas in the bunch']);
});
test('getAllByText, queryAllByText', () => {
- const { getAllByText, queryAllByText } = render();
- const buttons = getAllByText(/fresh/i);
+ render();
+ const buttons = screen.getAllByText(/fresh/i);
expect(buttons).toHaveLength(3);
- expect(() => getAllByText('InExistent')).toThrow(
+ expect(() => screen.getAllByText('InExistent')).toThrow(
'Unable to find an element with text: InExistent'
);
- expect(queryAllByText(/fresh/i)).toEqual(buttons);
- expect(queryAllByText('InExistent')).toHaveLength(0);
+ expect(screen.queryAllByText(/fresh/i)).toEqual(buttons);
+ expect(screen.queryAllByText('InExistent')).toHaveLength(0);
});
test('findByText queries work asynchronously', async () => {
const options = { timeout: 10 }; // Short timeout so that this test runs quickly
- const { rerender, findByText, findAllByText } = render();
- await expect(findByText('Some Text', {}, options)).rejects.toBeTruthy();
- await expect(findAllByText('Some Text', {}, options)).rejects.toBeTruthy();
+ render();
+ await expect(screen.findByText('Some Text', {}, options)).rejects.toBeTruthy();
+ await expect(screen.findAllByText('Some Text', {}, options)).rejects.toBeTruthy();
setTimeout(
() =>
- rerender(
+ screen.rerender(
Some Text
@@ -120,8 +122,8 @@ test('findByText queries work asynchronously', async () => {
20
);
- await expect(findByText('Some Text')).resolves.toBeTruthy();
- await expect(findAllByText('Some Text')).resolves.toHaveLength(1);
+ await expect(screen.findByText('Some Text')).resolves.toBeTruthy();
+ await expect(screen.findAllByText('Some Text')).resolves.toHaveLength(1);
}, 20000);
test('getByText works properly with custom text component', () => {
@@ -142,6 +144,7 @@ test('getByText works properly with custom text container', () => {
function MyText({ children }: ChildrenProps) {
return {children};
}
+
function BoldText({ children }: ChildrenProps) {
return {children};
}
@@ -201,7 +204,7 @@ test('queryByText not found', () => {
});
test('*ByText matches text across multiple nested Text', () => {
- const { getByText } = render(
+ render(
Hello{' '}
@@ -211,7 +214,7 @@ test('*ByText matches text across multiple nested Text', () => {
);
- expect(getByText('Hello World!')).toBeTruthy();
+ expect(screen.getByText('Hello World!')).toBeTruthy();
});
test('queryByText with nested Text components return the closest Text', () => {
@@ -221,8 +224,8 @@ test('queryByText with nested Text components return the closest Text', () => {
);
- const { queryByText } = render();
- expect(queryByText('My text', { exact: false })?.props.nativeID).toBe('2');
+ render();
+ expect(screen.queryByText('My text', { exact: false })?.props.nativeID).toBe('2');
});
test('queryByText with nested Text components each with text return the lowest one', () => {
@@ -233,9 +236,9 @@ test('queryByText with nested Text components each with text return the lowest o
);
- const { queryByText } = render();
+ render();
- expect(queryByText('My text', { exact: false })?.props.nativeID).toBe('2');
+ expect(screen.queryByText('My text', { exact: false })?.props.nativeID).toBe('2');
});
test('queryByText nested deep in ', () => {
@@ -253,7 +256,7 @@ test('queryByText nested deep in ', () => {
});
test('queryByText with nested Text components: not-exact text match returns the most deeply nested common component', () => {
- const { queryByText: queryByTextFirstCase } = render(
+ render(
bob
My
@@ -261,15 +264,15 @@ test('queryByText with nested Text components: not-exact text match returns the
);
- const { queryByText: queryByTextSecondCase } = render(
+ render(
bob
My text for test
);
- expect(queryByTextFirstCase('My text')).toBe(null);
- expect(queryByTextSecondCase('My text', { exact: false })?.props.nativeID).toBe('2');
+ expect(screen.queryByText('My text')).toBe(null);
+ expect(screen.queryByText('My text', { exact: false })?.props.nativeID).toBe('2');
});
test('queryAllByText does not match several times the same text', () => {
@@ -297,68 +300,68 @@ test('queryAllByText matches all the matching nodes', () => {
describe('supports TextMatch options', () => {
test('getByText, getAllByText', () => {
- const { getByText, getAllByText } = render(
+ render(
Text and details
);
- expect(getByText('details', { exact: false })).toBeTruthy();
- expect(getAllByText('detail', { exact: false })).toHaveLength(2);
+ expect(screen.getByText('details', { exact: false })).toBeTruthy();
+ expect(screen.getAllByText('detail', { exact: false })).toHaveLength(2);
});
test('getByPlaceholderText, getAllByPlaceholderText', () => {
- const { getByPlaceholderText, getAllByPlaceholderText } = render(
+ render(
);
- expect(getByPlaceholderText('details', { exact: false })).toBeTruthy();
- expect(getAllByPlaceholderText('detail', { exact: false })).toHaveLength(2);
+ expect(screen.getByPlaceholderText('details', { exact: false })).toBeTruthy();
+ expect(screen.getAllByPlaceholderText('detail', { exact: false })).toHaveLength(2);
});
test('getByDisplayValue, getAllByDisplayValue', () => {
- const { getByDisplayValue, getAllByDisplayValue } = render(
+ render(
);
- expect(getByDisplayValue('details', { exact: false })).toBeTruthy();
- expect(getAllByDisplayValue('detail', { exact: false })).toHaveLength(2);
+ expect(screen.getByDisplayValue('details', { exact: false })).toBeTruthy();
+ expect(screen.getAllByDisplayValue('detail', { exact: false })).toHaveLength(2);
});
test('getByTestId, getAllByTestId', () => {
- const { getByTestId, getAllByTestId } = render(
+ render(
);
- expect(getByTestId('id', { exact: false })).toBeTruthy();
- expect(getAllByTestId('test', { exact: false })).toHaveLength(2);
+ expect(screen.getByTestId('id', { exact: false })).toBeTruthy();
+ expect(screen.getAllByTestId('test', { exact: false })).toHaveLength(2);
});
test('with TextMatch option exact === false text search is NOT case sensitive', () => {
- const { getByText, getAllByText } = render(
+ render(
Text and details
);
- expect(getByText('DeTaIlS', { exact: false })).toBeTruthy();
- expect(getAllByText('detail', { exact: false })).toHaveLength(2);
+ expect(screen.getByText('DeTaIlS', { exact: false })).toBeTruthy();
+ expect(screen.getAllByText('detail', { exact: false })).toHaveLength(2);
});
});
describe('Supports normalization', () => {
test('trims and collapses whitespace by default', () => {
- const { getByText } = render(
+ render(
{` Text and
@@ -366,21 +369,21 @@ describe('Supports normalization', () => {
);
- expect(getByText('Text and whitespace')).toBeTruthy();
+ expect(screen.getByText('Text and whitespace')).toBeTruthy();
});
test('trim and collapseWhitespace is customizable by getDefaultNormalizer param', () => {
const testTextWithWhitespace = ` Text and
whitespace`;
- const { getByText } = render(
+ render(
{testTextWithWhitespace}
);
expect(
- getByText(testTextWithWhitespace, {
+ screen.getByText(testTextWithWhitespace, {
normalizer: getDefaultNormalizer({
trim: false,
collapseWhitespace: false,
@@ -392,38 +395,38 @@ describe('Supports normalization', () => {
test('normalizer function is customisable', () => {
const testText = 'A TO REMOVE text';
const normalizerFn = (textToNormalize: string) => textToNormalize.replace('TO REMOVE ', '');
- const { getByText } = render(
+ render(
{testText}
);
- expect(getByText('A text', { normalizer: normalizerFn })).toBeTruthy();
+ expect(screen.getByText('A text', { normalizer: normalizerFn })).toBeTruthy();
});
});
test('getByText and queryByText work properly with text nested in React.Fragment', () => {
- const { getByText, queryByText } = render(
+ render(
<>Hello>
);
- expect(getByText('Hello')).toBeTruthy();
- expect(queryByText('Hello')).not.toBeNull();
+ expect(screen.getByText('Hello')).toBeTruthy();
+ expect(screen.queryByText('Hello')).not.toBeNull();
});
test('getByText and queryByText work properly with text partially nested in React.Fragment', () => {
- const { getByText, queryByText } = render(
+ render(
He<>llo>
);
- expect(getByText('Hello')).toBeTruthy();
- expect(queryByText('Hello')).not.toBeNull();
+ expect(screen.getByText('Hello')).toBeTruthy();
+ expect(screen.queryByText('Hello')).not.toBeNull();
});
test('getByText and queryByText work properly with multiple nested fragments', () => {
- const { getByText, queryByText } = render(
+ render(
He
<>
@@ -431,46 +434,44 @@ test('getByText and queryByText work properly with multiple nested fragments', (
>
);
- expect(getByText('Hello')).toBeTruthy();
- expect(queryByText('Hello')).not.toBeNull();
+ expect(screen.getByText('Hello')).toBeTruthy();
+ expect(screen.queryByText('Hello')).not.toBeNull();
});
test('getByText and queryByText work with newlines', () => {
const textWithNewLines = 'Line 1\nLine 2';
- const { getByText, queryByText } = render({textWithNewLines});
- expect(getByText(textWithNewLines)).toBeTruthy();
- expect(queryByText(textWithNewLines)).toBeTruthy();
+ render({textWithNewLines});
+ expect(screen.getByText(textWithNewLines)).toBeTruthy();
+ expect(screen.queryByText(textWithNewLines)).toBeTruthy();
});
test('getByText and queryByText work with tabs', () => {
const textWithTabs = 'Line 1\tLine 2';
- const { getByText, queryByText } = render({textWithTabs});
- expect(getByText(textWithTabs)).toBeTruthy();
- expect(queryByText(textWithTabs)).toBeTruthy();
+ render({textWithTabs});
+ expect(screen.getByText(textWithTabs)).toBeTruthy();
+ expect(screen.queryByText(textWithTabs)).toBeTruthy();
});
test('getByText searches for text within itself', () => {
- const { getByText } = render(Hello);
- const textNode = within(getByText('Hello'));
+ render(Hello);
+ const textNode = within(screen.getByText('Hello'));
expect(textNode.getByText('Hello')).toBeTruthy();
});
test('getByText searches for text within self host element', () => {
- const { getByTestId } = render(Hello);
- const textNode = within(getByTestId('subject'));
+ render(Hello);
+ const textNode = within(screen.getByTestId('subject'));
expect(textNode.getByText('Hello')).toBeTruthy();
});
test('byText support hidden option', () => {
- const { getByText, queryByText } = render(
- Hidden from accessibility
- );
+ render(Hidden from accessibility);
- expect(getByText(/hidden/i, { includeHiddenElements: true })).toBeTruthy();
+ expect(screen.getByText(/hidden/i, { includeHiddenElements: true })).toBeTruthy();
- expect(queryByText(/hidden/i)).toBeFalsy();
- expect(queryByText(/hidden/i, { includeHiddenElements: false })).toBeFalsy();
- expect(() => getByText(/hidden/i, { includeHiddenElements: false }))
+ expect(screen.queryByText(/hidden/i)).toBeFalsy();
+ expect(screen.queryByText(/hidden/i, { includeHiddenElements: false })).toBeFalsy();
+ expect(() => screen.getByText(/hidden/i, { includeHiddenElements: false }))
.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /hidden/i
@@ -487,9 +488,9 @@ test('byText support hidden option', () => {
});
test('error message renders the element tree, preserving only helpful props', async () => {
- const view = render();
+ render();
- expect(() => view.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
"
`);
- expect(() => view.getAllByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
+ expect(() => screen.getAllByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
"
`);
- await expect(view.findByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
"
`);
- await expect(view.findAllByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
+ await expect(screen.findAllByText(/foo/)).rejects.toThrowErrorMatchingInlineSnapshot(`
"Unable to find an element with text: /foo/
{
- const { getByText } = render(hello);
- expect(getByText('hello').type).toBe('Text');
+ render(hello);
+ expect(screen.getByText('hello').type).toBe('Text');
});
diff --git a/src/user-event/__tests__/clear.test.tsx b/src/user-event/__tests__/clear.test.tsx
index 74626c5b..6fed7490 100644
--- a/src/user-event/__tests__/clear.test.tsx
+++ b/src/user-event/__tests__/clear.test.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
-import { View, TextInput, TextInputProps } from 'react-native';
-import { createEventLogger } from '../../test-utils/events';
-import { render, userEvent } from '../..';
+import { TextInput, TextInputProps, View } from 'react-native';
+import { createEventLogger } from '../../test-utils';
+import { render, userEvent, screen } from '../..';
beforeEach(() => {
jest.useRealTimers();
@@ -10,7 +10,7 @@ beforeEach(() => {
function renderTextInputWithToolkit(props: TextInputProps = {}) {
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
{
it('works when not all events have handlers', async () => {
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
{
});
it('does NOT work on View', async () => {
- const screen = render();
+ render();
const user = userEvent.setup();
await expect(
@@ -192,7 +192,7 @@ describe('clear()', () => {
it('does NOT bubble up', async () => {
const parentHandler = jest.fn();
- const screen = render(
+ render(
{
});
it('does NOT work on View', async () => {
- const screen = render();
+ render();
const user = userEvent.setup();
await expect(
diff --git a/src/user-event/type/__tests__/type-managed.test.tsx b/src/user-event/type/__tests__/type-managed.test.tsx
index 4eabc806..19dfe5f3 100644
--- a/src/user-event/type/__tests__/type-managed.test.tsx
+++ b/src/user-event/type/__tests__/type-managed.test.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
import { TextInput } from 'react-native';
-import { createEventLogger } from '../../../test-utils/events';
-import { render } from '../../..';
+import { createEventLogger } from '../../../test-utils';
+import { render, screen } from '../../..';
import { userEvent } from '../..';
beforeEach(() => {
@@ -51,7 +51,7 @@ describe('type() for managed TextInput', () => {
it('supports basic case', async () => {
jest.spyOn(Date, 'now').mockImplementation(() => 100100100100);
const { events, logEvent } = createEventLogger();
- const screen = render();
+ render();
const user = userEvent.setup();
await user.type(screen.getByTestId('input'), 'Wow');
@@ -83,7 +83,7 @@ describe('type() for managed TextInput', () => {
test('supports rejecting TextInput', async () => {
jest.spyOn(Date, 'now').mockImplementation(() => 100100100100);
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
'XXX'} />
);
diff --git a/src/user-event/type/__tests__/type.test.tsx b/src/user-event/type/__tests__/type.test.tsx
index 6d11ca51..44b97c3b 100644
--- a/src/user-event/type/__tests__/type.test.tsx
+++ b/src/user-event/type/__tests__/type.test.tsx
@@ -1,7 +1,7 @@
import * as React from 'react';
-import { View, TextInput, TextInputProps } from 'react-native';
-import { createEventLogger } from '../../../test-utils/events';
-import { render } from '../../..';
+import { TextInput, TextInputProps, View } from 'react-native';
+import { createEventLogger } from '../../../test-utils';
+import { render, screen } from '../../..';
import { userEvent } from '../..';
beforeEach(() => {
@@ -11,7 +11,7 @@ beforeEach(() => {
function renderTextInputWithToolkit(props: TextInputProps = {}) {
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
{
it('works when not all events have handlers', async () => {
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
{
});
it('does NOT work on View', async () => {
- const screen = render();
+ render();
const user = userEvent.setup();
await expect(
@@ -284,7 +284,7 @@ describe('type()', () => {
it('does NOT bubble up', async () => {
const parentHandler = jest.fn();
- const screen = render(
+ render(
{
it('supports direct access', async () => {
const { events, logEvent } = createEventLogger();
- const screen = render(
+ render(
{
it('does dispatch event', () => {
const onPress = jest.fn();
- const screen = render();
+ render();
dispatchEvent(screen.getByTestId('text'), 'press', TOUCH_EVENT);
expect(onPress).toHaveBeenCalledTimes(1);
@@ -17,7 +17,7 @@ describe('dispatchEvent', () => {
it('does not dispatch event to parent host component', () => {
const onPressParent = jest.fn();
- const screen = render(
+ render(
@@ -28,7 +28,7 @@ describe('dispatchEvent', () => {
});
it('does NOT throw if no handler found', () => {
- const screen = render(
+ render(
diff --git a/website/docs/UnderstandingAct.md b/website/docs/UnderstandingAct.md
index 1374d09d..218f1e2c 100644
--- a/website/docs/UnderstandingAct.md
+++ b/website/docs/UnderstandingAct.md
@@ -128,9 +128,11 @@ function TestAsyncComponent() {
```
```jsx
+import { render, screen } from '@testing-library/react-native';
+
test('render async natively', () => {
- const view = render();
- expect(view.getByText('Count 0')).toBeOnTheScreen();
+ render();
+ expect(screen.getByText('Count 0')).toBeOnTheScreen();
});
```
@@ -156,12 +158,12 @@ First solution is to use Jest's fake timers inside out tests:
```jsx
test('render with fake timers', () => {
jest.useFakeTimers();
- const view = render();
+ render();
act(() => {
jest.runAllTimers();
});
- expect(view.getByText('Count 1')).toBeOnTheScreen();
+ expect(screen.getByText('Count 1')).toBeOnTheScreen();
});
```
@@ -173,12 +175,12 @@ If we wanted to stick with real timers then things get a bit more complex. Let
```jsx
test('render with real timers - sleep', async () => {
- const view = render();
+ render();
await act(async () => {
await sleep(100); // Wait a bit longer than setTimeout in `TestAsyncComponent`
});
- expect(view.getByText('Count 1')).toBeOnTheScreen();
+ expect(screen.getByText('Count 1')).toBeOnTheScreen();
});
```
@@ -188,10 +190,10 @@ Let’s try more elegant solution using `waitFor` that will wait for our desired
```jsx
test('render with real timers - waitFor', async () => {
- const view = render();
+ render();
- await waitFor(() => view.getByText('Count 1'));
- expect(view.getByText('Count 1')).toBeOnTheScreen();
+ await waitFor(() => screen.getByText('Count 1'));
+ expect(screen.getByText('Count 1')).toBeOnTheScreen();
});
```
@@ -201,9 +203,9 @@ The above code can be simplified using `findBy` query:
```jsx
test('render with real timers - findBy', async () => {
- const view = render();
+ render();
- expect(await view.findByText('Count 1')).toBeOnTheScreen();
+ expect(await screen.findByText('Count 1')).toBeOnTheScreen();
});
```