假設我有以下this.MAX_LENGTH在構造函數中設置常量的組件。import PropTypes from 'prop-types';import React from 'react';class Input extends React.Component { static propTypes = { value: PropTypes.string.isRequired }; constructor(props) { super(props); // An example constant this.MAX_LENGTH = 1024; } render() { return ( <label htmlFor="comment_body"> <textarea className="comment-reply input-highlight-on-focus" type="input" name="comment[body]" id="comment_body" maxLength={this.MAX_LENGTH} value={this.props.value} /> </label> ) }}export default Input;該MAX_LENGTH常量用于設置的最大長度textarea。在我的 Jest 規范中,我想模擬 的值this.MAX_LENGTH,但我不確定如何設置該模擬。這是我的 Jest 測試的外觀(它使用chai并enzyme作為測試助手):it('renders the textarea', () => { // Mock the constant here to set a custom value of 99 // ???? const wrapper = mount(<Input value={'foo'} />) const textarea = wrapper.find('.comment-reply').first(); expect(textarea).to.have.attr('maxLength', '99');});我可以????用模擬常量的值來代替什么?我嘗試閱讀Jest 文檔中的ES6 Class Mocks,但它似乎是在模擬整個導入的類,我不確定它如何應用于單個常量。
在 Jest 中模擬類常量
慕桂英3389331
2022-11-11 14:19:15