【发布时间】:2019-07-27 00:31:05
【问题描述】:
尝试在“Nav”类中测试此功能,我的目标是测试路由器,或者更重要的是获得 Router.push(/) 的覆盖率
<AppBar className={classes.appBar} position="static">
<Toolbar className={classes.toolbar}>
{authenticated && this.state.layoutMode ==='desktop' ? (
<Grid container
direction ="row"
justify="flex-end"
alignItems="center">
<div className={classes.root}>
<Tabs id="Tab" className = {classes.tabBar} value={value} onChange={this.handleChange}>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
<Tab label="example" alt="example" onClick={() => Router.push('/example')}/>
</Tabs>
</div>
</Grid>
) : (
authenticated && <BurgerMenu/>
)}
到目前为止,我的测试看起来像这样
describe('Test for navigation', () => {
let sandbox;
beforeAll(() => {
sandbox = sinon.createSandbox();
});
afterEach(() => {
sandbox.restore();
});
it('should render with data', () => {
const stub = jest.fn();
sandbox.stub(Router, 'push').callsFake(stub);
const wrapper = shallow(<Nav authenticated />);
expect(wrapper).toMatchSnapshot();
expect(stub.mock.calls.length).toBe(0);
wrapper.find(AppBar).props().onClick('test');
expect(stub.mock.calls.length).toBe(7);
});
});
但是我遇到了问题,我似乎找不到“AppBar”的道具我已将 Nav 导入到测试中,但错误显示为“方法“道具”应在 1 个节点上运行。找到 0反而” 有什么想法吗?
【问题讨论】:
标签: reactjs testing jestjs sinon