Testing an IF statement with respect to state with Jest doesn't work : I am trying to test this method which is responsible for editing but am able to the lines which replace the **old title** and the **old body** with the new title and body.

This is located in the IF section. Below is the code which implements editing :

    onEditNote = (event) => {
          const { target: { value: id } } = event;
          const obj = {
            title: this.state.title,
            body: this.state.body,
          };
     
          // clear the errors while submitting
     
          this.setState({ titleError: '', bodyError: '' });
     
          if (obj.title === '') {
            this.setState({ titleError: 'Title empty, please add title' });
          } else if (obj.body === '') {
            this.setState({ bodyError: 'Body empty, please add body' });
          } else if (obj.title.length > 20) {
            this.setState({ titleError: 'Title is too long.' });
          } else {
            this.setState((state) => {
              const list = state.notesArray.map((item) => {
                if (item.id === id) {
                  // eslint-disable-next-line no-param-reassign
                  item.title = obj.title;
                  // eslint-disable-next-line no-param-reassign
                  item.body = obj.body;
                }
                return item;
              });
     
              localStorage.setItem('items', JSON.stringify(list));
     
              // eslint-disable-next-line no-undef
              $('#editModal').modal('close');
              this.onSuccessToast('Noted edited successfully.');
              return {
                list,
                title: '',
                body: '',
     
              };
            });
          }
        };

These are the lines in the above code which are not covered by the test I implemented :

  

>     if (item.id === id) {
>                       // eslint-disable-next-line no-param-reassign
>                       item.title = obj.title;
>                       // eslint-disable-next-line no-param-reassign
>                       item.body = obj.body;
>                     }

And below is my test I am implementing which doesn't cover the IF statement, yet I see I have covered it :

 

     it('should edit a Note.', () => {
        wrapper = shallow(<App />);
        $.fn.modal = jest.fn();
        wrapper.instance().onEditNote({ target: { value: '0' } });
    
        wrapper.instance().setState({
          title: 'huxy 12',
          body: 'hey huxy this is not great.',
          notesArray: [{ title: 'hey', body: 'its good', id: '0' }],
        });
    
        wrapper.instance().setState((state) => {
          state.notesArray.map((item) => {
            if (item.id === '0') {
              // eslint-disable-next-line no-param-reassign
              item.title = state.title;
              // eslint-disable-next-line no-param-reassign
              item.body = state.body;
            }
            return item;
          });
        });
      });

What am I missing in my test?




