相关疑难解决方法(0)

如何使用 Jasmine 在 Angular 单元测试中模拟 window.screen.width

我有一个 BreakpointService,它告诉我 - 取决于屏幕宽度 - 我应该在哪个 SidebarMode(关闭 - 缩小 - 打开)中显示我的侧边栏。

这是服务的主要部分:

constructor(private breakpointObserver: BreakpointObserver) {
    this.closed$ = this.breakpointObserver.observe(['(min-width: 1024px)']).pipe(
      filter((state: BreakpointState) => !state.matches),
      mapTo(SidebarMode.Closed)
    );

    this.opened$ = this.breakpointObserver.observe(['(min-width: 1366px)']).pipe(
      filter((state: BreakpointState) => state.matches),
      mapTo(SidebarMode.Open)
    );

    const minifiedStart$: Observable<boolean> = this.breakpointObserver.observe(['(min-width: 1024px)']).pipe(map(state => state.matches));

    const minifiedEnd$: Observable<boolean> = this.breakpointObserver.observe(['(max-width: 1366px)']).pipe(map(state => state.matches));

    this.minified$ = minifiedStart$.pipe(
      flatMap(start => minifiedEnd$.pipe(map(end => start && end))),
      distinctUntilChanged(),
      filter(val => val === true),
      mapTo(SidebarMode.Minified)
    );

    this.observer$ = merge(this.closed$, this.minified$, this.opened$);
  }
Run Code Online (Sandbox Code Playgroud)

通过这一行,我可以订阅事件:

this.breakpointService.observe().subscribe();
Run Code Online (Sandbox Code Playgroud)

现在,我想在单元测试中测试不同的模式,但我不知道

如何在测试中模拟 …

unit-testing chromium jasmine karma-jasmine angular

9
推荐指数
2
解决办法
7959
查看次数

标签 统计

angular ×1

chromium ×1

jasmine ×1

karma-jasmine ×1

unit-testing ×1