Forked from Twitter's jasmine-flight, and modified to use with webpack instead of requirejs
Extensions to the Jasmine test framework for use with Flight
Install it with Bower:
bower install --save-dev andrewk/webpack-jasmine-flight
jasmine-flight depends on jasmine
jasmine-flight provides a set of helpers to load and instantiate AMD components, mixins and modules.
Instantiates the component at componentReference and executes specDefinitions.
- The component constructor is available from within specDefinitions as
this.Component
- To create a component instance, call
this.setupComponent
describeComponent(require('ui/compose'), specDefinitions);
A function to execute after the component has loaded. Should contain spec definitions.
As per describeComponent, but prevents execution of any other specs.
Requires the mixin at mixinReference and executes specDefinitions.
- The Mixin is attached to a dummy Component
- the dummy Component constructor is available from within specDefinitions as this.Component
- To create a component instance, call
this.setupComponent
Result of requiring mixin. E.g. require('ui/with_close_button')
A function to execute after the mixin has loaded. Should contain spec definitions.
As per describeMixin, but prevents execution of any other specs.
Requires the AMD module at moduleReference and executes specDefinitions
- The module will be available as this.module from within specDefinitions.
Result of requiring a module. E.g. require('utils/time')
A function to execute after the module has loaded. Should contain spec definitions.
As per describeModule, but prevents execution of any other specs.
Instantiate a component or mixin within specDefinitions.
- The component instance is available at
this.component
- The node the component is attached to is
this.$node
Generates a DOM element to attach the component to. If no fixture is provided, the component will be attached to an empty DOM node.
Options to pass to the component.
In almost all cases, describeMixin and describeComponent are effectively identical in their usage, thus only describeComponent is detailed here.
This spec tests a simple component which has one methomd, 'getName', which returns the value of an input field.
describeComponent('ui/text_input', function () {
it ('gets the value of the input field it is attached to', function () {
this.setupComponent('<input type="text" value="hello world" />');
expect(this.component.getValue()).toEqual('hello, world');
});
});
This spec tests a component which has one method, getText
, which gets the text value of an element. It uses options to figure out which element to access.
describeComponent('ui/text', function () {
it ('gets the text of the element specified by elementSelector', function () {
this.setupComponent('<div><p class="js-name">Jimmy</p></div>', {
elementSelector: 'js-name'
});
expect(this.component.getText()).toEqual('Jimmy');
});
});
When testing components, you may want to stub out methods provided by mixins. In this example, we're stubbing a method named 'foo' (which was provided by a mixin) so that it always returns 'bar'.
describeComponent('ui/text', function () {
it ('foo returns "foo"', function () {
this.setupComponent();
var stub = spyOn(this.component, 'foo').andReturn('bar');
expect(this.component.foo()).toEqual('bar');
});
});
It's probably not a good idea to stub out methods on the component you're testing, but if you really, really want to...
describeComponent('ui/text', function () {
it('calls the stub instead', function () {
// spy on the prototype...
var spy = spyOn(this.Component.prototype, 'getText');
// ... and then instantiate the component
this.setupComponent();
expect(spy).toHaveBeenCalled();
});
});
Event Spies are not part of this package but are mentioned here because it's mostly what you'll be wanting to do. spyOnEvent and the associated matchers are provided by https://github.com/velesin/jasmine-jquery
describeComponent('ui/text', function () {
it('triggers 'data-username' after initialize', function () {
spyOnEvent(document, 'data-username');
this.setupComponent({
username: 'bob'
});
expect('data-username').toHaveBeenTriggeredOnAndWith(document {
username: 'bob'
});
});
});
Anyone and everyone is welcome to contribute. Please take a moment to review the guidelines for contributing.
- @esbie and
@skilldrick for creating the original
describeComponent
&describeMixin
methods. - @necolas for ongoing support & development
Copyright 2013 Twitter, Inc and other contributors.
Licensed under the MIT License