首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过酶反应打印测试文档

通过酶反应打印测试文档
EN

Stack Overflow用户
提问于 2019-03-29 17:46:07
回答 1查看 209关注 0票数 1
代码语言:javascript
复制
    const htmlString = ReactDOMServer.renderToStaticMarkup(printDetailsView(this.props.savedList));
        /* istanbul ignore next */
        setTimeout(() => {
            const printWindow = window.open('', 'PRINT', `width=700,left=${left},top=${top}`);
            /* istanbul ignore next */
            printWindow.document.write(htmlString);
            /* istanbul ignore next */
            printWindow.document.close();
            /* istanbul ignore next */
            printWindow.focus();
            /* istanbul ignore next */
            printWindow.print();
            /* istanbul ignore next */
            printWindow.close();
        }, 0);

如何在酶中模拟document.close(),document.write。

我试着像下面这样做短桩,但是不起作用。

代码语言:javascript
复制
 global.window.document.write = sinon.stub();
global.window.document.close = sinon.stub();



  describe('FilterPanel Connected component testing', () => {
    let wrapper;
    let tokenGet;
    let userStub;
    before(() => {
        tokenGet = sinon.stub(TokenProvider, 'get');
        tokenGet.callsFake((key) => {
            if (key === 'DP_FIRST_NAME') {
                return 'Vini';
            }
            return null;
        });
        userStub = sinon.stub(User, 'isUserLoggedIn');
        const deviceType = {
            isDesktop: true,
        };

        wrapper = mount(
            <FilterPanel
                myListsDetails={myListsDetails}
                savedListActions={savedListActions}
                actions={actions}
                deviceType={deviceType}
                messagesTexts={messagesTexts}
                store={storeFake(storeData)}
                isShared={false}
                openSlider={openSliderStub}
                savedList={savedList} />);
    });
    after(() => {
        shareListResetStub.reset();
        getSavedListsGuestStub.reset();
        tokenGet.resetHistory();
        openSliderStub.reset();
        userStub.resetHistory();
    });
    it('render FilterPanel', () => {
        expect(wrapper.find('FilterPanel').length).to.equal(1);
    });
    it('Call print function', () => {
        userStub.returns(true);
        const instance = wrapper.instance();
        instance.print();
    });

    it('Call print function', () => {
        userStub.returns(true);
        const instance = wrapper.instance();
        wrapper.setProps({
            savedList: { data: [] },
        });
        instance.print();
    });

    it('Dont print function since user is not logged in', () => {
        userStub.returns(false);
        const instance = wrapper.instance();
        instance.print();
        instance.checkAuth();
        expect(openSliderStub.called).to.be.true;
    });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-29 18:52:51

调用的不是window.document.close,而是printWindow上的document.close方法。

真正的DOM最好是完全不受影响:

代码语言:javascript
复制
const printWindowMock = {
  document: {
    write: sinon.stub(),
    ...
};

sinon.stub(window, 'open`).returns(printWindowMock);

模拟应该在每次测试后恢复,因此它们需要在beforeEach中完成,并在afterEach中恢复。这可以通过测试框架插件自动处理,比如mocha-sinon

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55414582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档