react-jest-snapshot-helper
Helper class to simplify Jest snapshot testing of React components with a variety of props combinations.
Install
npm install --save-dev react-jest-snapshot-helper
...or...
yarn add --dev react-jest-snapshot-helper
Usage
;; // constructor "remembers" component type and propsconst snapshotHelper = <FacultyMember name="Test Name" role=ROLE_TEACHER speciality= subject: 'Math' accredited: true /> ; ;
getSnapshot()
...returns a JSON snapshot string for the component. As with the test function, it accepts an optional propAdjustments argument:
const teacherSnapshot = snapshotHelper;const principalSnapshot = snapshotHelper;
withPropsAdjuster()
SnapshotHelper has an adjustProps function that is called when propsAdjustments are passed to test() or getSnapshot(). By default, this function does an Object.assign against the constructor-supplied "top-level" props properties, e.g.:
adjustedProps = Object.assign({}, this.props, propsAdjustments);`
withPropsAdjuster() is a fluent syntax convenience function for overriding the default adjustProps function. This is useful for simpler syntax for overriding only part of a hierarchial props object.
const snapshotHelper = <FacultyMember ...defaultProps />; // ^ Override adjustor to override "child" specialty properties, not parent props: ;
Static functions
If you don't need the ability to test a variety of props, you can just use the static getSnapshot and test methods:
;
(The static calls above are the equivalent of the following:)
const snapshot = <FacultyMember ...defaultProps />; <FacultyMember ...defaultProps />;