From 2767b49d342bedeb4f526999ccf692793aa21e2c Mon Sep 17 00:00:00 2001 From: Jeremy Shearer Date: Sat, 12 Jun 2021 18:29:28 -0500 Subject: [PATCH 1/2] fix: toHaveTextContent supports interpolated variables --- src/__tests__/to-have-text-content.js | 7 +++++++ src/to-have-text-content.js | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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..ed01909 100644 --- a/src/to-have-text-content.js +++ b/src/to-have-text-content.js @@ -1,5 +1,5 @@ import { matcherHint } from 'jest-matcher-utils'; -import { compose, defaultTo, is, join, map, path, filter } from 'ramda'; +import { compose, defaultTo, is, join, map, path, filter, pathOr } from 'ramda'; import { checkReactElement, getMessage, matches, normalize } from './utils'; @@ -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 { From 4f2d3d38562bad275fbea4956fab81396ac6d46b Mon Sep 17 00:00:00 2001 From: Jeremy Shearer Date: Tue, 15 Jun 2021 15:34:57 -0500 Subject: [PATCH 2/2] Update src/to-have-text-content.js Co-authored-by: Bruno Castro --- src/to-have-text-content.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/to-have-text-content.js b/src/to-have-text-content.js index ed01909..b469c06 100644 --- a/src/to-have-text-content.js +++ b/src/to-have-text-content.js @@ -1,5 +1,5 @@ import { matcherHint } from 'jest-matcher-utils'; -import { compose, defaultTo, is, join, map, path, filter, pathOr } from 'ramda'; +import { compose, defaultTo, is, join, map, path, filter } from 'ramda'; import { checkReactElement, getMessage, matches, normalize } from './utils';