Angular 2.0.0-rc.1 + karma: provide RouteSegment

When a component is introduced using RouteSegment, simply providing the RouteSegment in the test is not enough:

component.ts:

export class ComponentToTest {
  private param: string;

  constructor(routeSegment: RouteSegment) {
    this.param = routeSegment.getParam('param');
    this.getData();
  }
}

component.spec.ts:

import {Router, RouteSegment} from '@angular/router';

import {it, inject, beforeEachProviders} from '@angular/core/testing';
import {ROUTER_FAKE_PROVIDERS} from '@angular/router/testing';

import {ComponentToTest} from './component';

describe('ComponentToTest', () => {
  beforeEachProviders(() => [
    ROUTER_FAKE_PROVIDERS,
    RouteSegment,
    ComponentToTest
  ]);

  it('should call getData() on contruct', inject([Router], (router) => {
    spyOn(ComponentToTest.prototype, 'getData');
    expect(ComponentToTest.prototype.getData).not.toHaveBeenCalled();
    let component = new ComponentToTest(router);
    expect(ComponentToTest.prototype.getData).toHaveBeenCalled();
  }));
});

The following error will occur:

Error: Cannot resolve all parameters for "RouteSegment" (?,?,?,?,?). Make sure that all parameters are decorated with input or have valid type annotations and that "RouteSegment" is decorated with Input.

I do not know how to provide RouteSegment parameters.

+3
source share
2 answers

Solution 1

See HomeBrew answer

Not working for me, still not sure why.

Decision 2

Layout implementation of RouteSegment:

class MockRouteSegment implements RouteSegment {
  urlSegments: any;
  parameters: any;
  outlet: string;
  _type: any;
  _componentFactory: any;
  type: any;
  stringifiedUrlSegments: string;
  constructor(parameters?: { [key: string]: any; }) {
    this.parameters = parameters
  }
  getParam(param: string) {
    return this.parameters[param];
  }
}

Provide this as follows:

provide(RouteSegment, { useValue: new MockRouteSegment({ 'key': 'value' }) })
+1

, angular , . routeSegment :

import {RouteSegment, Router} from '@angular/router';
provide(RouteSegment, {useFactory: (r: any) => r.routeTree.root, deps: [Router]})

RouteSegment, , RouteParams:

provide(RouteParams, { useValue: new RouteParams({ page: 'terms-conditions' }) }),

:

provide(RouteSegment, { useValue: new RouteSegment([], { page: 'terms-conditions' }, null, null, null) }),

, .

+2

All Articles