与Sinon接触React Context消费者

时间:2019-03-22 23:09:26

标签: javascript reactjs unit-testing sinon react-context

使用Sinon v5.0.10对React's (relatively) new API中的Context.Consumer进行存根时,我遇到了一些麻烦。即使在对上下文使用者进行存根之后,我仍然会在默认上下文中获取该值,而不是存根指定的值。

上下文

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'
});

测试

AssertionError: expected '' to equal 'fake content'

错误

use DateTime::Format::Strptime qw( ); my $format = DateTime::Format::Strptime->new( pattern => '%FT%T.%6N', time_zone => 'UTC', # Or 'local' or 'America/Toronto' or '-0500' on_error => 'croak', ); my $dt = $format->parse_datetime('2019-01-24T00:00:05.011719'); $dt->set_formatter($format); # Set default stringification format. $dt->truncate( to => 'second' )->add( seconds => 1 ) if $dt->nanosecond; $dt->truncate( to => 'minute' )->add( minutes => 1 ) if $dt->second; $dt->add( minutes => 5 - ( $dt->minute % 5 ) ) if $dt->minute % 5; say $dt; # 2019-01-24T00:05:00.000000

0 个答案:

没有答案