npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snabbdom-testing-library

v0.0.1

Published

Test Snabbdom with dom-testing-library.

Downloads

3

Readme

snabbdom-testing-library

A tiny tool to test Snabbdom virtual DOM nodes using dom-testing-library.

npm i snabbdom-testing-library --save-dev

The Snippet

// The Snabbdom helper function *h* that creates VDOM nodes
// and the *patch* function that applies VDOM nodes to a container. 
// The Snabbdom documentation shows how to configure these two functions.
import {h, patch} from './my-snabbdom-configuration';

// Only the *makeRender* is a *snabbdom-testing-library* specific function.
// It creates a *render* function that renders VDOM nodes.
// It's stateful and works exactly like a real Snabbdom client code, that is
// the first call applies the given VDOM node to a new DIV element,
// while the succeeding calls apply VDOM nodes to the previous VDOM nodes.
// The rest of the exported functions are functions from *dom-testing-library*.
import {makeRender, fireEvent, queryByText} from 'snabbdom-testing-library';
// In this example we are using Sinon test doubles to test event handlers.
import sinon from 'sinon';

// The test *render* function is created based on our own configuration
// of the *patch* function. That way we may use any modules we want.
const render = makeRender({patch});

// Test doubles of event handlers.
const btn1OnClick = sinon.fake();
const btn2OnClick = sinon.fake();

// A div with two buttons.
const vnode = h('div', [
  h('button', {
    on: {click: btn1OnClick}
  }, '1st'),
  h('button', {
    on: {click: btn2OnClick}
  }, '2nd'),
]);

// *container* is the DOM element where the given VDOM node is rendered.
// *getByText* is the *dom-testing-library* query function scoped to the *container*,
// so that we don't need to pass it the *container* every single time.
const {getByText, container} = render(vnode);

// This will get the first button within the *container*.
// Please notice this *getByText* function comes from the *render* function call.
const btn1 = getByText('1st');

// This will get the second button within the *container*.
// Plese notice this *queryByText* function 
// is imported from *snabbdom-testing-library* 
// and is not scoped to the *container*.
const btn2 = queryByText(container, /2/);

// None of the buttons has been clicked.
expect(btn1OnClick.called).toBeFalsy();
expect(btn2OnClick.called).toBeFalsy();

// A simulated click.
fireEvent.click(btn1);

// Just the first button has been clicked.
expect(btn1OnClick.calledOnce).toBeTruthy();
expect(btn2OnClick.called).toBeFalsy();

// Another simulated click.
fireEvent.click(btn2);

// Both of the buttons have already been clicked.
expect(btn1OnClick.calledOnce).toBeTruthy();
expect(btn2OnClick.calledOnce).toBeTruthy();

More about testing

Because this library is nothing but a tiny wrapper around dom-testing-library, please refer to its documentation.

Valuable resources may also be found in the README of the react-testing-library library.

Running without Jest

You may need jsdom-global.