• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-route</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="./app-example-1.html">
20</head>
21<body>
22  <test-fixture id="ExampleApp">
23    <template>
24      <app-example-1></app-example-1>
25    </template>
26  </test-fixture>
27<script>
28  'use strict';
29
30  function setLocation(url) {
31    window.history.pushState({}, '', url);
32    Polymer.Base.fire('location-changed', {}, { node: window });
33  }
34
35  suite('<test-app-example-1>', function () {
36    var originalLocation;
37    var exampleApp;
38
39    setup(function() {
40      originalLocation = window.location.href;
41      exampleApp = fixture('ExampleApp');
42    });
43
44    teardown(function() {
45      window.history.replaceState({}, '', originalLocation);
46    });
47
48    test('runs through basic usage', function() {
49      // Navigate to /lol
50      setLocation('/lol');
51
52      expect(exampleApp.data).to.be.deep.eq({
53        page: 'lol'
54      });
55      expect(exampleApp.userData).to.be.deep.eq({
56      });
57      expect(exampleApp.route).to.be.deep.eq({
58        prefix: '',
59        path: '/lol',
60        __queryParams: {}
61      });
62      expect(exampleApp.userRoute).to.be.deep.eq({
63        prefix: null,
64        path: null,
65        __queryParams: {}
66      });
67      expect(window.location.pathname).to.be.equal('/lol');
68
69      // Navigate to /user
70      setLocation('/user');
71      expect(exampleApp.data).to.be.deep.eq({
72        page: 'user'
73      });
74
75      // We should have redirected to /user/view because of a redirect in
76      // the example app code.
77      expect(exampleApp.route).to.be.deep.eq({
78        prefix: '',
79        path: '/user/view',
80        __queryParams: {}
81      });
82      expect(exampleApp.userRoute).to.be.deep.eq({
83        prefix: '/user',
84        path: '/view',
85        __queryParams: {}
86      });
87      expect(window.location.pathname).to.be.equal('/user/view');
88
89      // Navigate to /user/details
90      setLocation('/user/details');
91      expect(exampleApp.data).to.be.deep.eq({
92        page: 'user'
93      });
94      expect(exampleApp.userData).to.be.deep.eq({
95        page: 'details'
96      });
97      expect(exampleApp.route).to.be.deep.eq({
98        prefix: '',
99        path: '/user/details',
100        __queryParams: {}
101      });
102      expect(exampleApp.userRoute).to.be.deep.eq({
103        prefix: '/user',
104        path: '/details',
105        __queryParams: {}
106      });
107      expect(window.location.pathname).to.be.equal('/user/details');
108
109      exampleApp.set('data.page', 'redirectToUser');
110      expect(window.location.pathname).to.be.equal('/user/view');
111
112      // This triggers two redirects in a row!
113      setLocation('/redirectToUser');
114      expect(window.location.pathname).to.be.equal('/user/view');
115
116      // Data binding changes to a different user subpage.
117      exampleApp.set('userData.page', 'profile');
118      expect(window.location.pathname).to.be.eq('/user/profile');
119
120      // Data binding changes to the aunt of the current page.
121      exampleApp.set('data.page', 'feed');
122      expect(window.location.pathname).to.be.eq('/feed');
123
124      setLocation('/user/etc');
125      exampleApp.set('userData.page', 'details');
126      expect(window.location.pathname).to.be.eq('/user/details')
127
128      expect(window.location.search).to.be.eq('');
129      exampleApp.set('userQueryParams.foo', 'bar');
130      expect(window.location.search).to.be.eq('?foo=bar');
131
132      exampleApp.userQueryParams = {bar: 'baz'};
133      expect(window.location.search).to.be.eq('?bar=baz');
134    });
135  });
136</script>
137</body>
138