Why is testing a component failing?












1















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










share|improve this question


















  • 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
















1















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










share|improve this question


















  • 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














1












1








1








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 specify defaultProps for component or pass some values in shallow()

    – skyboyer
    Nov 25 '18 at 12:31














  • 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








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












1 Answer
1






active

oldest

votes


















1














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={...} />))





share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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={...} />))





    share|improve this answer






























      1














      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={...} />))





      share|improve this answer




























        1












        1








        1







        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={...} />))





        share|improve this answer















        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={...} />))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        answered Nov 25 '18 at 19:56


























        community wiki





        Alex

































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin