admin管理员组文章数量:1400783
I have a function that accepts a parameter and returns a JSX.Element
//title.tsx
export const getTitle = (pageTitle: string) => {
return <title> {pageTitle} </title>;
}
This is how I'm testing it:
describe('Title ponent', () => {
test("Title ponent is rendered", () => {
const wrapper = getTitle('abc');
const expectedText = `abc`;
const actualText = wrapper; //wrapper.text() does not exists;
expect(actualText).toContain(expectedText);
});
});
The test fails with the following output:
Expected value: "abc"
Received object: <React.Fragment><title>abc</title></React.Fragment>
As per some research, I found a solution on Google, which assumes that the function is part of a class ponent. The solution requires wrapper
to be an returned by shallow
while passing the class-based ponent ControlledForm
). This does not apply to my case.
How to extract the text value out of
JSX.element
returned by the function to pare with some expected text?
Or is there another way to test such cases?
I have a function that accepts a parameter and returns a JSX.Element
//title.tsx
export const getTitle = (pageTitle: string) => {
return <title> {pageTitle} </title>;
}
This is how I'm testing it:
describe('Title ponent', () => {
test("Title ponent is rendered", () => {
const wrapper = getTitle('abc');
const expectedText = `abc`;
const actualText = wrapper; //wrapper.text() does not exists;
expect(actualText).toContain(expectedText);
});
});
The test fails with the following output:
Expected value: "abc"
Received object: <React.Fragment><title>abc</title></React.Fragment>
As per some research, I found a solution on Google, which assumes that the function is part of a class ponent. The solution requires wrapper
to be an returned by shallow
while passing the class-based ponent ControlledForm
). This does not apply to my case.
How to extract the text value out of
JSX.element
returned by the function to pare with some expected text?
Or is there another way to test such cases?
Share Improve this question edited Jun 9, 2020 at 17:46 Mr.X asked Jun 9, 2020 at 16:34 Mr.XMr.X 31.4k27 gold badges147 silver badges229 bronze badges 2-
1
I started writing up an answer for you but realize it doesn't address your core issue to write a correct test. I'll work on that part. In the meantime, let's get some terminology straight:
getTitle()
is a function that returns JSX, but it is not a functional ponent. A functional ponent must take a single JavaScript object as its only parameter. Traditionally we call this parameterprops
. The main difference is that you cannot use<getTitle>
as a JSX tag like you could if you wrote it correctly as a functional ponent. Instead, you have to callgetTitle()
directly. – Code-Apprentice Commented Jun 9, 2020 at 16:48 - @Code-Apprentice Thanks for the correction! – Mr.X Commented Jun 9, 2020 at 17:47
2 Answers
Reset to default 4Go for the props or use another shallow. I don't know which one is the red pill.
const titleName = "titleName";
const wrapper = shallow(getTitle(titleName));
const titleElement = wrapper.getElement("title");
titleElement.props.children[1] // "titleName" as [0] and [2] are " "
const containsTitle = titleElement.props.children.includes(titleName);
expect(containsTitle).toBe(true);
or
const titleName = "titleName";
const wrapper = shallow(getTitle(titleName));
const titleElement = wrapper.getElement("title");
const secondWrapper = shallow(titleElement);
secondWrapper.text() // "titleName"
const containsTitle = secondWrapper.contains(titleName);
expect(containsTitle).toBe(true);
Wanted to provide an alternative answer for anyone using Testing Library:
// This code snippet won't run, but will work in a project
import React from "react";
import { render } from "@testing-library/react";
describe('Title ponent', () => {
test("Title ponent is rendered", () => {
const actualText = "abc"
const {getByText} = render(<>{getTitle(actualText)}</>);
expect(getByText(actualText, {selector: "title"})).toBeInTheDocument();
});
});
Depending on what you're testing, you could also use, e.g., getByRole
, and then assert on the content / textContent
of the returned portion of the rendered DOM. And there are certainly other strategies that could work.
本文标签: javascriptHow to unit test a function that return JSXelementStack Overflow
版权声明:本文标题:javascript - How to unit test a function that return JSX.element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744191698a2594548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论