-
-
Notifications
You must be signed in to change notification settings - Fork 335
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
paultcochrane
wants to merge
5
commits into
Turbo87:master
Choose a base branch
from
paultcochrane:pr/add-basic-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add basic tests #160
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d789574
Rename 'test' target to 'lint'
paultcochrane f21bdf2
Add basic tests of Sidebar with OpenLayers
paultcochrane 20ee27c
Test sidebar configurations
paultcochrane d858953
Add basic tests of Sidebar API
paultcochrane 787b636
Run test suite on Travis-CI
paultcochrane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,6 @@ results | |
npm-debug.log | ||
node_modules | ||
|
||
coverage | ||
dist | ||
build | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ before_install: | |
|
||
install: | ||
- yarn install --no-lockfile | ||
- yarn add openlayers@^3 | ||
|
||
script: | ||
- yarn lint | ||
- yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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) atrequire
time other than explicitlyrequire
-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 putol
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?