因此,我有一个通过Context API消耗数据的组件。
我的笑话测试与以下内容大致相同,来自中级教程:
import React from 'react';
import Enzyme, { mount } from 'enzyme';
// ensure you're resetting modules before each test
beforeEach(() => {
jest.resetModules();
});
// Takes the context data we want to test, or uses defaults
const getLanguageSelectorWithContext = (context = {languages: ['en', 'fr', 'es'], activeLanguage: 'en'}) => {
// Will then mock the LocalizeContext module being used in our LanguageSelector component
jest.doMock('./LocalizeContext', () => {
return {
LocalizeContext: {
Consumer: (props) => props.children(context)
}
}
});
// you need to re-require after calling jest.doMock.
// return the updated LanguageSelector module that now includes the mocked context
return require('./LanguageSelector').LanguageSelector;
};
describe('<LanguageSelector />', () => {
it('should return default list of languages', () => {
// This will use the default context param since we pass nothing
const LanguageSelector = getLanguageSelectorWithContext();
const wrapper = mount(<LanguageSelector />);
expect(wrapper.find('li').length).toBe(3);
});
});
我的问题是,在测试的这一点上,是否可以访问组件中使用的context
?