Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dekujs/component-mock

A helper wrapper used for unit-testing deku components
https://github.com/dekujs/component-mock

Last synced: 3 months ago
JSON representation

A helper wrapper used for unit-testing deku components

Awesome Lists containing this project

README

        

# component-mock

> A wrapper for deku components to facilitate easier unit-testing.

## Usage

```js
var Mock = require('component-mock');

var Component = {
defaultProps: {
name: 'a'
},
render: function ({ props }) {
return

{props.name}
;
}
}

var mock = Mock(Component);

var node = mock.render();
assert.isNode(node, 'div');
assert.hasChildren(node, [ 'a' ]);

var node = mock.render({ props: { name: 'b' } });
assert.isNode(node, 'div');
assert.hasChildren(node, [ 'b' ]);
```

## 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)