From aa4589816afd0e9c7914880842beb2fcc1963f4a Mon Sep 17 00:00:00 2001 From: Norbert Renner Date: Tue, 7 Jun 2022 19:19:16 +0200 Subject: [PATCH 1/3] Add profile selection tests for RoutingOptions --- tests/control/RoutingOptions.test.js | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/control/RoutingOptions.test.js diff --git a/tests/control/RoutingOptions.test.js b/tests/control/RoutingOptions.test.js new file mode 100644 index 0000000..d1b8d01 --- /dev/null +++ b/tests/control/RoutingOptions.test.js @@ -0,0 +1,63 @@ +BR = {}; +$ = require('jquery'); +i18next = require('i18next'); +require('bootstrap'); +require('bootstrap-select'); +require('leaflet'); + +require('../../config.js'); +require('../../js/Util.js'); +require('../../js/router/BRouter.js'); +require('../../js/control/RoutingOptions.js'); + +const fs = require('fs'); +const indexHtmlString = fs.readFileSync('index.html', 'utf8'); +const indexHtml = new DOMParser().parseFromString(indexHtmlString, 'text/html'); + +let defaultProfile; + +beforeEach(() => { + document.body = indexHtml.body.cloneNode(true); + location.hash = ''; + // as used in BRouter.getUrlParams + defaultProfile = BR.conf.profiles[0]; +}); + +describe('Profile selection', () => { + test('sets default profile (trekking) when no hash and no localStorage entry', () => { + let routingOptions = new BR.RoutingOptions(); + routingOptions.setOptions({}); + const profile = routingOptions.getOptions().profile; + expect(profile).toEqual(defaultProfile); + }); + + // defaults are not set in hash, see BRouter.getUrlParams + test('sets default profile (trekking) when hash without `profile` param regardless of localStorage', () => { + location.hash = '#map=5/50.986/9.822/standard'; + localStorage.routingprofile = 'shortest'; + + let routingOptions = new BR.RoutingOptions(); + routingOptions.setOptions({}); + const profile = routingOptions.getOptions().profile; + expect(profile).toEqual(defaultProfile); + }); + + test('sets profile from hash', () => { + location.hash = '#map=5/50.986/9.822/standard&profile=fastbike'; + localStorage.routingprofile = 'shortest'; + + let routingOptions = new BR.RoutingOptions(); + routingOptions.setOptions({ profile: 'fastbike' }); + const profile = routingOptions.getOptions().profile; + expect(profile).toEqual('fastbike'); + }); + + test('sets profile from localStorage when no hash', () => { + localStorage.routingprofile = 'shortest'; + + let routingOptions = new BR.RoutingOptions(); + routingOptions.setOptions({}); + const profile = routingOptions.getOptions().profile; + expect(profile).toEqual('shortest'); + }); +}); From a57a56095021633fe6a99c516bc555a7218b780e Mon Sep 17 00:00:00 2001 From: Norbert Renner Date: Tue, 7 Jun 2022 19:26:41 +0200 Subject: [PATCH 2/3] Fix default profile not selected with hash --- js/control/RoutingOptions.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/control/RoutingOptions.js b/js/control/RoutingOptions.js index 4d25881..c916f79 100644 --- a/js/control/RoutingOptions.js +++ b/js/control/RoutingOptions.js @@ -18,7 +18,8 @@ BR.RoutingOptions = L.Evented.extend({ var option = document.createElement('option'); option.value = profiles[i]; option.text = i18next.t('navbar.profile.' + profiles[i]); - if (remembered_profile !== null && remembered_profile === profiles[i]) { + // set remembered profile, only when no URL hash (assumes fullHash plugin not initialised yet) + if (!location.hash && remembered_profile !== null && remembered_profile === profiles[i]) { option.selected = true; remembered_profile_was_selected = true; } From aef3fdc5a731514a8ad66d7a26c701e7f9df3bfb Mon Sep 17 00:00:00 2001 From: Norbert Renner Date: Tue, 7 Jun 2022 20:21:54 +0200 Subject: [PATCH 3/3] Fix RoutingOptions test for CI --- tests/control/RoutingOptions.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/control/RoutingOptions.test.js b/tests/control/RoutingOptions.test.js index d1b8d01..67af6e5 100644 --- a/tests/control/RoutingOptions.test.js +++ b/tests/control/RoutingOptions.test.js @@ -5,7 +5,7 @@ require('bootstrap'); require('bootstrap-select'); require('leaflet'); -require('../../config.js'); +require('../../config.template.js'); require('../../js/Util.js'); require('../../js/router/BRouter.js'); require('../../js/control/RoutingOptions.js');