it('should be wrapped in an array if array: true', function() {
      const m = $umf.compile('/foo?param1', { params: { param1: { array: true } } });

      // empty array [] is treated like "undefined"
      expect(m.format({ param1: undefined })).toBe('/foo');
      expect(m.format({ param1: [] })).toBe('/foo');
      expect(m.format({ param1: '' })).toBe('/foo');
      expect(m.format({ param1: '1' })).toBe('/foo?param1=1');
      expect(m.format({ param1: ['1'] })).toBe('/foo?param1=1');
      expect(m.format({ param1: ['1', '2'] })).toBe('/foo?param1=1&param1=2');

      expect(m.exec('/foo')).toEqual({ param1: undefined });
      expect(m.exec('/foo', {})).toEqual({ param1: undefined });
      expect(m.exec('/foo', { param1: '' })).toEqual({ param1: undefined });
      expect(m.exec('/foo', { param1: '1' })).toEqual({ param1: ['1'] });
      expect(m.exec('/foo', { param1: ['1', '2'] })).toEqual({ param1: ['1', '2'] });

      $url.url('/foo');
      expect(m.exec($url.path(), $url.search())).toEqual({ param1: undefined });
      $url.url('/foo?param1=');
      expect(m.exec($url.path(), $url.search())).toEqual({ param1: undefined });
      $url.url('/foo?param1=bar');
      expect(m.exec($url.path(), $url.search())).toEqual({ param1: ['bar'] });
      $url.url('/foo?param1=bar&param1=baz');
      if (angular.isArray($url.search()))
        // conditional for angular 1.0.8
        expect(m.exec($url.path(), $url.search())).toEqual({ param1: ['bar', 'baz'] });

      expect(m.format({})).toBe('/foo');
      expect(m.format({ param1: undefined })).toBe('/foo');
      expect(m.format({ param1: '' })).toBe('/foo');
      expect(m.format({ param1: 'bar' })).toBe('/foo?param1=bar');
      expect(m.format({ param1: ['bar'] })).toBe('/foo?param1=bar');
      expect(m.format({ param1: ['bar', 'baz'] })).toBe('/foo?param1=bar&param1=baz');
    });
Example #2
0
platformBrowserDynamic().bootstrapModule(KyloModule).then(platformRef => {
    const injector: Injector = platformRef.injector;
    const upgrade = injector.get(UpgradeModule) as UpgradeModule;

    // The DOM must be already be available
    upgrade.bootstrap(document.body, ["kylo"]);

    // Initialize the Angular Module (get() any UIRouter service from DI to initialize it)
    const url: UrlService = injector.get(UIRouter).urlService;//getUIRouter(injector).urlService;

    // Instruct UIRouter to listen to URL changes
    url.listen();
    url.sync();
});
    it('should automatically handle multiple search param values', function() {
      const m = $umf.compile('/foo/{fooid:int}?{bar:int}');

      $url.url('/foo/5?bar=1');
      expect(m.exec($url.path(), $url.search())).toEqual({ fooid: 5, bar: 1 });
      expect(m.format({ fooid: 5, bar: 1 })).toEqual('/foo/5?bar=1');

      $url.url('/foo/5?bar=1&bar=2&bar=3');
      if (angular.isArray($url.search()))
        // conditional for angular 1.0.8
        expect(m.exec($url.path(), $url.search())).toEqual({ fooid: 5, bar: [1, 2, 3] });
      expect(m.format({ fooid: 5, bar: [1, 2, 3] })).toEqual('/foo/5?bar=1&bar=2&bar=3');

      m.format();
    });
    it('should not be wrapped by ui-router into an array if array: false', function() {
      const m = $umf.compile('/foo?param1', { params: { param1: { array: false } } });

      expect(m.exec('/foo')).toEqualData({});

      $url.url('/foo?param1=bar');
      expect(m.exec($url.path(), $url.search())).toEqual({ param1: 'bar' });
      expect(m.format({ param1: 'bar' })).toBe('/foo?param1=bar');
      expect(m.format({ param1: ['bar'] })).toBe('/foo?param1=bar');

      $url.url('/foo?param1=bar&param1=baz');
      if (angular.isArray($url.search()))
        // conditional for angular 1.0.8
        expect(m.exec($url.path(), $url.search())).toEqual({ param1: 'bar,baz' }); // coerced to string
      expect(m.format({ param1: ['bar', 'baz'] })).toBe('/foo?param1=bar%2Cbaz'); // coerced to string
    });
    it('should be wrapped in an array if paramname looks like param[]', function() {
      const m = $umf.compile('/foo?param1[]');

      expect(m.exec('/foo')).toEqualData({});

      $url.url('/foo?param1[]=bar');
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar'] });
      expect(m.format({ 'param1[]': 'bar' })).toBe('/foo?param1[]=bar');
      expect(m.format({ 'param1[]': ['bar'] })).toBe('/foo?param1[]=bar');

      $url.url('/foo?param1[]=bar&param1[]=baz');
      if (angular.isArray($url.search()))
        // conditional for angular 1.0.8
        expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar', 'baz'] });
      expect(m.format({ 'param1[]': ['bar', 'baz'] })).toBe('/foo?param1[]=bar&param1[]=baz');
    });
    it('should be split on - in url and wrapped in an array if array: true', inject(function($location) {
      const m = $umf.compile('/foo/:param1', { params: { param1: { array: true } } });

      expect(m.exec('/foo/')).toEqual({ param1: undefined });
      expect(m.exec('/foo/bar')).toEqual({ param1: ['bar'] });
      $url.url('/foo/bar-baz');
      expect(m.exec($location.url())).toEqual({ param1: ['bar', 'baz'] });

      expect(m.format({ param1: [] })).toEqual('/foo/');
      expect(m.format({ param1: ['bar'] })).toEqual('/foo/bar');
      expect(m.format({ param1: ['bar', 'baz'] })).toEqual('/foo/bar-baz');
    }));
    it("should allow path param arrays with '-' in the values", function() {
      const m = $umf.compile('/foo/:param1[]');

      expect(m.exec('/foo/')).toEqual({ 'param1[]': undefined });
      expect(m.exec('/foo/bar\\-')).toEqual({ 'param1[]': ['bar-'] });
      expect(m.exec('/foo/bar\\--\\-baz')).toEqual({ 'param1[]': ['bar-', '-baz'] });

      expect(m.format({ 'param1[]': [] })).toEqual('/foo/');
      expect(m.format({ 'param1[]': ['bar-'] })).toEqual('/foo/bar%5C%2D');
      expect(m.format({ 'param1[]': ['bar-', '-baz'] })).toEqual('/foo/bar%5C%2D-%5C%2Dbaz');
      expect(m.format({ 'param1[]': ['bar-bar-bar-', '-baz-baz-baz'] })).toEqual(
        '/foo/bar%5C%2Dbar%5C%2Dbar%5C%2D-%5C%2Dbaz%5C%2Dbaz%5C%2Dbaz'
      );

      // check that we handle $location.url decodes correctly
      $url.url(m.format({ 'param1[]': ['bar-', '-baz'] }));
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar-', '-baz'] });

      // check that we handle $location.url decodes correctly for multiple hyphens
      $url.url(m.format({ 'param1[]': ['bar-bar-bar-', '-baz-baz-baz'] }));
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar-bar-bar-', '-baz-baz-baz'] });

      // check that pre-encoded values are passed correctly
      $url.url(m.format({ 'param1[]': ['%2C%20%5C%2C', '-baz'] }));
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['%2C%20%5C%2C', '-baz'] });
    });
    it('should behave similar to multi-value query params', function() {
      const m = $umf.compile('/foo/:param1[]');

      // empty array [] is treated like "undefined"
      expect(m.format({ 'param1[]': undefined })).toBe('/foo/');
      expect(m.format({ 'param1[]': [] })).toBe('/foo/');
      expect(m.format({ 'param1[]': '' })).toBe('/foo/');
      expect(m.format({ 'param1[]': '1' })).toBe('/foo/1');
      expect(m.format({ 'param1[]': ['1'] })).toBe('/foo/1');
      expect(m.format({ 'param1[]': ['1', '2'] })).toBe('/foo/1-2');

      expect(m.exec('/foo/')).toEqual({ 'param1[]': undefined });
      expect(m.exec('/foo/1')).toEqual({ 'param1[]': ['1'] });
      expect(m.exec('/foo/1-2')).toEqual({ 'param1[]': ['1', '2'] });

      $url.url('/foo/');
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': undefined });
      $url.url('/foo/bar');
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar'] });
      $url.url('/foo/bar-baz');
      expect(m.exec($url.path(), $url.search())).toEqual({ 'param1[]': ['bar', 'baz'] });

      expect(m.format({})).toBe('/foo/');
      expect(m.format({ 'param1[]': undefined })).toBe('/foo/');
      expect(m.format({ 'param1[]': '' })).toBe('/foo/');
      expect(m.format({ 'param1[]': 'bar' })).toBe('/foo/bar');
      expect(m.format({ 'param1[]': ['bar'] })).toBe('/foo/bar');
      expect(m.format({ 'param1[]': ['bar', 'baz'] })).toBe('/foo/bar-baz');
    });
Example #9
0
platformBrowserDynamic().bootstrapModule(SampleAppModuleAngular).then(platformRef => {
  // Initialize the Angular Module
  // get() the UIRouter instance from DI to initialize the router
  const urlService: UrlService = platformRef.injector.get(UIRouter).urlService;

  // Instruct UIRouter to listen to URL changes
  function startUIRouter() {
    urlService.listen();
    urlService.sync();
  }

  platformRef.injector.get<NgZone>(NgZone).run(startUIRouter);
});
    it('should allow custom types to handle multiple search param values manually', function() {
      $umf.type('custArray', {
        encode: function(array) {
          return array.join('-');
        },
        decode: function(val) {
          return angular.isArray(val) ? val : val.split(/-/);
        },
        equals: angular.equals,
        is: angular.isArray,
      });

      const m = $umf.compile('/foo?{bar:custArray}', { params: { bar: { array: false } } });

      $url.url('/foo?bar=fox');
      expect(m.exec($url.path(), $url.search())).toEqual({ bar: ['fox'] });
      expect(m.format({ bar: ['fox'] })).toEqual('/foo?bar=fox');

      $url.url('/foo?bar=quick-brown-fox');
      expect(m.exec($url.path(), $url.search())).toEqual({ bar: ['quick', 'brown', 'fox'] });
      expect(m.format({ bar: ['quick', 'brown', 'fox'] })).toEqual('/foo?bar=quick-brown-fox');
    });