16.15.0 • Published 2 years ago

react-shallow-renderer v16.15.0

Weekly downloads
619,142
License
MIT
Repository
github
Last release
2 years ago

react-shallow-renderer

GitHub license npm version CircleCI

When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.

Installation

# npm
npm install react-shallow-renderer --save-dev

# Yarn
yarn add react-shallow-renderer --dev

Usage

For example, if you have the following component:

function MyComponent() {
  return (
    <div>
      <span className="heading">Title</span>
      <Subcomponent foo="bar" />
    </div>
  );
}

Then you can assert:

import ShallowRenderer from 'react-shallow-renderer';
// in your test:
const renderer = new ShallowRenderer();
renderer.render(<MyComponent />);
const result = renderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
  <span className="heading">Title</span>,
  <Subcomponent foo="bar" />,
]);

Shallow testing currently has some limitations, namely not supporting refs.

Note:

We also recommend checking out Enzyme's Shallow Rendering API. It provides a nicer higher-level API over the same functionality.

Reference

shallowRenderer.render()

You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.

shallowRenderer.render() is similar to ReactDOM.render() but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.

shallowRenderer.getRenderOutput()

After shallowRenderer.render() has been called, you can use shallowRenderer.getRenderOutput() to get the shallowly rendered output.

You can then begin to assert facts about the output.

@wouterds/react-native-tvoseasy-select-rnreact-native-shekhar-bridge-testwilscanner@oiti/documentoscopy-react-nativequoc-testluminos-ui-corereact-native-macosreact-nativereact-native-windows@everything-registry/sub-chunk-2611jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgreact-native-jawwy_sample@amirdiafi/react-native-ios-haptics@assystant/firebase@ali5049/react-native-buttons@azalpacir/react-native-dhp-printer@aysea/react-native-ui-library@con-test/react-native-concent-common@baloochat/react-native-svg-uri@cs6/react-native-test-native-view-library@damruravihara/react-native-testing-package@damian.lnc/core@blusalt-sdk/react-native-blusalt-document-verification@brantalikp/rn-resize@callstack/react-native-visionosjawwy_library_newjawy_library_v1gamification-jawwy-libraryframework_test_library_sixdee_new_jawwyreact-credoreact-native-app-bubblereact-native-app-integrity-checksumreact-native-azure-communication-servicesreact-native-basic-appreact-native-basic-screenreact-native-auth-service-clientreact-native-aventonfacetec-aventonreact-native-awesome-android-123react-native-awesome-android-123-zeotapreact-native-awesome-module-latestreact-native-awesome-module-tworeact-native-animate-textreact-native-android-video-player-viewreact-native-bleccs-componentsreact-native-dimensions-layoutreact-native-dishreact-native-bridge-packagereact-native-bluetooth-device-detectreact-native-ctp-odpreact-native-expendable-viewreact-native-fedlight-dsmreact-native-get-countriesreact-native-ghn-ekycreact-native-dhp-printerreact-native-conekta-card-tokenizerreact-native-components-designreact-native-arps-authorize-netreact-native-check-componentreact-native-chenaarreact-native-build-vesion-getterreact-native-additionreact-native-rn-icons-libraryreact-native-rn-appreact-native-shared-gesturereact-native-smartcall-videodemoreact-native-onrampreact-native-rabbitmq-allreact-native-screen-idle-timerreact-native-rtn-ips-poslin-testreact-native-simple-timelinereact-native-sixdee_test_libreact-native-responsive-helperreact-native-sandycomponentreact-native-return-usb-datareact-native-savczuk-feature-libraryreact-native-sp-test-commonreact-native-scroll-tab-to-indexreact-native-module-for-testingreact-native-native-ios-test1react-native-native-audio-enginereact-native-swalreact-native-rom-componentsreact-native-volume-phisicalreact-native-wtfreact-native-withframework-checkreact-native-xprinter-thermal-blereact-native-xiaomi-permissionsreact-native-test-viewreact-native-test-comlibraryreact-native-tone-frameworkreact-native-trackierreact-native-tvosreact-native-ui-components-libraryreact-native-uvc-camera-android
16.15.0

2 years ago

16.14.1

3 years ago

16.14.0

3 years ago

16.13.1

4 years ago

16.13.0

4 years ago

16.12.0

4 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago