Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic tests #160

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ results
npm-debug.log
node_modules

coverage
dist
build
build
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ before_install:

install:
- yarn install --no-lockfile
- yarn add openlayers@^3

script:
- yarn lint
- yarn test
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
// Automatically clear mock calls and instances between every test
clearMocks: true,

// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
};
7 changes: 7 additions & 0 deletions js/ol3-sidebar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// detect jsdom environment
// borrowed from: https://github.com/jsdom/jsdom/issues/1537
var ol;
if (navigator.userAgent.includes("Node.js") || navigator.userAgent.includes("jsdom")) {
ol = require('openlayers');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is needed only for tests I would prefer it if we could do this in the test file instead 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I could have left the main code alone, I would have. I certainly tried very hard! Unfortunately, there seems to be no way to have the sidebar code "know" about OpenLayers (i.e. the ol namespace) at require time other than explicitly require-ing it inside the module itself. It didn't seem possible to load a <script> within the HTML loaded as part of jsdom in the jest setup, so it didn't seem to be possible to put ol into the global namespace for sidebar to find it. Do you have any ideas as to how one might be able to get around this?


ol.control.Sidebar = function (settings) {

var defaults = {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"url": "https://github.com/turbo87/sidebar-v2.git"
},
"scripts": {
"test": "gulp lint"
"lint": "gulp lint",
"test": "jest"
},
"devDependencies": {
"gulp": "~4.0.0",
Expand All @@ -35,6 +36,7 @@
"gulp-sass": "^3.1.0",
"gulp-uglify": "~3.0.0",
"gulp-zip": "~4.0.0",
"jest": "^24.9.0",
"jshint": "^2.9.5"
}
}
124 changes: 124 additions & 0 deletions tests/sidebar-ol.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// work around jsdom canvas issue in jest
HTMLCanvasElement.prototype.getContext = jest.fn()

var ol = require('openlayers');
var Sidebar = require('../js/ol3-sidebar');

describe('Simple sidebar object instantiation', function() {
beforeEach(function() {
document.body.innerHTML =
'<div id="sidebar" class="sidebar collapsed">' +
' <div class="sidebar-tabs">' +
' </div>' +
' <div class="sidebar-content">' +
' </div>' +
'</div>' +
'<div id="map" class="sidebar-map"></div>';
});

test('Sidebar object minimal instantiation', function() {
var sidebar = new ol.control.Sidebar({ element: 'sidebar' });

expect(sidebar).toBeTruthy();
});

test('Sidebar constructor sets position option', function() {
var sidebar = new ol.control.Sidebar({
element: 'sidebar',
position: 'right'
});

expect(sidebar._options.position).toBe('right');
});
});

describe('Check sidebar configurations', function() {
test('Minimal sidebar structure contains expected elements', function() {
document.body.innerHTML =
'<div id="sidebar" class="sidebar collapsed">' +
' <div class="sidebar-tabs">' +
' <ul>' +
' <li>single item</li>' +
' </ul>' +
' </div>' +
' <div class="sidebar-content">' +
' <div class="sidebar-pane">' +
' <span class="sidebar-close"></span>' +
' </div>' +
' </div>' +
'</div>' +
'<div id="map" class="sidebar-map"></div>';
var sidebar = new ol.control.Sidebar({ element: 'sidebar' });

expect(sidebar._tabitems.length).toBe(1);
expect(sidebar._panes.length).toBe(1);
expect(sidebar._closeButtons.length).toBe(1);
});
});

describe('Check sidebar API', function() {
var map, sidebar;

beforeEach(function() {
document.body.innerHTML =
'<div id="sidebar" class="sidebar collapsed">' +
' <div class="sidebar-tabs">' +
' <ul>' +
' <li><a href="#home">Home</a></li>' +
' </ul>' +
' </div>' +
' <div class="sidebar-content">' +
' <div class="sidebar-pane">' +
' <span class="sidebar-close"></span>' +
' </div>' +
' </div>' +
'</div>' +
'<div id="map" class="sidebar-map"></div>';
map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.transform([7, 51.2], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
sidebar = new ol.control.Sidebar({ element: 'sidebar' });
});

test('Sidebar control gets added to map', function() {
var numControlsBefore = map.getControls().getArray().length;
map.addControl(sidebar);
var numControlsAfter = map.getControls().getArray().length;

expect(numControlsAfter - numControlsBefore).toBe(1);
});

test('Sidebar is not collapsed when opened', function() {
map.addControl(sidebar);
sidebar.open();
var sidebarDivClass = document.getElementById('sidebar').getAttribute('class');

expect(sidebarDivClass).not.toContain('collapsed');
});

test('Sidebar is collapsed after being closed', function() {
map.addControl(sidebar);
var sidebarDivClass = document.getElementById('sidebar').getAttribute('class');

expect(sidebarDivClass).toContain('collapsed');

sidebar.open();

sidebarDivClass = document.getElementById('sidebar').getAttribute('class');
expect(sidebarDivClass).not.toContain('collapsed');

sidebar.close();

sidebarDivClass = document.getElementById('sidebar').getAttribute('class');
expect(sidebarDivClass).toContain('collapsed');
});
});