diff --git a/index.js b/index.js index e0652bf..015bfa0 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ module.exports.view = { boundary_rect: require('./view/boundary_rect'), boundary_country: require('./view/boundary_country'), sort_distance: require('./view/sort_distance'), + sort_popularity: require('./view/sort_popularity'), sources: require('./view/sources'), layers: require('./view/layers'), boundary_gid: require('./view/boundary_gid') diff --git a/test/run.js b/test/run.js index 4ca0ebd..9e83287 100644 --- a/test/run.js +++ b/test/run.js @@ -31,6 +31,7 @@ var tests = [ require('./view/population.js'), require('./view/population_only_function.js'), require('./view/sort_distance.js'), + require('./view/sort_popularity.js'), require('./view/sources.js'), require('./view/boundary_gid.js'), require('./view/leaf/match.js'), diff --git a/test/view/sort_distance.js b/test/view/sort_distance.js index c2fd0f3..bd7b915 100644 --- a/test/view/sort_distance.js +++ b/test/view/sort_distance.js @@ -3,6 +3,7 @@ var VariableStore = require('../../lib/VariableStore'); function getBaseVariableStore(toExclude) { var vs = new VariableStore(); + vs.var('sort:field', 'distance'); vs.var('focus:point:lat', 'lat value'); vs.var('focus:point:lon', 'lon value'); vs.var('sort:distance:order', 'order value'); diff --git a/test/view/sort_popularity.js b/test/view/sort_popularity.js new file mode 100644 index 0000000..8134e41 --- /dev/null +++ b/test/view/sort_popularity.js @@ -0,0 +1,68 @@ +var sort_popularity = require('../../view/sort_popularity'); +var VariableStore = require('../../lib/VariableStore'); + +function getBaseVariableStore(toExclude) { + var vs = new VariableStore(); + vs.var('sort:field', 'popularity'); + + if (toExclude) { + vs.unset(toExclude); + } + + return vs; + +} + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('interface: contructor', function(t) { + t.equal(typeof sort_popularity, 'function', 'valid function'); + t.equal(sort_popularity.length, 1, 'takes 1 arg'); + t.end(); + }); + +}; + +module.exports.tests.missing_variable_conditions = function(test, common) { + var variables = Object.keys(getBaseVariableStore().export()); + + variables.forEach(function(missing_variable) { + test('missing required variable ' + missing_variable + ' should return null', function(t) { + var vs = getBaseVariableStore(missing_variable); + + t.equal(sort_popularity(vs), null, 'should have returned null for unset ' + missing_variable); + t.end(); + + }); + }); + +}; + +module.exports.tests.no_exceptions_conditions = function(test, common) { + test('all properties set should return valid object', function(t) { + var vs = getBaseVariableStore(); + + var actual = sort_popularity(vs); + + var expected = { + popularity: { + order: 'desc' + } + }; + + t.deepEquals(actual, expected, 'should have returned object'); + t.end(); + + }); + +}; + +module.exports.all = function (tape, common) { + function test(name, testFunction) { + return tape('sort_popularity ' + name, testFunction); + } + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/view/sort_distance.js b/view/sort_distance.js index 1f3d049..e1c6f2a 100644 --- a/view/sort_distance.js +++ b/view/sort_distance.js @@ -2,7 +2,8 @@ module.exports = function( vs ){ // validate required params - if( !vs.isset('focus:point:lat') || + if( vs.var('sort:field').get() !== 'distance' || + !vs.isset('focus:point:lat') || !vs.isset('focus:point:lon') || !vs.isset('sort:distance:order') || !vs.isset('sort:distance:distance_type') || diff --git a/view/sort_popularity.js b/view/sort_popularity.js new file mode 100644 index 0000000..b39a8e9 --- /dev/null +++ b/view/sort_popularity.js @@ -0,0 +1,14 @@ +module.exports = function( vs ) { + if ( !vs.isset('sort:field') || + vs.var('sort:field').get() !== 'popularity' ) { + return null; + } + // base view + var view = { + popularity: { + order: 'desc', + } + }; + + return view; +};