4
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8
Code distributed by Google as part of the polymer project is also
9
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
13
<title>iron-location</title>
15
<script src="../../webcomponentsjs/webcomponents.js"></script>
16
<script src="../../web-component-tester/browser.js"></script>
18
<link rel="import" href="../../polymer/polymer.html">
19
<link rel="import" href="../../promise-polyfill/promise-polyfill.html">
20
<link rel="import" href="../iron-location.html">
23
<test-fixture id='Solo'>
25
<iron-location></iron-location>
28
<test-fixture id='Together'>
31
<iron-location id='one'></iron-location>
32
<iron-location id='two'></iron-location>
41
function timePasses(ms) {
42
return new Promise(function(resolve) {
43
window.setTimeout(function() {
49
suite('<iron-location>', function () {
52
initialUrl = window.location.href;
55
window.history.replaceState({}, '', initialUrl);
58
suite('when used solo', function() {
61
urlElem = fixture('Solo');
63
test('basic functionality with #hash urls', function() {
64
// Initialized to the window location's hash.
65
expect(window.location.hash).to.be.equal(urlElem.hash);
67
// Changing the urlElem's hash changes the URL
68
urlElem.hash = '/lol/ok';
69
expect(window.location.hash).to.be.equal('#/lol/ok');
71
// Changing the hash via normal means changes the urlElem.
72
var anchor = document.createElement('a');
73
anchor.href = '#/wat';
74
document.body.appendChild(anchor);
76
// In IE10 it appears that the hashchange event is asynchronous.
77
return timePasses(10).then(function() {
78
expect(urlElem.hash).to.be.equal('/wat');
81
test('basic functionality with paths', function() {
82
// Initialized to the window location's path.
83
expect(window.location.pathname).to.be.equal(urlElem.path);
85
// Changing the urlElem's path changes the URL
86
urlElem.path = '/foo/bar';
87
expect(window.location.pathname).to.be.equal('/foo/bar');
89
// Changing the path and sending a custom event on the window changes
91
window.history.replaceState({}, '', '/baz')
92
window.dispatchEvent(new CustomEvent('location-changed'));
93
expect(urlElem.path).to.be.equal('/baz');
95
test('basic functionality with ?key=value params', function() {
96
// Initialized to the window location's params.
97
expect(urlElem.query).to.be.eq('');
99
// Changing location.search and sending a custom event on the window
100
// changes the urlElem.
101
window.history.replaceState({}, '', '/?greeting=hello&target=world');
102
window.dispatchEvent(new CustomEvent('location-changed'));
103
expect(urlElem.query).to.be.equal('greeting=hello&target=world');
105
// Changing the urlElem's query changes the URL.
106
urlElem.query = 'greeting=hello&target=world&another=key';
107
expect(window.location.search).to.be.equal(
108
'?greeting=hello&target=world&another=key');
111
suite('when used with other iron-location elements', function() {
115
var elems = fixture('Together');
116
urlElem = elems.querySelector('#one');
117
otherUrlElem = elems.querySelector('#two');
119
function assertHaveSameUrls(urlElemOne, urlElemTwo) {
120
expect(urlElemOne.path).to.be.equal(urlElemTwo.path);
121
expect(urlElemOne.hash).to.be.equal(urlElemTwo.hash);
122
expect(urlElemOne.query).to.be.equal(urlElemTwo.query);
124
test('coordinate their changes (by firing events on window)', function() {
125
assertHaveSameUrls(urlElem, otherUrlElem);
127
urlElem.path = '/a/b/c';
128
assertHaveSameUrls(urlElem, otherUrlElem);
130
otherUrlElem.query = 'alsdkjflaksjfd=alksjfdlkajsdfl';
131
assertHaveSameUrls(urlElem, otherUrlElem);
133
urlElem.hash = 'lkjljifosjkldfjlksjfldsjf';
134
assertHaveSameUrls(urlElem, otherUrlElem);