How to get value of nested props function for unit testing

aaayumi

I have an issue with getting value of nested props function.

this.props.user.getPerson().getData()

getPerson returns new object with person related data.

getData returns person related data.

It's separated for readability and I want to make it work for unit testing.

Test 1:

 let _wrapper,
    initialProps

  beforeEach(() => {
    initialProps = {
      user: {
        getPerson: () => {}
      }
    }
    _wrapper = shallow(<Test1 {...initialProps} />)
  })
some tests...
})

It returned TypeError: Cannot read property 'getData' of undefined.

Test2

 let _wrapper,
    initialProps

  beforeEach(() => {
    initialProps = {
      user: {
        getPerson: () => { getData: () => {} }
      }
    }
    _wrapper = shallow(<Test2 {...initialProps} />)
  })
some tests...
})

It returned TypeError: Cannot read property 'getData' of undefined Same error as Test1.

I tried to get value of props function by passing function however it didn't work from second function.

How can I get value of nested props function?

Brian Adams

You need to wrap object literals in parentheses to return them from arrow functions:

initialProps = {
  user: {
    getPerson: () => ({ getData: () => { } })  // wrap in parentheses
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to get request object in django unit testing?

how to get request object in django unit testing?

How to pass any UUID in a function in unit testing in mockito?

How to mock a tornado coroutine function using mock framework for unit testing?

How to get $(this) in nested function

Unit testing an function returning a function

ReactJS Unit Testing - TypeError: this.props.onChange is not a function

How does the inner function in a HOC get the props

Unit testing Azure Function: Cannot create an instance of TraceWriter, how to mock?

Unit Testing a Function Macro

Unit Testing Android function

How to do unit testing for nested "using" statements in C#?

How to add unit test for a function that calls a function received in props

how to get state value according props

Vue Unit Testing: How to test complex components with props, vuex store, watchers, getters etc

How to get object props function caller?

Cmocka unit testing with C: mocking nested function calls

In order to test a function with unit testing, does it need to have a return value?

Async callback timeout when unit testing nested async function

How to do unit testing of main file's function in c

unit testing a function that doesn't return a value

How do you get to the value inside of a python function that returns nothing for unit testing?

How to perform unit testing on void function with coverage

How to get nested loop value using map function in React js?

Sinon how to stub method for unit testing a Async function

Unit testing: how to stub a wrapper function

how to fix an error "is not a function" in unit testing angular 11

How do I parse the value of a nested function to another nested function to get the value in JavaScript

Unit testing a function with assertions

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  7. 7

    Do Idle Snowflake Connections Use Cloud Services Credits?

  8. 8

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  9. 9

    Binding element 'string' implicitly has an 'any' type

  10. 10

    BigQuery - concatenate ignoring NULL

  11. 11

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  12. 12

    In Skype, how to block "User requests your details"?

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Pandas - check if dataframe has negative value in any column

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Generate random UUIDv4 with Elm

  17. 17

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  18. 18

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  19. 19

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  20. 20

    EXCEL: Find sum of values in one column with criteria from other column

  21. 21

    How to use merge windows unallocated space into Ubuntu using GParted?

HotTag

Archive