我在一个应用程序中有一个页面,它使用@recurly/react-recurly
来提供结账表单。它使用RecurlyProvider
和Elements
组件来设置表单本身。
import { Elements, RecurlyProvider } from '@recurly/react-recurly';
我未能正确设置使用@testing-library/react
进行递归反应的单元测试。起初,由于库不在,我遇到了一个错误:
Please load Recurly.js (https://js.recurly.com/v4/recurly.js) on this page prior to loading your React application.
为了克服这个错误^,我将脚本加载到jest-setup.js文件中:
const recurlyScript = document.createElement('script'); recurlyScript.src = 'https://js.recurly.com/v4/recurly.js'; recurlyScript.async = true; document.body.appendChild(recurlyScript); window.recurly = { configure: jest.fn(), Recurly: jest.fn(), useCheckoutPricing: jest.fn() }; export default window.recurly;
这让我犯了一个不同的错误。
TypeError: recurly.configure is not a function
对于实际测试,除了尝试呈现到页面之外,我没有做任何事情。我确信我需要以某种方式模拟提供者或库,但我没有找到实现这一点的正确方法。
it('renders page with recurly', () => { const { container } = render(<IntlProvider locale='en-US' onError={ jest.fn() }> <Page pageData={ pageData } /> </IntlProvider>); waitForComponentToRender(screen); expect(container.getElementsByClassName('app-content').length).toBe(1); });
如何克服此错误以实际测试页面上的功能?我如何正确地在玩笑中反复模仿,或通过@testing-library/react
解释这一点?如果能指出正确的方向,将不胜感激,谢谢!