AngularLocationWrapper.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { HistoryWrapper, locationService, setLocationService } from '@grafana/runtime';
  2. import { AngularLocationWrapper } from './AngularLocationWrapper';
  3. // The methods in this file are deprecated
  4. // Stub the deprecation warning here to prevent polluting the test output
  5. jest.mock('@grafana/data', () => ({
  6. ...jest.requireActual('@grafana/data'),
  7. deprecationWarning: () => {},
  8. }));
  9. describe('AngularLocationWrapper', () => {
  10. const { location } = window;
  11. beforeEach(() => {
  12. setLocationService(new HistoryWrapper());
  13. });
  14. beforeAll(() => {
  15. // @ts-ignore
  16. delete window.location;
  17. window.location = {
  18. ...location,
  19. hash: '#hash',
  20. host: 'localhost:3000',
  21. hostname: 'localhost',
  22. href: 'http://www.domain.com:9877/path/b?search=a&b=c&d#hash',
  23. origin: 'http://www.domain.com:9877',
  24. pathname: '/path/b',
  25. port: '9877',
  26. protocol: 'http:',
  27. search: '?search=a&b=c&d',
  28. };
  29. });
  30. afterAll(() => {
  31. window.location = location;
  32. });
  33. const wrapper = new AngularLocationWrapper();
  34. it('should provide common getters', () => {
  35. locationService.push('/path/b?search=a&b=c&d#hash');
  36. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#hash');
  37. expect(wrapper.protocol()).toBe('http');
  38. expect(wrapper.host()).toBe('www.domain.com');
  39. expect(wrapper.port()).toBe(9877);
  40. expect(wrapper.path()).toBe('/path/b');
  41. expect(wrapper.search()).toEqual({ search: 'a', b: 'c', d: true });
  42. expect(wrapper.hash()).toBe('hash');
  43. expect(wrapper.url()).toBe('/path/b?search=a&b=c&d#hash');
  44. });
  45. describe('path', () => {
  46. it('should change path', function () {
  47. locationService.push('/path/b?search=a&b=c&d#hash');
  48. wrapper.path('/new/path');
  49. expect(wrapper.path()).toBe('/new/path');
  50. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/new/path?search=a&b=c&d#hash');
  51. });
  52. it('should not break on numeric values', function () {
  53. locationService.push('/path/b?search=a&b=c&d#hash');
  54. wrapper.path(1);
  55. expect(wrapper.path()).toBe('/1');
  56. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/1?search=a&b=c&d#hash');
  57. });
  58. it('should allow using 0 as path', function () {
  59. locationService.push('/path/b?search=a&b=c&d#hash');
  60. wrapper.path(0);
  61. expect(wrapper.path()).toBe('/0');
  62. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/0?search=a&b=c&d#hash');
  63. });
  64. it('should set to empty path on null value', function () {
  65. locationService.push('/path/b?search=a&b=c&d#hash');
  66. wrapper.path('/foo');
  67. expect(wrapper.path()).toBe('/foo');
  68. wrapper.path(null);
  69. expect(wrapper.path()).toBe('/');
  70. });
  71. });
  72. describe('search', () => {
  73. it('should accept string', function () {
  74. locationService.push('/path/b');
  75. wrapper.search('x=y&c');
  76. expect(wrapper.search()).toEqual({ x: 'y', c: true });
  77. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?x=y&c');
  78. });
  79. it('search() should accept object', function () {
  80. locationService.push('/path/b');
  81. wrapper.search({ one: '1', two: true });
  82. expect(wrapper.search()).toEqual({ one: '1', two: true });
  83. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?one=1&two');
  84. });
  85. it('should copy object', function () {
  86. locationService.push('/path/b');
  87. const obj: Record<string, any> = { one: '1', two: true, three: null };
  88. wrapper.search(obj);
  89. expect(obj).toEqual({ one: '1', two: true, three: null });
  90. obj.one = 'changed';
  91. expect(wrapper.search()).toEqual({ one: '1', two: true });
  92. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?one=1&two');
  93. });
  94. it('should change single parameter', function () {
  95. wrapper.search({ id: 'old', preserved: true });
  96. wrapper.search('id', 'new');
  97. expect(wrapper.search()).toEqual({ id: 'new', preserved: true });
  98. });
  99. it('should remove single parameter', function () {
  100. wrapper.search({ id: 'old', preserved: true });
  101. wrapper.search('id', null);
  102. expect(wrapper.search()).toEqual({ preserved: true });
  103. });
  104. it('should remove multiple parameters', function () {
  105. locationService.push('/path/b');
  106. wrapper.search({ one: '1', two: true });
  107. expect(wrapper.search()).toEqual({ one: '1', two: true });
  108. wrapper.search({ one: null, two: null });
  109. expect(wrapper.search()).toEqual({});
  110. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b');
  111. });
  112. it('should accept numeric keys', function () {
  113. locationService.push('/path/b');
  114. wrapper.search({ 1: 'one', 2: 'two' });
  115. expect(wrapper.search()).toEqual({ '1': 'one', '2': 'two' });
  116. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?1=one&2=two');
  117. });
  118. it('should handle multiple value', function () {
  119. wrapper.search('a&b');
  120. expect(wrapper.search()).toEqual({ a: true, b: true });
  121. wrapper.search('a', null);
  122. expect(wrapper.search()).toEqual({ b: true });
  123. wrapper.search('b', undefined);
  124. expect(wrapper.search()).toEqual({});
  125. });
  126. it('should handle single value', function () {
  127. wrapper.search('ignore');
  128. expect(wrapper.search()).toEqual({ ignore: true });
  129. wrapper.search(1);
  130. expect(wrapper.search()).toEqual({ 1: true });
  131. });
  132. });
  133. describe('url', () => {
  134. it('should change the path, search and hash', function () {
  135. wrapper.url('/some/path?a=b&c=d#hhh');
  136. expect(wrapper.url()).toBe('/some/path?a=b&c=d#hhh');
  137. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/some/path?a=b&c=d#hhh');
  138. expect(wrapper.path()).toBe('/some/path');
  139. expect(wrapper.search()).toEqual({ a: 'b', c: 'd' });
  140. expect(wrapper.hash()).toBe('hhh');
  141. });
  142. it('should change only hash when no search and path specified', function () {
  143. locationService.push('/path/b?search=a&b=c&d');
  144. wrapper.url('#some-hash');
  145. expect(wrapper.hash()).toBe('some-hash');
  146. expect(wrapper.url()).toBe('/path/b?search=a&b=c&d#some-hash');
  147. expect(wrapper.absUrl()).toBe('http://www.domain.com:9877/path/b?search=a&b=c&d#some-hash');
  148. });
  149. it('should change only search and hash when no path specified', function () {
  150. locationService.push('/path/b');
  151. wrapper.url('?a=b');
  152. expect(wrapper.search()).toEqual({ a: 'b' });
  153. expect(wrapper.hash()).toBe('');
  154. expect(wrapper.path()).toBe('/path/b');
  155. });
  156. it('should reset search and hash when only path specified', function () {
  157. locationService.push('/path/b?search=a&b=c&d#hash');
  158. wrapper.url('/new/path');
  159. expect(wrapper.path()).toBe('/new/path');
  160. expect(wrapper.search()).toEqual({});
  161. expect(wrapper.hash()).toBe('');
  162. });
  163. it('should change path when empty string specified', function () {
  164. locationService.push('/path/b?search=a&b=c&d#hash');
  165. wrapper.url('');
  166. expect(wrapper.path()).toBe('/');
  167. expect(wrapper.search()).toEqual({});
  168. expect(wrapper.hash()).toBe('');
  169. });
  170. });
  171. });