React functions which are not called explicitly can not be tested using Jest framework. For example, as the following pictures, 1) componentDidMount function 2) getOption (It is used to configure Echarts). The simulate function can not be used to call the functions above (https://reactjs.org/docs/test-utils.html#simulate). Do you have any idea on testing the functions mentioned above? Thanks in advance. 

    componentDidMount() {
        this.secrh();
    }
    getOption = () => {
        var xdata = this.timeArr()
        let option = {
            tooltip: {
                trigger: 'axis'
            },
            color:["#2F4554","#C23531"],
            legend: {
                data: ['实际频率', '历史频率'],
                left: 'right',
                selected: {
                    '实际频率': this.state.Forecast,
                    '历史频率': this.state.Actual,
                }
            },

            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                axisLabel: {
                    interval: "auto",
                    rotate: 55
                },
            },
            yAxis: {
                name: "Hz",
                type: 'value',
                min: this.state.mindata,
                max: this.state.maxdata,

            },
            series: [
                {
                    name: '实际频率',
                    type: 'line',
                    data: this.state.actualdata
                },
                {
                    name: '历史频率',
                    type: 'line',
                    data: this.state.forecastdata
                },

            ]
        }

        xdata.forEach(item => {
            let day = item.split(" ")[0].split("-")[2];
            let hour = item.split(" ")[1].split(":")[0];
            option.xAxis.data.push(`${day}日${hour}时`);
        });
        return option;
    };
    render() {
        const dateFormat = 'YYYY/MM/DD HH';
        return (
            <div>
                <Spin spinning={this.state.loading}>
                    时间:  <RangePicker
                        locale={locale}
                        placeholder={['开始时间', '结束时间']}
                        disabledDate={this.disabledDate}
                        showTime={{ hideDisabledOptions: true }}
                        onChange={this.onChangeDate}
                        format="YYYY-MM-DD HH"
                        defaultValue={[moment(this.state.starttime, dateFormat), moment(this.state.endtime, dateFormat)]}
                    />
                    <Button className="morescreen" onClick={this.secrh}>查询</Button>
                    <ReactEcharts
                        option={this.getOption()}
                        style={{ height: '350px', width: '100%' }}
                    />
                </Spin>
            </div>
        )

0 Answers