Ad
Test Document Print Via Enzyme - React
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);
How can i mock document.close(), document.write in enzyme.
I tried stubbing like below but it doesnt work.
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;
});
});
Ad
Answer
It's not window.document.close
that is called but document.close
method on printWindow
.
Real DOM should preferably not be affected at all:
const printWindowMock = {
document: {
write: sinon.stub(),
...
};
sinon.stub(window, 'open`).returns(printWindowMock);
Mocks should be restored after each tests, so they need to be done in beforeEach
and restored in afterEach
. This can be automatically handled by testing framework plugins, like mocha-sinon
.
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad