From 1bd74199432a2a85bef2dd14f9c56b78096c90bc Mon Sep 17 00:00:00 2001 From: Jeremy Shearer Date: Mon, 2 Aug 2021 20:29:06 -0400 Subject: [PATCH] fix: toHaveTextContent supports interpolated variables (#59) --- src/__tests__/to-have-text-content.js | 7 +++++++ src/to-have-text-content.js | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/__tests__/to-have-text-content.js b/src/__tests__/to-have-text-content.js index 7f270e4..7f2b9eb 100644 --- a/src/__tests__/to-have-text-content.js +++ b/src/__tests__/to-have-text-content.js @@ -96,4 +96,11 @@ describe('.toHaveTextContent', () => { expect(getByTestId('parent')).toHaveTextContent(/^Shown$/); }); + + test('can handle text with an interpolated variable', () => { + const variable = 'variable'; + const { container } = render(With a {variable}); + + expect(container).toHaveTextContent('With a variable'); + }); }); diff --git a/src/to-have-text-content.js b/src/to-have-text-content.js index b2e0d3d..b469c06 100644 --- a/src/to-have-text-content.js +++ b/src/to-have-text-content.js @@ -9,7 +9,7 @@ function getText(child, currentValue = '') { if (!child) { return value; } else if (Array.isArray(child)) { - return child.reduce((acc, element) => acc + getText(path(['props', 'children'], element)), ''); + return child.reduce((acc, element) => acc + getText(element), ''); } else if (typeof child === 'object') { return getText(path(['props', 'children'], child), value); } else {