Skip to content

Commit

Permalink
Add tests for URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Dec 15, 2023
1 parent 6bcde85 commit 146f040
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 43 deletions.
28 changes: 7 additions & 21 deletions umap/static/umap/js/modules/urls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,28 @@ export default class URLs {
this.urls = serverUrls;
}

/**
* Returns the URL to the requested resource.
*
* @param {String} urlName The URL name, as specified by the server.
* @param {String|Object} params If a string, the map id, otherwise the parameters passed to the URL template.
* @returns {String} The URL that has been built.
*/
get(urlName, params) {
if (typeof params != 'object')
params = { map_id: params };

if (typeof this[urlName] === "function")
return this[urlName](params);

if (this.urls.hasOwnProperty(urlName)) {
console.log("urls", this.urls, urlName, params);
return L.Util.template(this.urls[urlName], params);
} else {
throw `Unable to find a URL for route ${urlName}`;
}
}

// Update if map_id is passed, create otherwise.
map_save({ map_id }) {
if (map_id)
return this.get("map_update", map_id);
map_save(params) {
if (params.map_id)
return this.get("map_update", params);
return this.get("map_create");
}

// Update the layer if pk is passed, create otherwise.
datalayer_save({ map_id, pk }) {
if (pk)
return this.get(
"datalayer_update",
{ map_id: map_id, pk: pk }
);
return this.get("datalayer_create", map_id);
datalayer_save(params) {
if (params.pk)
return this.get("datalayer_update", params);
return this.get("datalayer_create", params);
}
}
40 changes: 18 additions & 22 deletions umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ L.U.Map.include({
const zoomControl =
typeof geojson.properties.zoomControl !== 'undefined'
? geojson.properties.zoomControl
: true;
: true
geojson.properties.zoomControl = false
const fullscreenControl =
typeof geojson.properties.fullscreenControl !== 'undefined'
? geojson.properties.fullscreenControl
: true;
: true
geojson.properties.fullscreenControl = false
L.Util.setBooleanFromQueryString(geojson.properties, 'scrollWheelZoom')

L.Map.prototype.initialize.call(this, el, geojson.properties)

// After calling parent initialize, as we are doing initCenter our-selves
if (geojson.geometry) this.options.center = this.latLng(geojson.geometry)
this.urls = new window.URLs(this.options.urls);
this.urls = new window.URLs(this.options.urls)

this.ui = new L.U.UI(this._container)
this.xhr = new L.U.Xhr(this.ui)
Expand Down Expand Up @@ -274,8 +274,8 @@ L.U.Map.include({
history.pushState({}, '', url)
}
if (L.Util.queryString('download')) {
let download_url = this.urls.get('map_download', this.options.umap_id);
window.location = download_url;
let download_url = this.urls.get('map_download', { map_id: this.options.umap_id })
window.location = download_url
}
})

Expand Down Expand Up @@ -1094,7 +1094,7 @@ L.U.Map.include({
formData.append('name', this.options.name)
formData.append('center', JSON.stringify(this.geometry()))
formData.append('settings', JSON.stringify(geojson))
this.post(this.urls.get('map_save', this.options.umap_id), {
this.post(this.urls.get('map_save', { map_id: this.options.umap_id }), {
data: formData,
context: this,
callback: function (data) {
Expand Down Expand Up @@ -1176,7 +1176,7 @@ L.U.Map.include({
const formData = new FormData()
formData.append('email', email)

const url = this.urls.get('map_send_edit_link', this.options.umap_id)
const url = this.urls.get('map_send_edit_link', { map_id: this.options.umap_id })
this.post(url, {
data: formData,
})
Expand All @@ -1188,7 +1188,7 @@ L.U.Map.include({
content: L._('Please save the map first'),
level: 'error',
})
let url = this.urls.get('map_star', this.options.umap_id);
let url = this.urls.get('map_star', { map_id: this.options.umap_id })
this.post(url, {
context: this,
callback: function (data) {
Expand Down Expand Up @@ -1796,7 +1796,7 @@ L.U.Map.include({

del: function () {
if (confirm(L._('Are you sure you want to delete this map?'))) {
const url = this.urls.get('map_delete', this.options.umap_id);
const url = this.urls.get('map_delete', { map_id: this.options.umap_id })
this.post(url)
}
},
Expand All @@ -1805,8 +1805,8 @@ L.U.Map.include({
if (
confirm(L._('Are you sure you want to clone this map and all its datalayers?'))
) {
const url = this.urls.get('map_clone', this.options.umap_id);
this.post(url);
const url = this.urls.get('map_clone', { map_id: this.options.umap_id })
this.post(url)
}
},

Expand Down Expand Up @@ -1940,17 +1940,13 @@ L.U.Map.include({
},

openExternalRouting: function (e) {
let url = this.urls.get(
'routing',
{
lat: e.latlng.lat,
lng: e.latlng.lng,
locale: L.locale,
zoom: this.getZoom(),
}
);
if (url)
window.open(url)
let url = this.urls.get('routing', {
lat: e.latlng.lat,
lng: e.latlng.lng,
locale: L.locale,
zoom: this.getZoom(),
})
if (url) window.open(url)
},

getMap: function () {
Expand Down
48 changes: 48 additions & 0 deletions umap/static/umap/test/URLs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import URLs from "../js/modules/urls.mjs";

describe('URLs', () => {
// Mock server URLs that will be used for testing
const mockServerUrls = {
'map_create': '/maps/create',
'map_update': '/maps/{map_id}/update',
'datalayer_create': '/maps/{map_id}/datalayers/create',
'datalayer_update': '/maps/{map_id}/datalayers/{pk}/update'
};

let urls = new URLs(mockServerUrls);

describe('get()', () => {
it('should throw an error if the urlName does not exist', () => {
expect(() => urls.get('non_existent')).to.throw();
});

it('should return the correct templated URL for known urlNames', () => {
expect(urls.get('map_create')).to.be.equal('/maps/create');
expect(urls.get('map_update', { map_id: '123' })).to.be.equal('/maps/123/update');
});

it('should return the correct templated URL when provided with parameters', () => {
expect(urls.get('datalayer_update', { map_id: '123', pk: '456' })).to.be.equal('/maps/123/datalayers/456/update');
});
});

describe('map_save()', () => {
it('should return the create URL if no map_id is provided', () => {
expect(urls.map_save({})).to.be.equal('/maps/create');
});

it('should return the update URL if a map_id is provided', () => {
expect(urls.map_save({ map_id: '123' })).to.be.equal('/maps/123/update');
});
});

describe('datalayer_save()', () => {
it('should return the create URL if no pk is provided', () => {
expect(urls.datalayer_save({ map_id: '123' })).to.be.equal('/maps/123/datalayers/create');
});

it('should return the update URL if a pk is provided', () => {
expect(urls.datalayer_save({ map_id: '123', pk: '456' })).to.be.equal('/maps/123/datalayers/456/update');
});
});
});
1 change: 1 addition & 0 deletions umap/static/umap/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<script src="./Controls.js"></script>
<script src="./Permissions.js"></script>
<script src="./Choropleth.js"></script>
<script type="module" src="./URLs.mjs"></script>
<style type="text/css">
#mocha {
position: absolute;
Expand Down

0 comments on commit 146f040

Please sign in to comment.