Skip to content

Extensions to the Mocha test framework for use with Flight

License

Notifications You must be signed in to change notification settings

flightjs/mocha-flight

Repository files navigation

mocha-flight Build Status

Extensions to the Mocha test framework for use with Flight

Installation

We recommend that you use Bower:

bower install --save-dev mocha-flight

Alternatively, you can include mocha-flight.js in your app and load it in your test runner.

N.B. mocha-flight depends on Mocha

Usage

These examples use the chai.js assertion library.

Components

describeComponent('path/to/component', function () {
  beforeEach(function () {
    setupComponent();
  });

  it('should do x', function () {
    // a component instance is now accessible as `this.component`
    // the component root node is attached to the DOM
    // the component root node is also available as this.$node
  });
});

Mixins

describeMixin('path/to/mixin', function () {
  // initialize the component and attach it to the DOM
  beforeEach(function () {
    setupComponent();
  });

  it('should do x', function () {
    expect(this.component.doSomething()).to.equal(expected);
  });
});

Event spy

You will need to use a library like sinon.js.

describeComponent('path/to/component', function () {
  beforeEach(function () {
    setupComponent();
  });

  it('should do x', function () {
    sinon.stub(this.component, 'method');

    // You need to initialize the component after stubbing the function so the
    // event will be bound and call the stubbed callback function
    initializeComponent();

    $(document).trigger('EVENT_NAME');
    this.component.method.calledOnce.should.be.true;
    done();
  });
});

setupComponent

setupComponent(optionalFixture, optionalOptions);

Calling setupComponent twice will create an instance, tear it down and create a new one.

HTML Fixtures

describeComponent('ui/twitter_profile', function () {
  // is the component attached to the fixture?
  it('this.component.$node has class "foo"', function () {
    setupComponent('<span class="foo">Test</span>');
    expect(this.component.$node.find('span').hasClass('foo')).to.be.ok();
  });
});

Component Options

describeComponent('data/twitter_profile', function() {
  // is the option set correctly?
  it('this.component.attr.baseUrl is set', function() {
    setupComponent({
      baseUrl: 'http://twitter.com/1.1/'
    });
    expect(this.component.attr.baseUrl).to.equal('http://twitter.com/1.1/');
  });
});

Teardown

Components are automatically torn down after each test.

Contributing to this project

Anyone and everyone is welcome to contribute. Please take a moment to review the guidelines for contributing.

License

Copyright (c) 2013 Naoya Inada naoina@kuune.org

Licensed under the MIT License

About

Extensions to the Mocha test framework for use with Flight

Resources

License

Stars

Watchers

Forks

Packages

No packages published