有人可以帮助我了解如何从 react-redux 存根 useReducer 吗?这段代码看起来应该可以工作,但实际上并没有(存根从不拦截 useReducer 函数)。
import React from 'react';
import { mount } from '@cypress/react';
import * as ReactReduxModule from 'react-redux';
const { Provider } = ReactReduxModule;
import { Button } from './button';
import { store } from './store';
import * as SomethingModule from './something';
describe('<Button />', () => {
it('uses stubbed things', () => {
cy.stub(ReactReduxModule, 'useSelector')
.as('useSelector')
.returns({ moo: 'woof' });
cy.stub(SomethingModule, 'something').as('something').returns('scary!!');
cy.get('@something').should('not.have.been.called');
cy.get('@useSelector').should('not.have.been.called');
mount(
<Provider store={store}>
<Button />
</Provider>,
);
// cy.contains('moo: woof scary').should('exist');
cy.get('@something').should('have.been.called'); // passes
cy.get('@useSelector').should('have.been.called'); // fails! :(
});
});
“某物”存根确实触发并拦截了导入,但 useSelector 存根不会。:( 帮助 有问题的组件也超级简单:
import React from 'react';
import { useSelector } from 'react-redux';
import { something } from './something';
import { StoreState } from './store';
export const Button = ({}) => {
const { moo } = useSelector((store: StoreState) => {
return { moo: store.moo };
});
return (
<button>
moo: {moo} {something()}
</button>
);
};