如何使用sinon存根window.print函数

时间:2017-12-15 14:39:13

标签: reactjs sinon enzyme

我的代码:

printOrder() {
    setTimeout(() => {
        window.print();
    }, 0);
}

render() {
    const { orderDetails, messageTexts, preferences, isLoading, imagePath, featureFlags } = this.props;
    const error = get(orderDetails, 'errorCode', '');
    return (
        <Layout disableWhiteBG><span className={cx('printOrder')}><button className={cx('printOrders')} onClick={e => this.printOrder(e)}><Icon iconType="svg" width="45px" height="45px" viewBox="0 0 45 45" name="print" /></button></span>}
                    </span>
        </Layout>);
}   

我的测试用例

const printOrder = sinon.spy();

const TokenProvider = {
    get: TokenProviderGetStub,
    logout: TokenProviderLogoutStub,
};

const Cookie = {
    load: CookieLoadStub,
};

const Constants = {
    ...constants,
    classic_yoda_combination: true,
};

const User = {
    isUserLoggedIn: isUserLoggedInStub,
};

const OrderDetailsScreen = proxyquireStrict('./OrderDetails.jsx', {
    'yoda-site-components/lib/helpers/TokenProvider/TokenProvider': TokenProvider,
    'yoda-core-components/lib/helpers/Cookies/Cookies': Cookie,
    'yoda-site-components/lib/helpers/User/User': User,
    'yoda-site-components/lib/components/Layout/Layout': reactStubWithChild('Layout'),
    '../../../components/OrderDetails/OrderDetails': reactStubWithChild('OrderDetails'),
    '../../../common/redirect': redirectStub,
    '../../../common/constant': Constants,
    'react-router': {
        browserHistory: {
            push: pushStub,
        },
    },
});

    describe('Test cases for print Order', () => {
            describe('printOrders in DesktopScreen', () => {
                before(() => {
                    wrapper = shallow(
                        <OrderDetailsScreen {...props} />,
                    );

                    wrapper.find('.printOrders').simulate('click');
                });

                after(() => {
                    wrapper.unmount();
                    printOrder.reset();
                });

                it('Should call the printOrder button', () => {
                    expect(printOrder.calledOnce).to.be.true;
                });
            });

            describe('printOrders in MobileScreen', () => {
                before(() => {
                    wrapper = shallow(
                        <OrderDetailsScreen {...props} deviceType={{ isDesktop: false, isMobile: true }} />,
                    );

                    wrapper.find('.printOrders').simulate('click');
                });

                after(() => {
                    wrapper.unmount();
                    printOrder.reset();
                });

                it('Should call the printOrder button', () => {
                    expect(printOrder.calledOnce).to.be.true;
                });
            });
        });

我的问题是如何用sinon监视window.print?

0 个答案:

没有答案