Why is testing a component failing?
I am trying to write tests for the following component
class LoanApplication extends Component {
constructor(props) {
super(props);
}
canApplyLoan = () => {
const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
};
render() {
this.props.data.enableLoanForm = this.canApplyLoan();
return (
<div className='mainApplication'>
<h4>Apply for a Loan</h4>
<form onSubmit={this.props.loanSubmission} className='loanApp'>
<div className="form-group">
<label htmlFor="exampleInputFullName">Full Name</label>
<input type="text" className="form-control" id="exampleInputFullName"
aria-describedby="fullNameInfo" placeholder="Enter Full Name"
onChange={this.props.fullNameChange}/>
</div>
<div className="form-group">
<label htmlFor="datepicker">Date Of Birth</label>
<input type="date" id="datepicker" aria-describedby="dobInfo" className="form-control"
placeholder="DOB" onChange={this.props.dobChange}/>
</div>
<button className='btn loanBtn' disabled={!this.props.data.enableLoanForm}>Submit
</button>
</form>
................
My test code
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import LoanApplication from '../LoanApplication';
describe('LoanApplicationComponent', () => {
it('should render without throwing error', () => {
expect(shallow(<LoanApplication/>).find('form.loanApp').exists()).toBe(true);
});
});
The error is the following, when I run my jest test
FAIL src/components/__tests__/LoanApplication-test.js
● LoanApplicationComponent › should render without throwing error
TypeError: Cannot read property 'dob' of undefined
7 |
8 | canApplyLoan = () => {
> 9 | const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
| ^
10 | return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
11 | };
12 |
I am not sure why test is failing, do I need to do something specific the data that is being received through the props. Please advise
reactjs jestjs enzyme
add a comment |
I am trying to write tests for the following component
class LoanApplication extends Component {
constructor(props) {
super(props);
}
canApplyLoan = () => {
const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
};
render() {
this.props.data.enableLoanForm = this.canApplyLoan();
return (
<div className='mainApplication'>
<h4>Apply for a Loan</h4>
<form onSubmit={this.props.loanSubmission} className='loanApp'>
<div className="form-group">
<label htmlFor="exampleInputFullName">Full Name</label>
<input type="text" className="form-control" id="exampleInputFullName"
aria-describedby="fullNameInfo" placeholder="Enter Full Name"
onChange={this.props.fullNameChange}/>
</div>
<div className="form-group">
<label htmlFor="datepicker">Date Of Birth</label>
<input type="date" id="datepicker" aria-describedby="dobInfo" className="form-control"
placeholder="DOB" onChange={this.props.dobChange}/>
</div>
<button className='btn loanBtn' disabled={!this.props.data.enableLoanForm}>Submit
</button>
</form>
................
My test code
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import LoanApplication from '../LoanApplication';
describe('LoanApplicationComponent', () => {
it('should render without throwing error', () => {
expect(shallow(<LoanApplication/>).find('form.loanApp').exists()).toBe(true);
});
});
The error is the following, when I run my jest test
FAIL src/components/__tests__/LoanApplication-test.js
● LoanApplicationComponent › should render without throwing error
TypeError: Cannot read property 'dob' of undefined
7 |
8 | canApplyLoan = () => {
> 9 | const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
| ^
10 | return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
11 | };
12 |
I am not sure why test is failing, do I need to do something specific the data that is being received through the props. Please advise
reactjs jestjs enzyme
2
component tries to read bunch of props but you have passed none of them here:shallow(<LoanApplication/>)
. You should either specifydefaultProps
for component or pass some values inshallow()
– skyboyer
Nov 25 '18 at 12:31
add a comment |
I am trying to write tests for the following component
class LoanApplication extends Component {
constructor(props) {
super(props);
}
canApplyLoan = () => {
const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
};
render() {
this.props.data.enableLoanForm = this.canApplyLoan();
return (
<div className='mainApplication'>
<h4>Apply for a Loan</h4>
<form onSubmit={this.props.loanSubmission} className='loanApp'>
<div className="form-group">
<label htmlFor="exampleInputFullName">Full Name</label>
<input type="text" className="form-control" id="exampleInputFullName"
aria-describedby="fullNameInfo" placeholder="Enter Full Name"
onChange={this.props.fullNameChange}/>
</div>
<div className="form-group">
<label htmlFor="datepicker">Date Of Birth</label>
<input type="date" id="datepicker" aria-describedby="dobInfo" className="form-control"
placeholder="DOB" onChange={this.props.dobChange}/>
</div>
<button className='btn loanBtn' disabled={!this.props.data.enableLoanForm}>Submit
</button>
</form>
................
My test code
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import LoanApplication from '../LoanApplication';
describe('LoanApplicationComponent', () => {
it('should render without throwing error', () => {
expect(shallow(<LoanApplication/>).find('form.loanApp').exists()).toBe(true);
});
});
The error is the following, when I run my jest test
FAIL src/components/__tests__/LoanApplication-test.js
● LoanApplicationComponent › should render without throwing error
TypeError: Cannot read property 'dob' of undefined
7 |
8 | canApplyLoan = () => {
> 9 | const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
| ^
10 | return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
11 | };
12 |
I am not sure why test is failing, do I need to do something specific the data that is being received through the props. Please advise
reactjs jestjs enzyme
I am trying to write tests for the following component
class LoanApplication extends Component {
constructor(props) {
super(props);
}
canApplyLoan = () => {
const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
};
render() {
this.props.data.enableLoanForm = this.canApplyLoan();
return (
<div className='mainApplication'>
<h4>Apply for a Loan</h4>
<form onSubmit={this.props.loanSubmission} className='loanApp'>
<div className="form-group">
<label htmlFor="exampleInputFullName">Full Name</label>
<input type="text" className="form-control" id="exampleInputFullName"
aria-describedby="fullNameInfo" placeholder="Enter Full Name"
onChange={this.props.fullNameChange}/>
</div>
<div className="form-group">
<label htmlFor="datepicker">Date Of Birth</label>
<input type="date" id="datepicker" aria-describedby="dobInfo" className="form-control"
placeholder="DOB" onChange={this.props.dobChange}/>
</div>
<button className='btn loanBtn' disabled={!this.props.data.enableLoanForm}>Submit
</button>
</form>
................
My test code
import React from 'react';
import { shallow, mount, render } from 'enzyme';
import LoanApplication from '../LoanApplication';
describe('LoanApplicationComponent', () => {
it('should render without throwing error', () => {
expect(shallow(<LoanApplication/>).find('form.loanApp').exists()).toBe(true);
});
});
The error is the following, when I run my jest test
FAIL src/components/__tests__/LoanApplication-test.js
● LoanApplicationComponent › should render without throwing error
TypeError: Cannot read property 'dob' of undefined
7 |
8 | canApplyLoan = () => {
> 9 | const {dob, nric, loanAmount, selectedLoanTerm, selectedLoanType, fullName} = this.props.data;
| ^
10 | return dob.length && nric.length && loanAmount.length && loanAmount > '0' && selectedLoanTerm.length && selectedLoanType.length && fullName.length;
11 | };
12 |
I am not sure why test is failing, do I need to do something specific the data that is being received through the props. Please advise
reactjs jestjs enzyme
reactjs jestjs enzyme
asked Nov 25 '18 at 12:28
RRPRRP
5611831
5611831
2
component tries to read bunch of props but you have passed none of them here:shallow(<LoanApplication/>)
. You should either specifydefaultProps
for component or pass some values inshallow()
– skyboyer
Nov 25 '18 at 12:31
add a comment |
2
component tries to read bunch of props but you have passed none of them here:shallow(<LoanApplication/>)
. You should either specifydefaultProps
for component or pass some values inshallow()
– skyboyer
Nov 25 '18 at 12:31
2
2
component tries to read bunch of props but you have passed none of them here:
shallow(<LoanApplication/>)
. You should either specify defaultProps
for component or pass some values in shallow()
– skyboyer
Nov 25 '18 at 12:31
component tries to read bunch of props but you have passed none of them here:
shallow(<LoanApplication/>)
. You should either specify defaultProps
for component or pass some values in shallow()
– skyboyer
Nov 25 '18 at 12:31
add a comment |
1 Answer
1
active
oldest
votes
Component tries to read bunch of props but you have passed none of them:
shallow(<LoanApplication/>)
.
You should either:
a) specify defaultProps
:
LoanApplication.defaultProps = {
data: {...}
}
or:
b) pass some values in test:
shallow(<LoanApplication data={...} />))
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%2f53467435%2fwhy-is-testing-a-component-failing%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
Component tries to read bunch of props but you have passed none of them:
shallow(<LoanApplication/>)
.
You should either:
a) specify defaultProps
:
LoanApplication.defaultProps = {
data: {...}
}
or:
b) pass some values in test:
shallow(<LoanApplication data={...} />))
add a comment |
Component tries to read bunch of props but you have passed none of them:
shallow(<LoanApplication/>)
.
You should either:
a) specify defaultProps
:
LoanApplication.defaultProps = {
data: {...}
}
or:
b) pass some values in test:
shallow(<LoanApplication data={...} />))
add a comment |
Component tries to read bunch of props but you have passed none of them:
shallow(<LoanApplication/>)
.
You should either:
a) specify defaultProps
:
LoanApplication.defaultProps = {
data: {...}
}
or:
b) pass some values in test:
shallow(<LoanApplication data={...} />))
Component tries to read bunch of props but you have passed none of them:
shallow(<LoanApplication/>)
.
You should either:
a) specify defaultProps
:
LoanApplication.defaultProps = {
data: {...}
}
or:
b) pass some values in test:
shallow(<LoanApplication data={...} />))
answered Nov 25 '18 at 19:56
community wiki
Alex
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%2f53467435%2fwhy-is-testing-a-component-failing%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
2
component tries to read bunch of props but you have passed none of them here:
shallow(<LoanApplication/>)
. You should either specifydefaultProps
for component or pass some values inshallow()
– skyboyer
Nov 25 '18 at 12:31