Skip to content

Commit

Permalink
Merge pull request #633 from BitLucid/js_unit_testing
Browse files Browse the repository at this point in the history
Js unit testing
  • Loading branch information
tchalvak committed Mar 24, 2016
2 parents 92c4da5 + 66ceb4a commit 62b5a07
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ Icon
.TemporaryItems
.Trashes
.VolumeIcon.icns
node_modules/
node_modules/
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all ci pre-test test test-main test-integration test-unit test-functional post-test clean dep build install dist-clean db db-fixtures migration
.PHONY: all ci pre-test test test-main test-integration test-unit test-functional test-js post-test clean dep build install dist-clean db db-fixtures migration

DOMAIN=http://nw.local/
COMPOSER=./composer.phar
Expand Down Expand Up @@ -83,6 +83,9 @@ test-cron-run:
test-functional:
python3 -m pytest deploy/tests/functional/

test-js:
karma start deploy/tests/karma.conf.js --browsers PhantomJS --single-run

test-ratchets:
#split out for ci for now
python3 -m pytest deploy/tests/functional/test_ratchets.py
Expand All @@ -103,11 +106,13 @@ clean:

dep:
@$(COMPOSER) install
@npm install

dist-clean: clean
@rm -rf ./vendor/*
@rm -rf "$(COMPONENTS)"
@rm -rf "$(SRC)resources/"logs/*
@rm -rf ./node_modules
@echo "Done"
@echo "You'll have to dropdb $(DBNAME) yourself."

Expand Down Expand Up @@ -171,12 +176,14 @@ web-reload:
ci-pre-configure:
# Set php version through phpenv. 5.3, 5.4 and 5.5 available
phpenv local 5.5
ln -s `pwd` /tmp/root
#precache composer for ci
composer config -g github-oauth.github.com $(GITHUB_ACCESS_TOKEN)
composer install --prefer-dist --no-interaction
# Set up the resources file, replacing first occurance of strings with their build values
sed -i "0,/postgres/{s/postgres/${DBUSER}/}" deploy/resources.build.php
#eventually that sed should be made to match only the first hit
sed -i "s|/srv/ninjawars/|../..|g" deploy/tests/karma.conf.js
rm -f $(WWW)js/jquery-linkify.min.js #delete bad component linkage
ln -s resources.build.php deploy/resources.php
# Set up selenium and web server for browser tests
#wget http://selenium-release.storage.googleapis.com/2.42/selenium-server-standalone-2.42.2.jar
Expand All @@ -196,4 +203,4 @@ python-install:

ci: ci-pre-configure build python-install test-unit db-init db db-fixtures

ci-test: pre-test test-main test-cron-run test-ratchets post-test
ci-test: pre-test test-main test-cron-run test-ratchets test-js post-test
11 changes: 11 additions & 0 deletions deploy/tests/js/ArraySpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict"; // Strict checking.
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
var res = [1,2,3].indexOf(5);
expect(res).toEqual(-1);
expect([1,2,3].indexOf(0)).toEqual(-1);
expect([1,2,3].indexOf(3)).toEqual(2);
});
});
});
24 changes: 24 additions & 0 deletions deploy/tests/js/DebugSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict"; // Strict checking.
describe('Debug', function() {
describe('dump()', function() {
it('should return formatted var text of arguments', function() {
var arr = {jim:'bob'};
var res = dump(arr, 0);
expect(res).not.toEqual('');
});
});
});
describe('Echo', function() {
describe('echo()', function() {
it('should just return a parameter passed in', function() {
var res = testEcho('something');
expect(res).toEqual('something');
});
});
});

describe('Hello world', function(){
it('says hello', function(){
expect(helloWorld()).toEqual('hello world');
});
});
39 changes: 39 additions & 0 deletions deploy/tests/js/NwSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict"; // Strict checking.


/*require(['jquery'], function($) {
$('body').css('background-color', 'green');
});*/
/*requirejs(["nw.js"], function(NW) {
//Called when nw.js is included
//This function is called when scripts/helper/util.js is loaded.
//If util.js calls define(), then this function is not fired until
//util's dependencies have loaded, and the util argument will hold
//the module value for "helper/util".
});*/

describe('NW', function() {
describe('NW App testing context', function() {
var app = NW;
var env = environment;
beforeEach(function() {
});

afterEach(function() {
});

it('should be able to see var defined in nw.js', function() {
expect(env).toBeDefined();
expect(env).toMatch(/NW.+context/);
});
it('should be able to get the NW object', function() {
expect(app).toBeDefined();
});
it('should have access to the location checks', function() {
expect(g_isIndex).toBeDefined();
expect(g_isRoot).toBeDefined();
expect(g_isSubpage).toBeDefined();
});
});
});
85 changes: 85 additions & 0 deletions deploy/tests/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Karma configuration
// Generated on Sun Mar 20 2016 07:47:48 GMT-0400 (EDT)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../..',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'requirejs'],


// list of files / patterns to load in the browser
files: [
'test-main.js',
'deploy/www/js/jquery.min.js',
'deploy/www/js/jquery.timeago.js',
'deploy/www/js/nw.js',
'deploy/www/js/debug.js',
'deploy/www/js/passwords.js',
'deploy/www/js/talk.js',
'deploy/www/js/casino.js',
'deploy/www/js/disagreement.js',
{pattern: 'deploy/www/js/*.js', included: false},
{pattern: 'deploy/tests/js/*Spec.js', included: false}
],


// list of files to exclude
exclude: [
'deploy/www/js/jquery.timeago.js',
'deploy/www/js/jquery.linkify.js',
'deploy/www/js/jquery.linkify.min.js',
'deploy/www/js/casino.js',
'deploy/www/js/disagreement.js',
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
});
//console.log(config);
}
134 changes: 71 additions & 63 deletions deploy/www/js/debug.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,74 @@
function simple_var_dump(obj) {

try {
console.log('simple_var_dump: ', obj);
alert("Firebug dump successful.");
} catch(e) {
alert("You don't have Firebug enabled!");
dump = '';
if(typeof obj == "object") {
dump += "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
} else {
dump += "Type: "+typeof(obj)+"\nValue: "+obj;
}
alert(dump);
}

/*Simple array:
var myVar = {
key1 : 'value1',
key2 : 'value2',
key3 : ['a', 'b', 'c']
}; */
}//end function var_dump


/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
* The level - OPTIONAL
* Returns : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
* Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}



function simple_var_dump(obj) {

try {
console.log('simple_var_dump: ', obj);
alert("Firebug dump successful.");
} catch(e) {
alert("You don't have Firebug enabled!");
dump = '';
if(typeof obj == "object") {
dump += "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
} else {
dump += "Type: "+typeof(obj)+"\nValue: "+obj;
}
alert(dump);
}

/*Simple array:
var myVar = {
key1 : 'value1',
key2 : 'value2',
key3 : ['a', 'b', 'c']
}; */
}//end function var_dump

function testEcho(param) {
return param;
}

function helloWorld(){
return 'hello world';
}


/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
* The level - OPTIONAL
* Returns : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
* Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}



/**
* Concatenates the values of a variable into an easily readable string
* by Matt Hackett [scriptnode.com]
Expand Down
Loading

0 comments on commit 62b5a07

Please sign in to comment.