diff --git a/README.md b/README.md index 0a1c704..8695722 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,7 @@ expect(queryByTestId('button')).not.toHaveProp('title', 'ok'); toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }); ``` -Check if an element has the supplied text. +Check if an element or its children have the supplied text. This will perform a partial, case-sensitive match when a string match is provided. To perform a case-insensitive match, you can use a `RegExp` with the `/i` modifier. diff --git a/src/__tests__/to-have-text-content.js b/src/__tests__/to-have-text-content.js index 0dfd3a8..7f270e4 100644 --- a/src/__tests__/to-have-text-content.js +++ b/src/__tests__/to-have-text-content.js @@ -49,17 +49,27 @@ describe('.toHaveTextContent', () => { test('can handle multiple levels with content spread across descendants', () => { const { queryByTestId } = render( - - Step - 1 + + One + Two - of + Three - 4 - , + + Four + {null} + Five + + Six + Seven + + Eight + + Nine + , ); - expect(queryByTestId('parent')).toHaveTextContent('Step 1 of 4'); + expect(queryByTestId('parent')).toHaveTextContent('OneTwoThreeFourFiveSixSevenEightNine'); }); test('does not throw error with empty content', () => { diff --git a/src/to-have-text-content.js b/src/to-have-text-content.js index 21967d1..b2e0d3d 100644 --- a/src/to-have-text-content.js +++ b/src/to-have-text-content.js @@ -8,6 +8,8 @@ function getText(child, currentValue = '') { if (!child) { return value; + } else if (Array.isArray(child)) { + return child.reduce((acc, element) => acc + getText(path(['props', 'children'], element)), ''); } else if (typeof child === 'object') { return getText(path(['props', 'children'], child), value); } else {