(Asynchronous Module Definition) API //my React class define(['react','reactComponent1','reactComponent2', ‘myStore’], function(React,Component1,Component2, MyStore){ var myReactComponent = React.createClass({ //Reacty stuff here that uses Component1, Component2 and MyStore }); return myReactComponent; });
• Doesn’t require a DOM describe("My Billing Form", function() { it("should default form first name", function() { //use various matchers jasmine provides to assert results expect(myForm.firstName).toBe("myDefaultValue"); }); });
server and executes test code against source code • Supports multiple browsers (Full List) • Displays results of tests (stating the obvious) • Plugs in well with several CI build tools
will be used to resolve all patterns (eg. files, exclude) basePath: '', //points to current directory // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine', 'requirejs'], // list of files / patterns to load in the browser files: [ {pattern: 'libs/**/*.js', included: false}, {pattern: 'app/**/*.js', included: false}, {pattern: 'test/**/*Spec.js', included: false}, 'test/main-test.js'], // list of files to exclude exclude: [ 'app/main.js', 'libs/require.js'], //available launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: [ 'PhantomJS', 'Chrome', 'Firefox'], //other properties below }); };
props.isAdmin = false; var container = ReactTestUtils.renderIntoDocument(MyLinkComponent(props)), //traverse components in container and return components where function eval is true myLinks = React.addons.TestUtils. findAllInRenderedTree(container, function(component){ return component.getDOMNode() && component.getDOMNode().text === props.text; }); expect(myLinks.length).toBe(0); }); it('should display link with class title-options', function(){ var container = ReactTestUtils.renderIntoDocument(MyLinkComponent(props)), //scry returns all instances, use find for only one instance. myDiv = ReactTestUtils.scryRenderedDOMComponentsWithClass(container,'title-options'); expect(myDiv.length).toBe(1); }); it('should change state when link is clicked', function(){ var container = ReactTestUtils.renderIntoDocument(MyLinkComponent(props)); ReactTestUtils.Simulate.click( ReactTestUtils.findRenderedDOMComponentWithTag(container, 'a')); expect(container.state.hasBeenClicked).toBe(true); }); MyLinkComponentSpec.js
{ // Empty out data when new user is set case ActionTypes.RESET_USER: _setUser(false); UserStore.emitChange(); break; case ActionTypes.RECEIVE_USER: _setUser(action.user); UserStore.emitChange(); break; default: // do nothing } }); return UserStore; });