Supreetha M S
Supreetha M S

Reputation: 1

includes() and toLowerCase throws not a function error from jest unit test

MyComponent.js has toLowerCase and includes on the props. When I unit test MyComponent, I get toLowerCase and includes() is not a function.

MyComponent.js

this.props.currentUser.role.toLowerCase().includes('admin')

MyComponent.test.js

    mount(<Provider store={store}>
        <MyComponent />
      </Provider>);
  });

Error:

TypeError: this.props.currentUser.role.toLowerCase is not a function 
TypeError: this.props.currentUser.role.includes is not a function

Upvotes: 0

Views: 1980

Answers (3)

Nijat Aliyev
Nijat Aliyev

Reputation: 876

Firstable check if value exists and if role is string then apply some functions

this.props.currentUser && this.props.currentUser.role &&
this.props.currentUser.role.toString().toLowerCase().includes('admin') ? 'Yes included' or 'Not'

Be carefully too if you used toLowerCase() function do not compare with Admin compare with admin

Upvotes: 0

soumya sunny
soumya sunny

Reputation: 226

You should pass the currentUser object as a prop to the component.

let user ={role:"test"}
mount(<Provider store={store}>
        <MyComponent currentUser={user}/>
      </Provider>);
});

Upvotes: 2

Dani
Dani

Reputation: 584

.toLowerCase() and .include() only work on strings. Make sure the parameter you are passing to the props is a string. In your code you are not passing any props so basically you are trying to execute those functions on an undefined type. Try this instead:

mount(<Provider store={store}>
        <MyComponent currentUser={user}/> //Specify 'user' before
      </Provider>);

Upvotes: 0

Related Questions