1<!doctype html> 2<!-- 3@license 4Copyright (c) 2016 The Polymer Project Authors. All rights reserved. 5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 8Code distributed by Google as part of the polymer project is also 9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 10--> 11<html> 12<head> 13 <title>app-location</title> 14 15 <script src="../../webcomponentsjs/webcomponents-lite.js"></script> 16 <script src="../../web-component-tester/browser.js"></script> 17 18 <link rel="import" href="../../polymer/polymer.html"> 19 <link rel="import" href="../../iron-test-helpers/mock-interactions.html"> 20 <link rel="import" href="../app-location.html"> 21</head> 22<body> 23 <test-fixture id="BasicLocation"> 24 <template> 25 <app-location></app-location> 26 </template> 27 </test-fixture> 28 29 <test-fixture id="LocationViaHash"> 30 <template> 31 <app-location use-hash-as-path></app-location> 32 </template> 33 </test-fixture> 34 35 <test-fixture id="ClickableLink"> 36 <template> 37 <a></a> 38 </template> 39 </test-fixture> 40 41 <script> 42 'use strict'; 43 44 function setLocation(url) { 45 window.history.pushState({}, '', url); 46 Polymer.Base.fire('location-changed', {}, { node: window }); 47 } 48 49 function assign(a, b) { 50 if (Object.assign) { 51 return Object.assign.apply(Object, arguments); 52 } 53 54 for (var property in b) { 55 a[property] = b[property]; 56 } 57 58 return a; 59 } 60 61 suite('<app-location>', function () { 62 var initialUrl; 63 64 setup(function() { 65 initialUrl = window.location.href; 66 }); 67 68 teardown(function() { 69 window.history.replaceState({}, '', initialUrl); 70 }); 71 72 suite('in the default configuration', function() { 73 var appLocation; 74 75 setup(function() { 76 appLocation = fixture('BasicLocation'); 77 }); 78 79 test('it automatically exposes the current route', function() { 80 expect(appLocation.route).to.be.ok; 81 expect(appLocation.route.path).to.be.equal(window.location.pathname); 82 }); 83 84 suite('manipulating the route', function() { 85 var originalPath; 86 var originalQueryParams; 87 88 setup(function() { 89 originalPath = appLocation.route.path; 90 originalQueryParams = assign({}, appLocation.route.__queryParams); 91 }); 92 93 teardown(function() { 94 appLocation.set('route.prefix', ''); 95 appLocation.set('route.path', originalPath); 96 appLocation.set('route.__queryParams', originalQueryParams); 97 }); 98 99 test('it reflects path to location.pathname', function() { 100 appLocation.set('route.path', '/foo/bar'); 101 expect(window.location.pathname).to.be.equal('/foo/bar'); 102 }); 103 104 test('it reflects queryParams values to location.search', function() { 105 appLocation.set('route.__queryParams.foo', 1); 106 expect(window.location.search).to.match(/foo=1/); 107 }); 108 109 test('it reflects completely replaced queryParams', function() { 110 appLocation.set('route.__queryParams', { bar: 1 }); 111 expect(window.location.search).to.be.equal('?bar=1'); 112 }); 113 114 test('it reflects the prefix to location.pathname', function() { 115 appLocation.set('route.prefix', '/fiz'); 116 expect(window.location.pathname).to.be.equal('/fiz' + originalPath); 117 }); 118 }); 119 120 /** 121 * NOTE: For a more thorough spec describing this behavior, please refer 122 * to the `iron-location` component. 123 */ 124 suite('manipulating the history state', function() { 125 var originalLocation; 126 127 setup(function() { 128 originalLocation = window.location.toString(); 129 }); 130 131 teardown(function() { 132 setLocation(originalLocation); 133 }); 134 135 test('it reflects location.pathname to route.path', function() { 136 setLocation('/fiz/buz'); 137 expect(appLocation.route.path).to.be.equal('/fiz/buz'); 138 }); 139 140 test('it reflects location.search to route.__queryParams', function() { 141 setLocation('?fiz=buz'); 142 expect(appLocation.route.__queryParams).to.be.eql({ 143 fiz: 'buz' 144 }); 145 }); 146 }); 147 }); 148 149 suite('using the hash as the route path', function() { 150 var appLocation; 151 152 setup(function() { 153 appLocation = fixture('LocationViaHash'); 154 }); 155 156 test('it reflects location.hash to route.path', function() { 157 setLocation('#/fiz/buz'); 158 expect(appLocation.route.path).to.be.equal('/fiz/buz'); 159 }); 160 161 test('it reflects route.path to location.hash', function() { 162 appLocation.set('route.path', '/foo/bar'); 163 expect(window.location.hash).to.be.equal('#/foo/bar'); 164 }); 165 }); 166 }); 167 </script> 168</body> 169