File tree Expand file tree Collapse file tree 7 files changed +126
-0
lines changed Expand file tree Collapse file tree 7 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 1+ node_modules /
Original file line number Diff line number Diff line change 1+ ## Getting Started with JavaScript Unit Testing Using Mocha
2+
3+ Code to accompany the following article: http://www.sitepoint.com/unit-test-javascript-mocha-chai
4+
5+ To run the code samples:
6+
7+ - Clone the repo
8+ - Run ` npm install `
9+ - Open ` testrunner.html ` in browser
10+ - Alternatively, uncomment Node specific code and run ` mocha ` from the command line (this requires Mocha to be installed globally).
11+
12+ License MIT
Original file line number Diff line number Diff line change 1+ // Browser version
2+ function addClass ( el , newClass ) {
3+ if ( el . className . indexOf ( newClass ) !== - 1 ) {
4+ return ;
5+ }
6+
7+ if ( el . className !== '' ) {
8+ //ensure class names are separated by a space
9+ newClass = ' ' + newClass ;
10+ }
11+
12+ el . className += newClass ;
13+ }
14+
15+ // Node Version
16+ // module.exports = {
17+ // addClass: function(el, newClass) {
18+ // if(el.className.indexOf(newClass) !== -1) {
19+ // return;
20+ // }
21+
22+ // if(el.className !== '') {
23+ // //ensure class names are separated by a space
24+ // newClass = ' ' + newClass;
25+ // }
26+
27+ // el.className += newClass;
28+ // }
29+ // }
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " mocha-unit-testing" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " Mocha Tests" ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " mocha"
8+ },
9+ "author" : " Jani Hartikainen" ,
10+ "license" : " MIT" ,
11+ "devDependencies" : {
12+ "chai" : " ^3.4.1" ,
13+ "mocha" : " ^2.3.4"
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ // Uncomment if using Node
2+ // var chai = require('chai');
3+ var assert = chai . assert ;
4+
5+ describe ( 'Array' , function ( ) {
6+ it ( 'should start empty' , function ( ) {
7+ var arr = [ ] ;
8+
9+ assert . equal ( arr . length , 0 ) ;
10+ } ) ;
11+ } ) ;
Original file line number Diff line number Diff line change 1+ // Uncomment if using Node
2+ // var chai = require('chai');
3+ var assert = chai . assert ;
4+
5+ // var className = require('../js/className.js');
6+ // var addClass = className.addClass;
7+
8+ describe ( 'addClass' , function ( ) {
9+ it ( 'should add class into element' , function ( ) {
10+ var element = { className : '' } ;
11+
12+ addClass ( element , 'test-class' ) ;
13+
14+ assert . equal ( element . className , 'test-class' ) ;
15+ } ) ;
16+
17+ it ( 'should not add a class which already exists in element' , function ( ) {
18+ var element = { className : 'exists' } ;
19+
20+ addClass ( element , 'exists' ) ;
21+
22+ var numClasses = element . className . split ( ' ' ) . length ;
23+ assert . equal ( numClasses , 1 ) ;
24+ } ) ;
25+
26+ it ( 'should append new class after existing one' , function ( ) {
27+ var element = { className : 'exists' } ;
28+
29+ addClass ( element , 'new-class' ) ;
30+
31+ var classes = element . className . split ( ' ' ) ;
32+ assert . equal ( classes [ 1 ] , 'new-class' ) ;
33+ } ) ;
34+ } ) ;
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html >
3+ < head >
4+ < title > Mocha Tests</ title >
5+ < link rel ="stylesheet " href ="node_modules/mocha/mocha.css ">
6+ </ head >
7+ < body >
8+ < div id ="mocha "> </ div >
9+ < script src ="node_modules/mocha/mocha.js "> </ script >
10+ < script src ="node_modules/chai/chai.js "> </ script >
11+ < script > mocha . setup ( 'bdd' ) </ script >
12+
13+ <!-- load code you want to test here -->
14+ < script src ="js/className.js "> </ script >
15+
16+ <!-- load your test files here -->
17+ < script src ="test/arrayTest.js "> </ script >
18+ < script src ="test/classNameTest.js "> </ script >
19+
20+ < script >
21+ mocha . run ( ) ;
22+ </ script >
23+ </ body >
24+ </ html >
You can’t perform that action at this time.
0 commit comments