Difference between revisions of "Javascript jasmine"

From John Freier
Jump to: navigation, search
Line 92: Line 92:
 
     );
 
     );
 
   });
 
   });
 +
 +
 +
== RequireJS Knockout ==
 +
To test your Knockout modules with Jasmine-node you just have you install knockoutjs through NPM and it use require to load your module you want to test.
 +
 +
1.  Install Knockout in to your node modules.
 +
  npm install knockout
 +
 +
2.  Just include your module that is already requiring Knockout.
 +
 +
Test
 +
 +
require(['requireModel.js'], function(RequireModel) {
 +
 +
describe('Require Model Tests', function() {
 +
 +
it('should default to require 1650', function() {
 +
 +
var model = new RequireModel();
 +
 +
expect(model.id()).toBe(1650);
 +
expect(model.getRequire()).toBe('/require/1650');
 +
 +
});
 +
});
 +
});
 +
 +
Module (requireModule.js)
 +
 +
  define(
 +
  [
 +
  /* No dependencies */
 +
  'knockout'
 +
  ],
 +
  function(ko) {
 +
 
 +
  function RequireModel() {
 +
  //...
 +
  this.urlContext = '/require/';
 +
 
 +
  this.id = ko.observable(1650);
 +
 
 +
  this.getRequire = function() {
 +
  return this.urlContext + this.id();
 +
  };
 +
  }
 +
 
 +
  // Define the class methods.
 +
  RequireModel.prototype = {};
 +
 
 +
  // Return the module constructor.
 +
  return( RequireModel );
 +
  }
 +
  );

Revision as of 11:20, 28 May 2015

RequireJS

Using RequireJS with Jasmine is totally possible.

1. To load in your modules to test, you must tell Jasmine-node to run using requireJS and not node-require.

  jasmine-node --runWithRequireJs --requireJsSetup etc/spec/requirejs-setup.js etc/spec/ --captureExceptions

2. Need to setup the require-setup.js. This require the use of coping two files from github.

https://github.com/mhevery/jasmine-node/blob/master/spec-requirejs/requirejs-setup.js

and

https://github.com/mhevery/jasmine-node/blob/master/spec-requirejs/requirejs-wrapper-template.js

3. Just setup you test. and done.

 require(['classService.js'], function(ClassService) {
 	
 	describe('ClassService Tests', function() {
 		
 		it('should default', function() {
 			
 			var classService = new ClassService();
 			
 			expect(classService.getName()).toBe('Ok');
 			
 		});		
 	
 	});
 	
 });


RequireJS and JQuery

To setup Jasmine to run unit test agains modules that use jquery you need to first import the jquery package through NPM. Then you need to set up a headless browser using JSDom and then attach it to the module you want to test.

1. Install NPM require packages.

 npm install jquery
 npm install jsdom

2. Write your test and include headless DOM.

 require(['jquery', 'jsdom', 'jqueryService.js'], function($, jsdom, JQueryService) {
 	
 	describe('JQueryService Tests', function() {
 		
 		it('should default', function() {
 			
 			// Create headless DOM model.
 			var markup = '<html><body><button id="nextButton">Hello</button></body></html>';
 			var myWindow = jsdom.jsdom(markup).parentWindow;
 			$ = new $(myWindow);
 			
 			var jqueryService = new JQueryService($);
 
 			jqueryService.changeButtonId();
 			
 			expect($("#nextButton").html()).toBe('Ok');
 			
 		});		
 	
 	});

3. Here is the example module that is being tested.

 define(
 	[
 	 	'jquery'
 	],
 	function($) {
 	
 		 function JQueryService(jq) {
 			 
 			 if(jq) $ = jq;
 
 			 this.changeButtonId = function() {
 				 $('#nextButton').html('Ok');
 			 }
 			 
 		 }
 		 
 		 // Define the class methods.
 		 JQueryService.prototype = {};
 		 
 		 // Return the module constructor.
 		 return( JQueryService );
 		
 	}
   );
 });


RequireJS Knockout

To test your Knockout modules with Jasmine-node you just have you install knockoutjs through NPM and it use require to load your module you want to test.

1. Install Knockout in to your node modules.

 npm install knockout

2. Just include your module that is already requiring Knockout.

Test

require(['requireModel.js'], function(RequireModel) {

describe('Require Model Tests', function() {

it('should default to require 1650', function() {

var model = new RequireModel();

expect(model.id()).toBe(1650); expect(model.getRequire()).toBe('/require/1650');

}); }); });

Module (requireModule.js)

 define(
 	[
 	 	/* No dependencies */
 	 	'knockout'
 	],
 	function(ko) {
 	
 		 function RequireModel() {
 			 //...
 			 this.urlContext = '/require/'; 
 
 			 this.id = ko.observable(1650);
 			 
 			 this.getRequire = function() {
 				return this.urlContext + this.id();
 			 };
 		 }
 		 
 		 // Define the class methods.
 		 RequireModel.prototype = {};
 
 		 // Return the module constructor.
 		 return( RequireModel );
 	}
 );