简体   繁体   English

如何测试Angular中的route queryParams?

[英]How to test route queryParams in Angular?

I have a components gets a value from the params of ActivatedRoute.我有一个组件从 ActivatedRoute 的参数中获取一个值。

the components looks like组件看起来像

constructor(
  private route: ActivatedRoute,
  private router: Router,
  private marketService: MarketService,
) { }

ngOnInit() {
  // New Post
  this.route.queryParams.pipe(
    filter(params => params.type))
    .subscribe(
      params => {
        this.isNew = true;
        this.brd_path = params.type;
        this.category_path = this.route.snapshot.params['category_path'];
      },
      () => { },
      () => { }
    );

  // Edit Post
  this.route.queryParams.pipe(
    filter(params => params.no)) 
    .subscribe(
      params => {
        if (!this.marketService.getCurrentMarket()) {
          this.router.navigateByUrl('');
          return;
        }
        this.isNew = false;
        this.market_no = +params.no; 
      }
    );
}

I want to test depend params isNew, brd_path, category_path, market_no我想测试 depend params isNew, brd_path, category_path, market_no

TEST code测试代码

describe('SellCreateComponent', () => {
  let component: SellCreateComponent;
  let fixture: ComponentFixture<SellCreateComponent>;
  let el: HTMLElement;
 
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule,
        BrowserAnimationsModule,
      ],
      declarations: [SellCreateComponent],
      providers: [
        { provide: MarketService },
        {
          provide: ActivatedRoute, useValue: {
            paramMap: of(convertToParamMap({ brd_path: 'replica' }))
          }
        },
      ]
    }).compileComponents();
    fixture = TestBed.createComponent(SellCreateComponent);
    component = fixture.componentInstance;
    el = fixture.debugElement.nativeElement;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  describe('ngOnInit', () => {
    it('should get the route parameter', fakeAsync(() => {
      component.ngOnInit();

      expect(component.brd_path).toBe('replica');
      expect(component.isNew).toBe(true);
    }));
  });

});

when i run the test.当我运行测试时。 i got the error.我得到了错误。

SellCreateComponent > ngOnInit > should get the route parameter TypeError: Cannot read properties of undefined (reading 'pipe') SellCreateComponent > ngOnInit > 应该获取路由参数 TypeError: Cannot read properties of undefined (reading 'pipe')

Thank you!谢谢!

I think you're mocking ActivatedRoute incorrectly.我认为您 mocking ActivatedRoute错误。

You're supplying paramMap but you're looking for queryParams .您正在提供paramMap但您正在寻找queryParams

Try this:尝试这个:

provide: ActivatedRoute, useValue: {
            queryParams: of(convertToParamMap({ brd_path: 'replica' }))
          }

That should hopefully get rid of that error.这应该有望消除该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM