Оцепление потребителя React Context с помощью Sinon - PullRequest
0 голосов
/ 23 марта 2019

У меня возникли проблемы с использованием Sinon v5.0.10 для заглушки Context.Consumer из React (относительно) нового API . Даже после того, как я заглушу получателя контекста, я все равно получаю значение в контексте по умолчанию вместо значения, указанного заглушкой.

Контекст

export const MyContext = React.createContext({ condition: false });

Компонент

export const MyComponent = props => (
    <MyContext.Consumer>
        {myContext => myContext.condition && props.children}
    </MyContext.Consumer>
);

Тест

it("renders its children when the condition is true", () => {
    // arrange
    const fakeConsumer = sandbox.stub(MyContext, "Consumer");
    fakeConsumer.callsFake(props => props.children({ condition: true }));

    // act
    const sut = enzyme.render(<MyComponent>fake content</MyComponent>);

    // assert
    assert.equal(sut.text(), "fake content"); // AssertionError: expected '' to equal 'fake content'
});

Error

AssertionError: expected '' to equal 'fake content'

...