Testing placeholder prop in imported component
I have a react component in which I've imported react-date-picker
, which doesn't have the ability to set the placeholder as prop for the input fields, so I've used a workaround found (in componentDidMount
) on its github page here.
here is my component:
import React from 'react';
import DatePicker from 'react-date-picker';
class AlertModal extends React.Component {
...
componentDidMount(){
document.querySelector('.react-date-picker__button__input__day').placeholder = 'Day';
document.querySelector('.react-date-picker__button__input__month').placeholder = 'Month';
document.querySelector('.react-date-picker__button__input__year').placeholder = 'Year'
}
render = () => {
return (
<div>
<DatePicker
onChange={this.handleToDate}
value={this.state.toDate}
minDate={this.state.minToDate}
calendarIcon={null}
showLeadingZeros={true}
/>
</div>
)
}
}
The issue I have is in my test, when I try the below it fails with: Cannot set property 'placeholder' of undefined
.
describe('datepicker placehoder overrides', () => {
let component;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
Any idea?
javascript reactjs jestjs
add a comment |
I have a react component in which I've imported react-date-picker
, which doesn't have the ability to set the placeholder as prop for the input fields, so I've used a workaround found (in componentDidMount
) on its github page here.
here is my component:
import React from 'react';
import DatePicker from 'react-date-picker';
class AlertModal extends React.Component {
...
componentDidMount(){
document.querySelector('.react-date-picker__button__input__day').placeholder = 'Day';
document.querySelector('.react-date-picker__button__input__month').placeholder = 'Month';
document.querySelector('.react-date-picker__button__input__year').placeholder = 'Year'
}
render = () => {
return (
<div>
<DatePicker
onChange={this.handleToDate}
value={this.state.toDate}
minDate={this.state.minToDate}
calendarIcon={null}
showLeadingZeros={true}
/>
</div>
)
}
}
The issue I have is in my test, when I try the below it fails with: Cannot set property 'placeholder' of undefined
.
describe('datepicker placehoder overrides', () => {
let component;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
Any idea?
javascript reactjs jestjs
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50
add a comment |
I have a react component in which I've imported react-date-picker
, which doesn't have the ability to set the placeholder as prop for the input fields, so I've used a workaround found (in componentDidMount
) on its github page here.
here is my component:
import React from 'react';
import DatePicker from 'react-date-picker';
class AlertModal extends React.Component {
...
componentDidMount(){
document.querySelector('.react-date-picker__button__input__day').placeholder = 'Day';
document.querySelector('.react-date-picker__button__input__month').placeholder = 'Month';
document.querySelector('.react-date-picker__button__input__year').placeholder = 'Year'
}
render = () => {
return (
<div>
<DatePicker
onChange={this.handleToDate}
value={this.state.toDate}
minDate={this.state.minToDate}
calendarIcon={null}
showLeadingZeros={true}
/>
</div>
)
}
}
The issue I have is in my test, when I try the below it fails with: Cannot set property 'placeholder' of undefined
.
describe('datepicker placehoder overrides', () => {
let component;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
Any idea?
javascript reactjs jestjs
I have a react component in which I've imported react-date-picker
, which doesn't have the ability to set the placeholder as prop for the input fields, so I've used a workaround found (in componentDidMount
) on its github page here.
here is my component:
import React from 'react';
import DatePicker from 'react-date-picker';
class AlertModal extends React.Component {
...
componentDidMount(){
document.querySelector('.react-date-picker__button__input__day').placeholder = 'Day';
document.querySelector('.react-date-picker__button__input__month').placeholder = 'Month';
document.querySelector('.react-date-picker__button__input__year').placeholder = 'Year'
}
render = () => {
return (
<div>
<DatePicker
onChange={this.handleToDate}
value={this.state.toDate}
minDate={this.state.minToDate}
calendarIcon={null}
showLeadingZeros={true}
/>
</div>
)
}
}
The issue I have is in my test, when I try the below it fails with: Cannot set property 'placeholder' of undefined
.
describe('datepicker placehoder overrides', () => {
let component;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
Any idea?
javascript reactjs jestjs
javascript reactjs jestjs
edited Nov 13 '18 at 18:45
isherwood
36.8k1081111
36.8k1081111
asked Nov 13 '18 at 16:46
Mauro74Mauro74
2,136133870
2,136133870
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50
add a comment |
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50
add a comment |
1 Answer
1
active
oldest
votes
From the docs for Jest:
Jest ships with jsdom which simulates a DOM environment as if you were
in the browser. This means that every DOM API that we call can be
observed in the same way it would be observed in a browser!
So you just have to add the relevant HTML markup to the DOM before proceeding the test.
describe('datepicker placehoder overrides', () => {
let component;
document.body.innerHTML = `<div>
<input class="react-date-picker__button__input__day" />
<input class="react-date-picker__button__input__month" />
<input class="react-date-picker__button__input__year" />
</div>`;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285784%2ftesting-placeholder-prop-in-imported-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From the docs for Jest:
Jest ships with jsdom which simulates a DOM environment as if you were
in the browser. This means that every DOM API that we call can be
observed in the same way it would be observed in a browser!
So you just have to add the relevant HTML markup to the DOM before proceeding the test.
describe('datepicker placehoder overrides', () => {
let component;
document.body.innerHTML = `<div>
<input class="react-date-picker__button__input__day" />
<input class="react-date-picker__button__input__month" />
<input class="react-date-picker__button__input__year" />
</div>`;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
add a comment |
From the docs for Jest:
Jest ships with jsdom which simulates a DOM environment as if you were
in the browser. This means that every DOM API that we call can be
observed in the same way it would be observed in a browser!
So you just have to add the relevant HTML markup to the DOM before proceeding the test.
describe('datepicker placehoder overrides', () => {
let component;
document.body.innerHTML = `<div>
<input class="react-date-picker__button__input__day" />
<input class="react-date-picker__button__input__month" />
<input class="react-date-picker__button__input__year" />
</div>`;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
add a comment |
From the docs for Jest:
Jest ships with jsdom which simulates a DOM environment as if you were
in the browser. This means that every DOM API that we call can be
observed in the same way it would be observed in a browser!
So you just have to add the relevant HTML markup to the DOM before proceeding the test.
describe('datepicker placehoder overrides', () => {
let component;
document.body.innerHTML = `<div>
<input class="react-date-picker__button__input__day" />
<input class="react-date-picker__button__input__month" />
<input class="react-date-picker__button__input__year" />
</div>`;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
From the docs for Jest:
Jest ships with jsdom which simulates a DOM environment as if you were
in the browser. This means that every DOM API that we call can be
observed in the same way it would be observed in a browser!
So you just have to add the relevant HTML markup to the DOM before proceeding the test.
describe('datepicker placehoder overrides', () => {
let component;
document.body.innerHTML = `<div>
<input class="react-date-picker__button__input__day" />
<input class="react-date-picker__button__input__month" />
<input class="react-date-picker__button__input__year" />
</div>`;
const mockSubmit = jest.fn();
const mockCancel = jest.fn();
it('should be DD for days', () => {
component = mount(<AlertModal submit={mockSubmit} cancel={mockCancel} />);
expect(
component
.find('.react-date-picker__inputGroup__day')
.at(0)
.props().placeholder
).toBeEqual('DD');
});
});
answered Nov 13 '18 at 18:07
Boy With Silver WingsBoy With Silver Wings
4,3462732
4,3462732
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285784%2ftesting-placeholder-prop-in-imported-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not directly related to an answer, but you may want to try to avoid direct dom manipulation in react. Consider using setState instead of querySelector.
– Bonnie
Nov 13 '18 at 16:50