gpt4 book ai didi

javascript - 对于无渲染组件, enzyme 浅层返回未定义

转载 作者:行者123 更新时间:2023-11-30 20:28:18 24 4
gpt4 key购买 nike

这是我的js文件

import React, { Component } from 'react';

export default class ProjectStore extends Component {
static lodingCount = 0;
constructor(props) {
super(props);
}

static setLodingCount(countValue){
ProjectStore.lodingCount = countValue;
}

static getLodingCount(){
return ProjectStore.lodingCount;
}
}

我写了一个测试用例,如下所示:

import React from 'react';
import {shallow} from 'enzyme';
import ProjectStore from '../projectStore.js';

describe('<ProjectStore />', () => {
it('should return loading count', () => {
const ProjectStore = shallow(<ProjectStore />);
ProjectStore.setLodingCount(2);
expect(ProjectStore.lodingCount).toEqual(2);
});
});

但是当我执行 npm test 时,它返回:

ReferenceError: ProjectStore is not defined

我在这里做错了什么?

最佳答案

测试类中的静态方法时,您不需要呈现该组件。您需要做的就是从该类调用该静态方法,如下所示:

import React from 'react';
import Adapter from 'enzyme-adapter-react-16';

import ProjectStore from './projectStore.js';

describe('<ProjectStore />', () => {
it('should return loading count', () => {
ProjectStore.setLodingCount(2);
expect(ProjectStore.lodingCount).toEqual(2);
});
});

您可以从 this answer 了解更多信息.

关于javascript - 对于无渲染组件, enzyme 浅层返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com