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

deku-testutils

v1.0.2

Published

A collection of utilities for testing Deku components

Downloads

18

Readme

deku-testutils Build Status

A collection of Deku test utilities.

Install

npm install deku-testutils

Includes:

API

utils.Mock

Mock(Component)

Returns a wrapper object for the Component. The goal is that there will be many methods that reflect various lifecycle events for the deku component. Currently, we only deal with render, but others will be added over time as we develop good testing strategies.

mock.render(component)

Calls Component.render(). The render function will have all the parameters it would normally expect generated automatically. (eg: props, props.children and state)

This also uses Component.defaultProps and Component.initialState() to ensure the component object is generated accurately.

The setState function that is passed is simply a no-op, it won't trigger any other changes. (as it shouldn't, since this is designed for unit-testing)

utils.assert

assert.isNode(node, [type])

Checks the given node to make sure it looks like a virtual node. If the type is specified, it must match strictly.

assert.isNode(<div />);
assert.isNode(<b>Hello World</b>, 'b');
assert.isNode(<Button>Log In</Button>, Button);

assert.hasAttribute(node, attr, [value])

Checks the given node to make sure it has the given attr attribute. If the value is specified, it must match that value strictly.

assert.hasAttribute(<a href="http://example.com/">Home</a>, 'href');
assert.hasAttribute(<button type="submit">Submit</button>, 'type', 'submit');

When using a Function, it will be invoked with the attribute value. From there, you can run any other assertion that should throw if the value is invalid.

assert.hasAttribute(<Select options={[ 'a', 'b' ]} />, 'options', function (options) {
  assert.deepEqual(options, [ 'a', 'b', 'c' ]); // will fail
});

NOTE: this allows for falsy values, as an attribute can be present but intentionally false, such as checked={false}.

assert.notHasAttribute(node, attr)

Checks the given node to make sure it does not have the given attr attribute.

assert.notHasAttribute(<div />, 'id');

NOTE: this will not throw for falsy values, as an attribute can be present but intentionally false, such as checked={false}.

assert.hasClass(node, name)

Checks that the given node has the given CSS class name. This is largely a helper for HTML elements, although any component that uses class in the same fashion can be checked.

assert.hasClass(<div class="a b c" />, 'b');

assert.notHasClass(node, name)

Checks that the given node does not have the given CSS class name. This is largely a helper for HTML elements, although any component that uses class in the same fashion can be checked.

assert.notHasClass(<div class="a" />, 'b');

assert.hasChildren(node, [children])

Checks that the given node has child nodes matching the children argument:

  • when a Number, it will ensure node has that many child nodes
  • when a Function, it will run the function against each child node (which should throw if they are invalid)
  • when an Array, it will check for loose/deep equality
  • when not specified, it will just make sure the node has at least 1 child
var node = (
  <ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
  </ul>
);

// make sure there are any children
assert.hasChildren(node);

// make sure there are 3 children
assert.hasChildren(node, 3);

// our fn just runs other assertions
assert.hasChildren(node, function (child) {
  assert.isNode(child, 'li');
  assert.hasChildren(child);
});

assert.notHasChildren(node)

Checks that the given node does not have any child nodes.

assert.notHasChildren(<div />);

assert.hasChild(node, index, [criteria])

Check if the given node at a given zero-indexed index has the corresponding child, using the following criteria:

  • When a Function, it will run criteria, passing the child node as an argument. criteria is expected to throw an error if the node is invalid.
  • Otherwise, it will do a deep comparison between the child node and the criteria.
var node = (
  <ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
  </ul>
);

// make sure a child at index 0 exists
assert.hasChild(node, 0);

// do a deep comparison on the child at index 0
assert.hasChild(node, 0, 'div');

// run other assertions on the child node
assert.hasChild(node, 0, function (child) {
  assert.isNode(child, 'li);
});

utils.mount

mount(node)

Mount a Deku node to the DOM and return an object with a property element containing a reference to the rendered DOM element and a property unmount containing a function to remove the rendered DOM element from the DOM.

mountedNode.unmount()

Remove the rendered element from the DOM

mountedNode.element

A reference to the mounted dom element

utils.isNode

isNode(node, [type])

Returns true if node is a valid Deku node. If type is specified, it will ensure that type is strictly equal. (whether that is a Component or a String element name)

utils.findAll

findAll(node, fn)

Returns an array of items found within node that satisfy fn.

utils.findWithClass

findWithClass(node, class)

Returns a single node found within node that has class. This will throw an error if more than one node is found.

utils.findAllWithClass

findAllWithClass(node, class)

Returns an array of nodes found within node that have class.

License

MIT