Reactjs 测试使用上下文的react组件-更改被测组件的状态
我想测试下面的React组件Reactjs 测试使用上下文的react组件-更改被测组件的状态,reactjs,testing,jestjs,use-context,Reactjs,Testing,Jestjs,Use Context,我想测试下面的React组件 import React, { useContext, useState } from "react"; import { IntlProvider } from "react-intl"; export const Context = React.createContext(); const defaultLocale = navigator.language; const LocalizationWrapper = (
import React, { useContext, useState } from "react";
import { IntlProvider } from "react-intl";
export const Context = React.createContext();
const defaultLocale = navigator.language;
const LocalizationWrapper = (props) => {
const [locale, setLocale] = useState(defaultLocale);
function changeLocale(newLocale) {
setLocale(newLocale);
}
return (
<Context.Provider value={{ locale, changeLocale }}>
<IntlProvider locale={locale}>{props.children}</IntlProvider>
</Context.Provider>
);
};
import React from 'react';
import { render, unmountComponentAtNode } from "react-dom";
import { FormattedDate } from "react-intl";
import { act } from "react-dom/test-utils";
import LocalizationWrapper from './localizationWrapper';
let container = null;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});
it("date formatted with default locale", () => {
act(() => {
render(
<LocalizationWrapper >
<FormattedDate value={new Date("2021-04-23")} />
</LocalizationWrapper>, container);
});
expect(container.textContent).toBe("4/23/2021");
});
const context = useContext(Context);
context.changeLocale(locale);