All Projects → preactjs → Enzyme Adapter Preact Pure

preactjs / Enzyme Adapter Preact Pure

Licence: mit
Preact adapter for the Enzyme UI testing library

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Enzyme Adapter Preact Pure

React Ssr Starter
All have been introduced React environment
Stars: ✭ 20 (-67.74%)
Mutual labels:  enzyme, preact
coderplex
(Old) Frontend for coderplex.org
Stars: ✭ 40 (-35.48%)
Mutual labels:  preact, enzyme
Htm Editable Resume
An highly editable resume form developed with HTM (Hyperscript Tagged Markup) which is JSX-like syntax in plain JavaScript.
Stars: ✭ 31 (-50%)
Mutual labels:  preact
Navi
Open Source Project for Grow with Google Udacity Scholarship Challenge - Navigation app using offline first strategy and google maps api - To get started please refer to the README.md - CONTRIBUTING.md and the project Wiki
Stars: ✭ 51 (-17.74%)
Mutual labels:  preact
Should Enzyme
Useful functions for testing React Components with Enzyme.
Stars: ✭ 41 (-33.87%)
Mutual labels:  enzyme
React Colorful
🎨 A tiny (2,5 KB) color picker component for React and Preact apps
Stars: ✭ 951 (+1433.87%)
Mutual labels:  preact
Push Starter
React Redux Starter with SSR 🤖
Stars: ✭ 43 (-30.65%)
Mutual labels:  enzyme
Slidecontrol
Slidecontrol enables you to control your slides with your phone 📱
Stars: ✭ 30 (-51.61%)
Mutual labels:  preact
Cloudflare Worker Preact Pwa
Cloudflare worker running a Preact Progressive Web App
Stars: ✭ 57 (-8.06%)
Mutual labels:  preact
Whack A Mole
Offline-first arcade game for children
Stars: ✭ 40 (-35.48%)
Mutual labels:  preact
Ts Preact Starter
Barebones starter project for Preact with TypeScript
Stars: ✭ 49 (-20.97%)
Mutual labels:  preact
Razzle Unrouted
Blazingly fast server-rendered MVC Webapps with Preact and Webpack
Stars: ✭ 39 (-37.1%)
Mutual labels:  preact
Wasmboy
Game Boy / Game Boy Color Emulator Library, 🎮written for WebAssembly using AssemblyScript. 🚀Demos built with Preact and Svelte. ⚛️
Stars: ✭ 963 (+1453.23%)
Mutual labels:  preact
Preact Cli Plugin Async
Preact CLI plugin that adds converts async/await to Promises.
Stars: ✭ 44 (-29.03%)
Mutual labels:  preact
Preact Boilerplate
🎸 Ready-to-rock Preact starter project, powered by Webpack.
Stars: ✭ 959 (+1446.77%)
Mutual labels:  preact
Testing Jest Enzyme
List of components and tests for post "React Components Testing with Jest & Enzyme"
Stars: ✭ 54 (-12.9%)
Mutual labels:  enzyme
Enzyme To Json
Snapshot test your Enzyme wrappers
Stars: ✭ 954 (+1438.71%)
Mutual labels:  enzyme
Blaze
⚡ File sharing progressive web app built using WebTorrent and WebSockets
Stars: ✭ 991 (+1498.39%)
Mutual labels:  preact
Shudan
A highly customizable, low-level Preact Goban component.
Stars: ✭ 43 (-30.65%)
Mutual labels:  preact
Mobile Boilerplate
React Native boilerplate (TypeScript, MobX-State-Tree, NativeBase, React Navigation, Enzyme) by Prominent Edge
Stars: ✭ 57 (-8.06%)
Mutual labels:  enzyme

enzyme-adapter-preact-pure

Build Status Greenkeeper badge

This is an adapter to support using the Enzyme UI component testing library with Preact. For documentation, please see the testing guide on the PreactJS website.

Supported Preact versions

Version 3.x of the adapter supports Preact v10+. Earlier versions support both Preact v8 and v10.

Usage

Add the library to your development dependencies:

# If using npm
npm install --save-dev enzyme-adapter-preact-pure

# If using yarn
yarn add --dev enzyme-adapter-preact-pure

Then in the setup code for your tests, configure Enzyme to use the adapter provided by this package:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-preact-pure';

configure({ adapter: new Adapter });

Once the adapter is configured, you can write Enzyme tests for your Preact UI components following the Enzyme docs. The full DOM rendering, shallow rendering and string rendering modes are supported.

Example projects

For runnable example projects, see the examples/ directory. To run the examples locally, clone this repository, then run:

cd examples/<project name>
npm install
npm test

Differences compared to Enzyme + React

The general intent is that tests written using Enzyme + React can be easily made to work with Enzyme + Preact or vice-versa. However there are some differences in behavior between this adapter and Enzyme's React adapters to be aware of:

Shallow rendering

  • When using Enzyme's shallow rendering mode, this adapter always invokes the component's lifecycle methods (componentDidUpdate etc.). The disableLifecycleMethods option is not respected.

  • React's shallow rendering does not create actual DOM nodes. The shallow rendering implemented by this adapter does. It works by simply by rendering the component as normal, except making any child components output only the children passed to them. In other words, during shallow rendering, all child components behave as if they were defined like this:

    function ShallowRenderedChild({ children }) {
      return children;
    }
    

    This means that any side effects that rendered DOM elements have, such as <img> elements loading images, will still execute.

Simulating events

The simulate API does not dispatch actual DOM events in the React adapters, it just calls the corresponding handler. The Preact adapter does dispatch an actual event using element.dispatchEvent(...).

State updates

setState synchronously re-renders the component in React, except in event handlers. Preact on the other hand by default batches together calls to setState within the same tick of the event loop and schedules a render to happen in a future microtask. React's behavior may change in a future release.

To make writing tests easier, the Preact adapter will apply any pending state updates and re-render when:

  • The component is initially rendered by mount or shallow
  • An Enzyme API call is made that is expected to trigger a change in the rendered output, such as wrapper.setProps, wrapper.simulate or wrapper.setState
  • wrapper.update is called explicitly by a test

The consequences of this when writing tests are that any state updates triggered outside of an Enzyme method call will not be reflected in the rendered DOM until wrapper.update is called. Note this function also needs to be called when using React, as it synchronizes Enzyme's snapshot of the output with the actual DOM tree.

Example:

const wrapper = shallow(<ParentComponent/>);

// Trigger a state update outsize of Enzyme.
wrapper.find(ChildComponent).props().onClick();

// Update the Enzyme wrapper's snapshot.
wrapper.update();

// Test that parent component updated as expected.

When using the Hooks API which is available in React 16.8+ and Preact 10, you also need to wrap any code which triggers effects in an act call in order to flush effects and trigger a re-render. The initial render and calls to APIs such as setProps or simulate are automatically wrapped in act for you.

In Preact the act function is available in the "preact/test-utils" package.

import { act } from 'preact/test-utils';

// Any effects scheduled by the initial render will run before `mount` returns.
const wrapper = mount(<Widget showInputField={false}/>);

// Perform an action outside of Enzyme which triggers effects in the parent
// `Widget`. Since Enzyme doesn't know about this, we have to wrap the calls
// with `act` to make effects execute before we call `wrapper.update`.
act(() => {
  wrapper.find(ChildWidget).props().onButtonClicked();
});

// Update the Enzyme wrapper's snapshot
wrapper.update();

Property names

In order to support Enzyme's class selectors, class props on Preact components are mapped to className.

import { mount } from 'enzyme';

const wrapper = mount(<div class="widget"/>);
wrapper.props() // Returns `{ children: [], className: 'widget' }`
wrapper.find('.widget').length // Returns `1`

Usage with preact/compat

This package has the same interface as the official enzyme-adapter-react-$version packages. If you are using preact/compat, you can alias enzyme-adapter-react-$version to this package in the same way as preact/compat.

Usage with TypeScript

This package is compatible with TypeScript and ships with type declarations. In order to mix Enzymes types from @types/enzyme with Preact, you will need to include some extensions to the "preact" types which are provided by this project.

To do that, add the following line to one of the source files or .d.ts files for your project:

/// <reference types="enzyme-adapter-preact-pure" />

See the TypeScript example in examples/typescript for a runnable example.

Development

After cloning the repository, you can build it and run tests as follows:

# Install dependencies.
yarn install

# Build the adapter library.
yarn build

# Run tests.
yarn test

# Run tests against a custom build of Preact.
yarn test --preact-lib <path to Preact bundle>

FAQ

Can I use this library to test components that use hooks?

Yes. This library supports components that use the "Hooks" APIs available in Preact v10+. You may need to use the act function from preact/test-utils to flush effects synchronously in certain places. See the notes above about state updates in tests.

Why does the package name have a "-pure" suffix?

The name has a "-pure" suffix to distinguish it from enzyme-adapter-preact package which indirectly depends on React. This library is a "pure" Preact adapter which does not require Preact's React compatibility add-on.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].