All Projects â†’ mantoni â†’ choo-test

mantoni / choo-test

Licence: MIT license
🚂🚋🚋🚋 Easy choo app unit testing

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to choo-test

stream
Monorepo for Resonate Coop player
Stars: ✭ 164 (+613.04%)
Mutual labels:  choo
Choo
🚂🚋 - sturdy 4kb frontend framework
Stars: ✭ 6,637 (+28756.52%)
Mutual labels:  choo
component-box
A little component cacher 📦
Stars: ✭ 25 (+8.7%)
Mutual labels:  choo
choo-pwa
PWA with Choo
Stars: ✭ 18 (-21.74%)
Mutual labels:  choo
cache-component
DEPRECATED: Use nanocomponent. Will continue to operate as normal
Stars: ✭ 16 (-30.43%)
Mutual labels:  choo
choo-offline
🔧 offline first support for choo apps
Stars: ✭ 12 (-47.83%)
Mutual labels:  choo
awesome-yo-yo
Modules & resources related to yo-yo
Stars: ✭ 27 (+17.39%)
Mutual labels:  choo
nanoconstruct
Tiny tool to test and develop nanocomponents
Stars: ✭ 12 (-47.83%)
Mutual labels:  choo
olaf
A P2P chat using Dat.
Stars: ✭ 24 (+4.35%)
Mutual labels:  choo
hyperscript-attribute-to-property
Convert hyperscript attributes to properties
Stars: ✭ 19 (-17.39%)
Mutual labels:  choo

Choo Test

SemVer License

Easy Choo testing for Choo v5, v6 and v7.

Install

$ npm install choo-test --save-dev

Usage

Here is an example using Mochify as the test runner:

var assert = require('assert');
var choo = require('choo');
var html = require('choo/html');
var test = require('choo-test');

function model(state, emitter) {
  state.text = 'Test';

  emitter.on('change', () => {
    state.text = 'Changed';
    emitter.emit('render');
  });
}

function view(state, emit) {
  return html`<button onclick=${function () {
    emit('change');
  }}>${state.text}</button>`;
  
}

describe('choo-app', function () {
  var restore;
  var app;

  beforeEach(function () {
    app = choo();
    app.use(model);
    app.route('/', view);
  });

  afterEach(function () {
    restore();
  });

  it('changes the button text on click', function (done) {
    restore = test.start(app);

    test.fire('button', 'click');

    test.onRender(function () {
      assert.equal(test.$('button').innerText, 'Changed');
      done();
    });
  });

});

How does it work?

This module is a collection of helper functions. Each of them can be used separately.

When you use the start function to start your Choo app, it wraps and appends the application to a div tag in the document.body. When calling the returned restore function, the DOM node is removed again.

The onRender function creates a MutationObserver and invokes the given callback if any change in the DOM tree happens.

Global window events are captured and unregistered when calling restore().

API

  • $(selector[, scope]): Find a DOM element using querySelector. scope must be a DOM node to search and defaults to document.
  • $$(selector[, scope]): Find all DOM element using querySelectorAll. scope must be a DOM node to search and defaults to document.
  • fire(selector, event[, args]): Fire an event using bean.fire.
  • onRender([nodeOrSelector, ]fn): Register a function to invoke after the next DOM mutation occurred. If only a function is given, the entire document is observed. If no mutation occurs within 1500 ms, a timeout error is thrown.
  • start(app): Creates a div tag and append it to document.body, then starts the given Choo app and attaches the returned tree to the div node. Returns a restore() function which remove the div node from the body.

Testing XHR

Use the Sinon.js fake server for XHR testing. If you're using the xhr library, you have to initialize the XMLHttpRequest implementation like this:

sandbox = sinon.sandbox.create({
  useFakeServer: true
});
sandbox.stub(xhr, 'XMLHttpRequest', sandbox.server.xhr);

License

MIT

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].