From 2845af0daf7eeedb669d929b8b7fe07c72c75958 Mon Sep 17 00:00:00 2001 From: Alan Chandler Date: Wed, 5 Oct 2016 13:39:38 +0100 Subject: [PATCH] Fix various problems found when put into use Incorrectly detecting query params with no content Incorrectly handling params update where all params are zero length --- akc-location.html | 2 +- akc-route.html | 8 ++++---- bower.json | 2 +- test/akc-location_test.html | 9 ++++++--- test/akc-route_test.html | 6 ++++++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/akc-location.html b/akc-location.html index 7f70693..767aa7c 100644 --- a/akc-location.html +++ b/akc-location.html @@ -184,7 +184,7 @@ } else { newUrl = window.encodeURI(path).replace(/\#/g, '%23').replace(/\?/g, '%3F'); } - if (query) { + if (query && Object.keys(query).length > 0) { this.__query = query; newUrl += '?' + this.__queryString.replace(/\#/g, '%23'); diff --git a/akc-route.html b/akc-route.html index 422e25f..6281834 100644 --- a/akc-route.html +++ b/akc-route.html @@ -50,7 +50,7 @@ */ inRoute: { type: Object, - value: function() {return {};}, + value: function() {return {path: '', active: false, params: {}, query: {}};}, notify:true }, /** @@ -92,7 +92,7 @@ */ outRoute: { type: Object, - value: function() {return {};}, + value: function() {return {path: '', active: false, params: {}, query: {}};}, notify: true } }, @@ -208,11 +208,11 @@ } path += '/' + params[key].toString(); } else { - path += '/' + matchePieces[i]; + path += '/' + matchedPieces[i]; } } } - if (this.outRoute.path.length > 1 && this.outRoute.active) { + if (path.length > 1 && this.outRoute.path.length > 1 && this.outRoute.active) { path += this.outRoute.path; //only do this if the outpath was really there } if (path !== this.inRoute.path) { diff --git a/bower.json b/bower.json index becd3d4..94097e6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "akc-route", - "version": "0.0.4", + "version": "0.0.5", "description": "A distributed router inspired by PolymerElements/app-route", "authors": [ "Alan Chandler" diff --git a/test/akc-location_test.html b/test/akc-location_test.html index d712f43..70fc842 100644 --- a/test/akc-location_test.html +++ b/test/akc-location_test.html @@ -79,9 +79,7 @@ expect(el.route.path).to.equal(window.location.pathname); }); it('expect search string changes to reflect in route', function() { -console.log('just before setting query string to ?date=20160101&staff=2'); setLocation(window.location.pathname + '?date=20160101&staff=2'); -console.log('query string set in test'); expect(el.route.query).to.deep.equal({date: '20160101', staff: '2'}); }); it('reflect changes to route path to url', function() { @@ -91,7 +89,12 @@ it('reflect query string changes to route.query', function() { el.set('route.query', {firstname: 'Joe', lastname: 'Bloggs'}); expect(window.location.search).to.equal('?firstname=Joe&lastname=Bloggs'); - }) + }); + it('seting query to null object in route should clear qhery string', function() { + el.set('route.query', {firstname: 'Joe', lastname: 'Bloggs'}); + el.set('route.query', {}); + expect(window.location.search).to.equal(''); + }); }); }); diff --git a/test/akc-route_test.html b/test/akc-route_test.html index 12b25e6..e3df9ef 100644 --- a/test/akc-route_test.html +++ b/test/akc-route_test.html @@ -133,6 +133,12 @@ el.set('subSubRoute.path', '/20161231'); expect(el.route.path).to.equal('/reports/bydate/20160101'); }); + it('zero length parameter change limits actuve', function() { + el.match = '/:page'; + el.route = {path: '/reports/bydate/20160101', active: true, params: {}, query: {}}; + el.set('subRoute.params', {page: ''}); + expect(el.route.path).to.equal('/'); + }); });