PageRenderTime 28ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/ember-routing/tests/location/auto_location_test.js

https://gitlab.com/Aaeinstein54/ember.js
JavaScript | 292 lines | 231 code | 60 blank | 1 comment | 6 complexity | ef35a798f5ef279cf63fe09e2974f621 MD5 | raw file
  1. import { get } from 'ember-metal/property_get';
  2. import run from 'ember-metal/run_loop';
  3. import assign from 'ember-metal/assign';
  4. import AutoLocation from 'ember-routing/location/auto_location';
  5. import {
  6. getHistoryPath,
  7. getHashPath
  8. } from 'ember-routing/location/auto_location';
  9. import HistoryLocation from 'ember-routing/location/history_location';
  10. import HashLocation from 'ember-routing/location/hash_location';
  11. import NoneLocation from 'ember-routing/location/none_location';
  12. import buildOwner from 'container/tests/test-helpers/build-owner';
  13. import { OWNER } from 'container/owner';
  14. function mockBrowserLocation(overrides) {
  15. return assign({
  16. href: 'http://test.com/',
  17. pathname: '/',
  18. hash: '',
  19. search: '',
  20. replace() {
  21. ok(false, 'location.replace should not be called during testing');
  22. }
  23. }, overrides);
  24. }
  25. function mockBrowserHistory(overrides) {
  26. return assign({
  27. pushState() {
  28. ok(false, 'history.pushState should not be called during testing');
  29. },
  30. replaceState() {
  31. ok(false, 'history.replaceState should not be called during testing');
  32. }
  33. }, overrides);
  34. }
  35. function createLocation(location, history) {
  36. let owner = buildOwner();
  37. owner.register('location:history', HistoryLocation);
  38. owner.register('location:hash', HashLocation);
  39. owner.register('location:none', NoneLocation);
  40. let autolocation = AutoLocation.create({
  41. [OWNER]: owner,
  42. location: location,
  43. history: history,
  44. global: {}
  45. });
  46. return autolocation;
  47. }
  48. var location;
  49. QUnit.module('Ember.AutoLocation', {
  50. teardown() {
  51. if (location) {
  52. run(location, 'destroy');
  53. }
  54. }
  55. });
  56. QUnit.test('AutoLocation should return concrete implementation\'s value for `getURL`', function() {
  57. expect(1);
  58. var browserLocation = mockBrowserLocation();
  59. var browserHistory = mockBrowserHistory();
  60. location = createLocation(browserLocation, browserHistory);
  61. location.detect();
  62. var concreteImplementation = get(location, 'concreteImplementation');
  63. concreteImplementation.getURL = function() {
  64. return '/lincoln/park';
  65. };
  66. equal(location.getURL(), '/lincoln/park');
  67. });
  68. QUnit.test('AutoLocation should use a HistoryLocation instance when pushStates is supported', function() {
  69. expect(1);
  70. var browserLocation = mockBrowserLocation();
  71. var browserHistory = mockBrowserHistory();
  72. location = createLocation(browserLocation, browserHistory);
  73. location.detect();
  74. ok(get(location, 'concreteImplementation') instanceof HistoryLocation);
  75. });
  76. QUnit.test('AutoLocation should use a HashLocation instance when pushStates are not supported, but hashchange events are and the URL is already in the HashLocation format', function() {
  77. expect(1);
  78. var browserLocation = mockBrowserLocation({
  79. hash: '#/testd'
  80. });
  81. location = createLocation(browserLocation);
  82. location.global = {
  83. onhashchange() { }
  84. };
  85. location.detect();
  86. ok(get(location, 'concreteImplementation') instanceof HashLocation);
  87. });
  88. QUnit.test('AutoLocation should use a NoneLocation instance when neither history nor hashchange are supported.', function() {
  89. expect(1);
  90. location = createLocation(mockBrowserLocation());
  91. location.detect();
  92. ok(get(location, 'concreteImplementation') instanceof NoneLocation);
  93. });
  94. QUnit.test('AutoLocation should use an index path (i.e. \'/\') without any location.hash as OK for HashLocation', function() {
  95. expect(1);
  96. var browserLocation = mockBrowserLocation({
  97. href: 'http://test.com/',
  98. pathname: '/',
  99. hash: '',
  100. search: '',
  101. replace(path) {
  102. ok(false, 'location.replace should not be called');
  103. }
  104. });
  105. location = createLocation(browserLocation);
  106. location.global = {
  107. onhashchange() { }
  108. };
  109. location.detect();
  110. ok(get(location, 'concreteImplementation') instanceof HashLocation, 'uses a HashLocation');
  111. });
  112. QUnit.test('AutoLocation should transform the URL for hashchange-only browsers viewing a HistoryLocation-formatted path', function() {
  113. expect(3);
  114. var browserLocation = mockBrowserLocation({
  115. hash: '',
  116. hostname: 'test.com',
  117. href: 'http://test.com/test',
  118. pathname: '/test',
  119. protocol: 'http:',
  120. port: '',
  121. search: '',
  122. replace(path) {
  123. equal(path, 'http://test.com/#/test', 'location.replace should be called with normalized HashLocation path');
  124. }
  125. });
  126. var location = createLocation(browserLocation);
  127. location.global = {
  128. onhashchange() { }
  129. };
  130. location.detect();
  131. ok(get(location, 'concreteImplementation') instanceof NoneLocation, 'NoneLocation should be used while we attempt to location.replace()');
  132. equal(get(location, 'cancelRouterSetup'), true, 'cancelRouterSetup should be set so the router knows.');
  133. });
  134. QUnit.test('AutoLocation should replace the URL for pushState-supported browsers viewing a HashLocation-formatted url', function() {
  135. expect(2);
  136. var browserLocation = mockBrowserLocation({
  137. hash: '#/test',
  138. hostname: 'test.com',
  139. href: 'http://test.com/#/test',
  140. pathname: '/',
  141. protocol: 'http:',
  142. port: '',
  143. search: ''
  144. });
  145. var browserHistory = mockBrowserHistory({
  146. replaceState(state, title, path) {
  147. equal(path, '/test', 'history.replaceState should be called with normalized HistoryLocation url');
  148. }
  149. });
  150. var location = createLocation(browserLocation, browserHistory);
  151. location.detect();
  152. ok(get(location, 'concreteImplementation'), HistoryLocation);
  153. });
  154. QUnit.test('AutoLocation requires any rootURL given to end in a trailing forward slash', function() {
  155. expect(3);
  156. var browserLocation = mockBrowserLocation();
  157. var expectedMsg = /rootURL must end with a trailing forward slash e.g. "\/app\/"/;
  158. location = createLocation(browserLocation);
  159. location.rootURL = 'app';
  160. expectAssertion(function() {
  161. location.detect();
  162. }, expectedMsg);
  163. location.rootURL = '/app';
  164. expectAssertion(function() {
  165. location.detect();
  166. }, expectedMsg);
  167. // Note the trailing whitespace
  168. location.rootURL = '/app/ ';
  169. expectAssertion(function() {
  170. location.detect();
  171. }, expectedMsg);
  172. });
  173. QUnit.test('AutoLocation provides its rootURL to the concreteImplementation', function() {
  174. expect(1);
  175. var browserLocation = mockBrowserLocation({
  176. pathname: '/some/subdir/derp'
  177. });
  178. var browserHistory = mockBrowserHistory();
  179. location = createLocation(browserLocation, browserHistory);
  180. location.rootURL = '/some/subdir/';
  181. location.detect();
  182. var concreteLocation = get(location, 'concreteImplementation');
  183. equal(location.rootURL, concreteLocation.rootURL);
  184. });
  185. QUnit.test('getHistoryPath() should return a normalized, HistoryLocation-supported path', function() {
  186. expect(3);
  187. var browserLocation = mockBrowserLocation({
  188. href: 'http://test.com/app/about?foo=bar#foo',
  189. pathname: '/app/about',
  190. search: '?foo=bar',
  191. hash: '#foo'
  192. });
  193. equal(getHistoryPath('/app/', browserLocation), '/app/about?foo=bar#foo', 'URLs already in HistoryLocation form should come out the same');
  194. browserLocation = mockBrowserLocation({
  195. href: 'http://test.com/app/#/about?foo=bar#foo',
  196. pathname: '/app/',
  197. search: '',
  198. hash: '#/about?foo=bar#foo'
  199. });
  200. equal(getHistoryPath('/app/', browserLocation), '/app/about?foo=bar#foo', 'HashLocation formed URLs should be normalized');
  201. browserLocation = mockBrowserLocation({
  202. href: 'http://test.com/app/#about?foo=bar#foo',
  203. pathname: '/app/',
  204. search: '',
  205. hash: '#about?foo=bar#foo'
  206. });
  207. equal(getHistoryPath('/app', browserLocation), '/app/#about?foo=bar#foo', 'URLs with a hash not following #/ convention shouldn\'t be normalized as a route');
  208. });
  209. QUnit.test('getHashPath() should return a normalized, HashLocation-supported path', function() {
  210. expect(3);
  211. var browserLocation = mockBrowserLocation({
  212. href: 'http://test.com/app/#/about?foo=bar#foo',
  213. pathname: '/app/',
  214. search: '',
  215. hash: '#/about?foo=bar#foo'
  216. });
  217. equal(getHashPath('/app/', browserLocation), '/app/#/about?foo=bar#foo', 'URLs already in HistoryLocation form should come out the same');
  218. browserLocation = mockBrowserLocation({
  219. href: 'http://test.com/app/about?foo=bar#foo',
  220. pathname: '/app/about',
  221. search: '?foo=bar',
  222. hash: '#foo'
  223. });
  224. equal(getHashPath('/app/', browserLocation), '/app/#/about?foo=bar#foo', 'HistoryLocation formed URLs should be normalized');
  225. browserLocation = mockBrowserLocation({
  226. href: 'http://test.com/app/#about?foo=bar#foo',
  227. pathname: '/app/',
  228. search: '',
  229. hash: '#about?foo=bar#foo'
  230. });
  231. equal(getHashPath('/app/', browserLocation), '/app/#/#about?foo=bar#foo', 'URLs with a hash not following #/ convention shouldn\'t be normalized as a route');
  232. });