diff --git a/src/__tests__/byDisplayValue.test.js b/src/__tests__/byDisplayValue.test.js index 4bdc743d9..518ced57b 100644 --- a/src/__tests__/byDisplayValue.test.js +++ b/src/__tests__/byDisplayValue.test.js @@ -81,10 +81,10 @@ test('findBy queries work asynchronously', async () => { ); await expect( - findByDisplayValue('Display Value', options) + findByDisplayValue('Display Value', {}, options) ).rejects.toBeTruthy(); await expect( - findAllByDisplayValue('Display Value', options) + findAllByDisplayValue('Display Value', {}, options) ).rejects.toBeTruthy(); setTimeout( diff --git a/src/__tests__/byTestId.test.js b/src/__tests__/byTestId.test.js index 3f8363e5a..630c6e97e 100644 --- a/src/__tests__/byTestId.test.js +++ b/src/__tests__/byTestId.test.js @@ -115,8 +115,8 @@ test('getAllByTestId, queryAllByTestId', () => { 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(); + await expect(findByTestId('aTestId', {}, options)).rejects.toBeTruthy(); + await expect(findAllByTestId('aTestId', {}, options)).rejects.toBeTruthy(); setTimeout( () => diff --git a/src/__tests__/byText.test.js b/src/__tests__/byText.test.js index 5b4ff36dd..b64b81c76 100644 --- a/src/__tests__/byText.test.js +++ b/src/__tests__/byText.test.js @@ -1,7 +1,14 @@ // @flow import * as React from 'react'; -import { View, Text, TouchableOpacity, Image } from 'react-native'; -import { render } from '..'; +import { + View, + Text, + TouchableOpacity, + Image, + Button, + TextInput, +} from 'react-native'; +import { render, getDefaultNormalizer } from '..'; const MyButton = ({ children, onPress }) => ( @@ -88,8 +95,8 @@ test('getAllByText, queryAllByText', () => { 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(); + await expect(findByText('Some Text', {}, options)).rejects.toBeTruthy(); + await expect(findAllByText('Some Text', {}, options)).rejects.toBeTruthy(); setTimeout( () => @@ -105,6 +112,39 @@ test('findByText queries work asynchronously', async () => { await expect(findAllByText('Some Text')).resolves.toHaveLength(1); }, 20000); +describe('findBy options deprecations', () => { + let warnSpy; + beforeEach(() => { + warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + }); + afterEach(() => { + warnSpy.mockRestore(); + }); + + test('findByText queries warn on deprecated use of WaitForOptions', async () => { + const options = { timeout: 10 }; + // mock implementation to avoid warning in the test suite + const { rerender, findByText } = render(); + await expect(findByText('Some Text', options)).rejects.toBeTruthy(); + + setTimeout( + () => + rerender( + + Some Text + + ), + 20 + ); + + await expect(findByText('Some Text')).resolves.toBeTruthy(); + + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('Use of option "timeout"') + ); + }, 20000); +}); + test.skip('getByText works properly with custom text component', () => { function BoldText({ children }) { return {children}; @@ -181,7 +221,7 @@ test('queryByText not found', () => { ).toBeFalsy(); }); -test('queryByText nested text across multiple in ', () => { +test('queryByText does not match nested text across multiple in ', () => { const { queryByText } = render( Hello{' '} @@ -192,7 +232,7 @@ test('queryByText nested text across multiple in ', () => { ); - expect(queryByText('Hello World!')?.props.nativeID).toBe('1'); + expect(queryByText('Hello World!')).toBe(null); }); test('queryByText with nested Text components return the closest Text', () => { @@ -204,7 +244,7 @@ test('queryByText with nested Text components return the closest Text', () => { const { queryByText } = render(); - expect(queryByText('My text')?.props.nativeID).toBe('2'); + expect(queryByText('My text', { exact: false })?.props.nativeID).toBe('2'); }); test('queryByText with nested Text components each with text return the lowest one', () => { @@ -217,10 +257,10 @@ test('queryByText with nested Text components each with text return the lowest o const { queryByText } = render(); - expect(queryByText('My text')?.props.nativeID).toBe('2'); + expect(queryByText('My text', { exact: false })?.props.nativeID).toBe('2'); }); -test('queryByText nested in ', () => { +test('queryByText nested deep in ', () => { const CustomText = ({ children }) => { return {children}; }; @@ -228,22 +268,171 @@ test('queryByText nested in ', () => { expect( render( - Hello World! + Hello World! ).queryByText('Hello World!') - ).toBeTruthy(); + ).toBe(null); }); -test('queryByText nested deep in ', () => { - const CustomText = ({ children }) => { - return {children}; - }; +test('queryByText with nested Text components: not-exact text match returns the most deeply nested common component', () => { + const { queryByText: queryByTextFirstCase } = render( + + bob + My + text + + ); + + const { queryByText: queryByTextSecondCase } = render( + + bob + My text for test + + ); + expect(queryByTextFirstCase('My text')).toBe(null); expect( - render( - - Hello World! - - ).queryByText('Hello World!') - ).toBeTruthy(); + queryByTextSecondCase('My text', { exact: false })?.props.nativeID + ).toBe('2'); +}); + +test('queryAllByText does not match several times the same text', () => { + const allMatched = render( + + Start + This is a long text + + ).queryAllByText('long text', { exact: false }); + expect(allMatched.length).toBe(1); + expect(allMatched[0].props.nativeID).toBe('2'); +}); + +test('queryAllByText matches all the matching nodes', () => { + const allMatched = render( + + Start + This is a long text + This is another long text + + ).queryAllByText('long text', { exact: false }); + expect(allMatched.length).toBe(2); + expect(allMatched.map((node) => node.props.nativeID)).toEqual(['2', '3']); +}); + +describe('supports TextMatch options', () => { + test('getByText, getAllByText', () => { + const { getByText, getAllByText } = render( + + Text and details +