Skip to content

Commit

Permalink
Add testing infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
louh committed Aug 23, 2015
1 parent f5a72d3 commit 5d9f2be
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .eslintrc
@@ -0,0 +1,7 @@
{
"extends": ["semistandard"],
"globals": {
"define": false,
"XMLHttpRequest": false
}
}
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
node_modules
node_modules
.DS_Store
coverage/
15 changes: 7 additions & 8 deletions Jakefile.js
Expand Up @@ -9,13 +9,11 @@ To use, install Node, then run the following commands in the project root:
To check the code & run tests: run "jake test".
*/
/* global jake, desc, task, build, complete */

//var version = require('./src/Leaflet.js').version;
/* global jake, desc, task, complete */

function hint (msg, path) {
return function () {
var command = 'node node_modules/.bin/semistandard ' + path + ' | node node_modules/.bin/snazzy';
var command = 'node node_modules/.bin/eslint ' + path;
var done = function () {
console.log('\tCheck passed.\n');
complete();
Expand Down Expand Up @@ -62,16 +60,17 @@ function test (complete, fail) {
testConfig.reporters = ['coverage'];
}

console.log('Running tests...');

karma.server.start(testConfig, function (exitCode) {
var server = new karma.Server(testConfig, function (exitCode) {
if (!exitCode) {
console.log('\tTests ran successfully.\n');
complete();
} else {
process.exit(exitCode);
}
});

console.log('Running tests...');
server.start();
}

desc('Check plugin JavaScript for errors with Semistandard');
Expand All @@ -82,7 +81,7 @@ task('lintspec', { async: true }, hint('Checking for specs & support JS errors..

desc('Run PhantomJS tests');
task('test', ['lint', 'lintspec'], { async: true }, function () {
//test(complete);
test(complete);
});

task('default', ['test']);
Expand Down
4 changes: 4 additions & 0 deletions dist/pelias-leaflet-geocoder.js
Expand Up @@ -50,9 +50,13 @@

initialize: function (apiKey, options) {
this.apiKey = apiKey;
/*
// TODO: apiKey may not be required if the
// a custom API endpoint did not require it
if (!apiKey || typeof apiKey !== 'string') {
console.error('Please provide a Pelias API key.');
}
*/

L.Util.setOptions(this, options);
this.marker;
Expand Down
2 changes: 1 addition & 1 deletion examples/index.html
Expand Up @@ -23,7 +23,7 @@
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Add Pelias geocoding plugin
L.control.geocoder('pelias-api-key').addTo(map);
var geocoder = L.control.geocoder('pelias-api-key').addTo(map);
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/leaflet-v1.html
Expand Up @@ -27,7 +27,7 @@
var hash = new L.Hash(map);

// Add Pelias geocoding plugin
L.control.geocoder('pelias-api-key').addTo(map);
var geocoder = L.control.geocoder('pelias-api-key').addTo(map);
</script>
</body>
</html>
6 changes: 3 additions & 3 deletions examples/position-topright.html
Expand Up @@ -23,17 +23,17 @@
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Add Pelias geocoding plugin to top right
L.control.geocoder('pelias-api-key', {
var geocoder1 = L.control.geocoder('pelias-api-key', {
position: 'topright'
}).addTo(map);

// Add Pelias geocoding plugin to bottom left
L.control.geocoder('pelias-api-key', {
var geocoder2 = L.control.geocoder('pelias-api-key', {
position: 'bottomleft'
}).addTo(map);

// Add Pelias geocoding plugin to bottom right
L.control.geocoder('pelias-api-key', {
var geocoder3 = L.control.geocoder('pelias-api-key', {
position: 'bottomright'
}).addTo(map);
</script>
Expand Down
2 changes: 1 addition & 1 deletion examples/search-icon-active-state.html
Expand Up @@ -39,7 +39,7 @@
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

// Add Pelias geocoding plugin
L.control.geocoder('pelias-api-key').addTo(map);
var geocoder = L.control.geocoder('pelias-api-key').addTo(map);
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/tangram.html
Expand Up @@ -29,7 +29,7 @@
var hash = new L.Hash(map);

// Add Pelias geocoding plugin
L.control.geocoder('pelias-api-key').addTo(map);
var geocoder = L.control.geocoder('pelias-api-key').addTo(map);

// Add Tangram scene layer
// (Note: Tangram can also be initialized before the Pelias geocoder, if you choose.)
Expand Down
2 changes: 1 addition & 1 deletion index.html
Expand Up @@ -46,7 +46,7 @@
var hash = new L.Hash(map);

// Add geocoder
L.control.geocoder('pelias-api-key', {
var geocoder = L.control.geocoder('pelias-api-key', {
pointIcon: 'dist/images/point_icon.png',
polygonIcon: 'dist/images/polygon_icon.png',
fullWidth: 650,
Expand Down
23 changes: 20 additions & 3 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"example": "examples"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jake test"
},
"repository": {
"type": "git",
Expand All @@ -31,14 +31,31 @@
},
"homepage": "https://github.com/pelias/leaflet-geocoder",
"devDependencies": {
"eslint": "^1.2.1",
"eslint-config-semistandard": "^5.0.0",
"eslint-config-standard": "^4.1.0",
"eslint-plugin-standard": "^1.2.0",
"expect.js": "^0.3.1",
"happen": "^0.1.3",
"jake": "^8.0.12",
"semistandard": "^7.0.2",
"snazzy": "^2.0.1"
"karma": "~0.13.9",
"karma-chrome-launcher": "^0.2.0",
"karma-coverage": "~0.5.0",
"karma-firefox-launcher": "~0.1.6",
"karma-mocha": "~0.2.0",
"karma-phantomjs-launcher": "^0.2.0",
"karma-safari-launcher": "~0.1.1",
"mocha": "~2.2.5",
"phantomjs": "^1.9.18",
"sinon": "^1.16.1"
},
"semistandard": {
"globals": [
"define",
"XMLHttpRequest"
]
},
"dependencies": {
"leaflet": "^1.0.0-beta.1"
}
}
15 changes: 15 additions & 0 deletions spec/.eslintrc
@@ -0,0 +1,15 @@
{
"extends": ["semistandard"],
"globals": {
"L": true,
"module": false,
"define": false,
"expect": false,
"it": false,
"describe": false,
"sinon": false,
"happen": false,
"beforeEach": false,
"afterEach": false
}
}
Empty file removed spec/.gitkeep
Empty file.
34 changes: 34 additions & 0 deletions spec/index.html
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Pelias Leaflet Geocoder Spec Runner</title>
<link rel='stylesheet' type='text/css' href='../node_modules/mocha/mocha.css'>
<link rel='stylesheet' type='text/css' href='../node_modules/leaflet/dist/leaflet.css'>
</head>
<body>
<div id='mocha'></div>
<script src='../node_modules/expect.js/index.js'></script>
<script type='text/javascript' src='../node_modules/mocha/mocha.js'></script>
<script type='text/javascript' src='../node_modules/happen/happen.js'></script>
<script type='text/javascript' src='../node_modules/sinon/pkg/sinon.js'></script>
<script type='text/javascript' src='../node_modules/leaflet/dist/leaflet.js'></script>

<!-- source files -->
<script type='text/javascript' src='../dist/pelias-leaflet-geocoder.js'></script>

<script>
mocha.setup({
ui: 'bdd',
ignoreLeaks: true
});
</script>

<!-- spec files -->
<script type='text/javascript' src='suites/ControlSpec.js'></script>

<script>
(window.mochaPhantomJS || window.mocha).run();
</script>
</body>
</html>
77 changes: 77 additions & 0 deletions spec/karma.conf.js
@@ -0,0 +1,77 @@
// Karma configuration
module.exports = function (config) {

var files = [
// Test ependencies
'node_modules/sinon/pkg/sinon.js',
'node_modules/expect.js/index.js',
'node_modules/happen/happen.js',
// Leaflet library
'node_modules/leaflet/dist/leaflet.js',
// Geocoder plugin
'dist/pelias-leaflet-geocoder.js',
// Tests
'spec/suites/**/*.js',
// Misc
{ pattern: 'dist/images/*.png', included: false }
];

config.set({
// base path, that will be used to resolve files and exclude
basePath: '../',

plugins: [
'karma-mocha',
'karma-coverage',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-safari-launcher',
'karma-firefox-launcher',
],

// frameworks to use
frameworks: ['mocha'],

// list of files / patterns to load in the browser
files: files,
exclude: [],

// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['dots'],

// web server port
port: 9876,

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

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

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

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 5000,

// Workaround for PhantomJS random DISCONNECTED error
browserDisconnectTimeout: 10000, // default 2000
browserDisconnectTolerance: 1, // default 0

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
37 changes: 37 additions & 0 deletions spec/suites/ControlSpec.js
@@ -0,0 +1,37 @@
describe('Control', function () {
var map;

beforeEach(function () {
map = L.map(document.createElement('div'));
});

describe('#addTo', function () {
it('adds the geocoder control to the map', function () {
var geocoder = new L.Control.Geocoder();
geocoder.addTo(map);
expect(map.getContainer().querySelector('.leaflet-pelias-control')).to.equal(geocoder.getContainer());
});
});

// Note: this is Leaflet v1
// Previous versions of Leaflet: geocoder.removeFrom(map);
describe('#remove', function () {
it('removes the geocoder control from the map', function () {
var geocoder = new L.Control.Geocoder();
geocoder.addTo(map).remove();
expect(map.getContainer().querySelector('.leaflet-pelias-control')).to.equal(null);
});

it('calls onRemove if defined', function () {
var geocoder = new L.Control.Geocoder();
geocoder.onRemove = sinon.spy();
geocoder.addTo(map).remove();
expect(geocoder.onRemove.called).to.be(true);
});

it('is a no-op if the geocoder control has not been added', function () {
var geocoder = new L.Control.Geocoder();
expect(geocoder.remove()).to.equal(geocoder);
});
});
});
Empty file removed src/.gitkeep
Empty file.

0 comments on commit 5d9f2be

Please sign in to comment.