link_srv.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { LinkSrv } from '../link_srv';
  2. import _ from 'lodash';
  3. jest.mock('angular', () => {
  4. const AngularJSMock = require('test/mocks/angular');
  5. return new AngularJSMock();
  6. });
  7. describe('linkSrv', () => {
  8. let linkSrv;
  9. const templateSrvMock = {};
  10. const timeSrvMock = {};
  11. beforeEach(() => {
  12. linkSrv = new LinkSrv(templateSrvMock, timeSrvMock);
  13. });
  14. describe('when appending query strings', () => {
  15. it('add ? to URL if not present', () => {
  16. const url = linkSrv.appendToQueryString('http://example.com', 'foo=bar');
  17. expect(url).toBe('http://example.com?foo=bar');
  18. });
  19. it('do not add & to URL if ? is present but query string is empty', () => {
  20. const url = linkSrv.appendToQueryString('http://example.com?', 'foo=bar');
  21. expect(url).toBe('http://example.com?foo=bar');
  22. });
  23. it('add & to URL if query string is present', () => {
  24. const url = linkSrv.appendToQueryString('http://example.com?foo=bar', 'hello=world');
  25. expect(url).toBe('http://example.com?foo=bar&hello=world');
  26. });
  27. it('do not change the URL if there is nothing to append', () => {
  28. _.each(['', undefined, null], toAppend => {
  29. const url1 = linkSrv.appendToQueryString('http://example.com', toAppend);
  30. expect(url1).toBe('http://example.com');
  31. const url2 = linkSrv.appendToQueryString('http://example.com?', toAppend);
  32. expect(url2).toBe('http://example.com?');
  33. const url3 = linkSrv.appendToQueryString('http://example.com?foo=bar', toAppend);
  34. expect(url3).toBe('http://example.com?foo=bar');
  35. });
  36. });
  37. });
  38. });