Component test utils

Component test utils

  • Philosophy
  • Doc
  • Blog

›Shallow

Introduction

  • Documentation
  • Getting Started

Shallow

  • shallow/constructor
  • shallow/html
  • shallow/props
  • shallow/setProps
  • shallow/querySelector
  • shallow/querySelectors
  • shallow/exists
  • shallow/dispatchEvent
  • shallow/unmount

Frameworks

  • api-react
  • api-vuejs
  • api-angular
  • api-vanilla

shallow/dispatchEvent

dispatchEvent()

You can also use the trigger alias.

  • Arguments:
    • {string} event the event name
    • {any} options

Example

React

const Component = () => {
  const [nbLikes, setNbLike] = React.useState(0);
  return (
    <button type="button" onClick={() => setNbLike(nbLikes + 1)}>
      {nbLikes}
    </button>
  );
};

const cmp = shallow(<Component />);

cmp.dispatchEvent('click');
// cmp.trigger('click'); is equivalent

expect(cmp.html()).toBe('<button type="button" onClick="[onClick]">1</button>');
← shallow/existsshallow/unmount →
  • dispatchEvent()
    • Example
Component test utils
Docs
Getting StartedAPI Reference
More
BlogGitHubStar
MIT Copyright © 2020 Florent Berthelot