__Forked from Twitter's jasmine-flight, and modified to use with webpack instead of requirejs__
npm install webpack-jasmine-flightExtensions to the Jasmine test framework for use with Flight
Install it with Bower:
``bash`
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.Componentthis.setupComponent
* To create a component instance, call
#### componentReference: Function
`javascript`
describeComponent(require('ui/compose'), specDefinitions);
#### specDefinitions: Function
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
#### mixinReference: Function
Result of requiring mixin. E.g. require('ui/with_close_button')
#### specDefinitions: Function
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.
#### moduleReference: mixed
Result of requiring a module. E.g. require('utils/time')
#### specDefinitions: Function
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.componentthis.$node
* The node the component is attached to is
#### fixture: String | jQuery
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: Object
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.
`javascript`
describeComponent('ui/text_input', function () {
it ('gets the value of the input field it is attached to', function () {
this.setupComponent('');
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.
` Jimmyjavascript`
describeComponent('ui/text', function () {
it ('gets the text of the element specified by elementSelector', function () {
this.setupComponent('', {
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'.
`javascript`
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...
`javascript
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
`javascript`
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.
* Bug reports
* Feature requests
* Pull requests
* @tbrd
* webpack modifications by @andrewk
* @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