用笑话和酶测试 UseReactiveVar

Test UseReactiveVar with jest and enzyme

我正在使用 apollo-client-3 进行状态管理。我想测试 useReactiveVar.

这是我的代码:

import { useReactiveVar } from '@apollo/client';

const showBoxVar = makeVar(false);
const App: FunctionComponent = () => {

    const showBox = useReactiveVar(showBoxVar);
    return (
        <React.Fragment>
            {showBox && <Box/> }

            <SomeComponent />

        </React.Fragment>
    );
}

我想测试一旦 showBox 为真,Box 就会呈现。怎么做?

你可以像下面这样嘲笑他们

jest.mock('@apollo/client', () => {
  return {
    useReactiveVar: () => true,
    makeVar: jest.fn(() => null)
  }
});