10

Angular 6、Rxjs、Jest、Jasmine-marbles。

非常常见的场景:搜索服务器端项目的组件。在组件中,有一些控件可以更改搜索条件,我想以“反应式”进行编码。所以在组件代码中我有这样的东西:

class SearchComponent  implements OnInit {
  public searchData: SearchData = {};
  public searchConditions$ = new Subject<SearchData>();

  constructor(private searchService: SearchService) { }

  public results$: Observable<ResultData> = this.searchConditions$.pipe(
    distinctUntilChanged(this.compareProperties), // omissis but it works
    flatMap(searchData => this.searchService.search(searchData)),
    shareReplay(1)
  );

  // search actions
  ngOnInit() {
    this.searchConditions$.next(this.searchData);
  }
  public onChangeProp1(prop1: string) {
    this.searchData = { ...this.searchData, prop1 };
    this.searchConditions$.next(this.searchData);
  }
  public onChangeProp2(prop2: string) {
    this.searchData = { ...this.searchData, prop2 };
    this.searchConditions$.next(this.searchData);
  }
}

也就是说,Subject每次 UI 中的某些内容发生更改时都会触发搜索条件。

现在我想测试一下,搜索服务只会被调用以获取不同的输入。我可以用这种方式“没有弹珠”:

test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
    service.search = jest.fn(() => of(TestData.results));
    component.results$.subscribe({
        complete: () => {
            expect(service.Search).toHaveBeenCalledTimes(1);
            done();
        }
    });

    component.searchConditions$.next(TestData.searchCriteria);
    component.searchConditions$.next(TestData.searchCriteria);
    component.searchConditions$.next(TestData.searchCriteria);
    component.searchConditions$.complete();
});

现在我想用jasmine marbles转换这个测试,但我不知道如何......

我想要这样的东西:

test('when searchConditions$ come with equal events search service will not be called more than once', (done: any) => {
    service.search = jest.fn(() => of(TestData.results));
    component.searchConditions$ = cold('--a--a|', { a : TestData.searchCriteria});
    const expected = cold('--b---|', { b : TestData.results});
    expect(component.results$).toBeObservable(expected);
});

显然,它不起作用...

更新

以某种方式关闭...使用“测试助手”

test('when searchConditions$ comes with equal events search service will not be called more than once - marble version', () => {
    service.search = jest.fn(() => of(TestData.results));
    const stream   = cold('--a--a|', { a : TestData.searchCriteria});
    const expected = cold('--b---|', { b : TestData.results});
    stubSubject(component.searchConditions$, stream);
    expect(component.results$).toBeObservable(expected);
});


// test helpers
const stubSubject = (subject: Subject<any> , marbles: TestObservable) => {
    marbles.subscribe({
        next: (value: any) => subject.next(value),
        complete: () => subject.complete(),
        error: (e) => subject.error(e)
    });
};
4

1 回答 1

1

测试的主要目标是模拟依赖项,并且不更改测试单元 SearchComponent 内部的任何内容。

因此stubSubject(component.searchConditions$, stream)orcomponent.searchConditions$ = cold是一种不好的做法。

因为我们想在其中安排发射,所以searchConditions$我们需要一个内部的预定流,我们也可以在这里使用coldor 。hot

源数据(对不起,我猜到了一些类型)


type SearchData = {
    prop?: string;
};
type ResultData = Array<string>;

@Injectable()
class SearchService {
    public search(term: SearchData): Observable<any> {
        return of();
    }
}

@Component({
    selector: 'search',
    template: '',
})
class SearchComponent  implements OnInit {
    public searchData: SearchData = {};
    public searchConditions$ = new Subject<SearchData>();

    constructor(private searchService: SearchService) { }

    public results$: Observable<ResultData> = this.searchConditions$.pipe(
        distinctUntilKeyChanged('prop'),
        tap(console.log),
        flatMap(searchData => this.searchService.search(searchData)),
        shareReplay(1),
    );

    // search actions
    ngOnInit() {
        this.searchConditions$.next(this.searchData);
    }
    public onChangeProp1(prop1: string) {
        this.searchData = { ...this.searchData, prop: prop1 }; // I've changed it to prop
        this.searchConditions$.next(this.searchData);
    }
    public onChangeProp2(prop2: string) {
        this.searchData = { ...this.searchData, prop: prop2 }; // I've changed it to prop
        this.searchConditions$.next(this.searchData);
    }
}

及其测试

test('when searchConditions$ come with equal events search service will not be called more than once', () => {
    service.search = jest.fn(() => of(TestData.results));

    // our internal scheduler how we click our component.
    // the first delay `-` is important to allow `expect` to subscribe.
    cold('-a--a--b--b--a-|', {
        a: 'a',
        b: 'b',
    }).pipe(
        tap(v => component.searchConditions$.next({prop: v})),
        // or like user does it
        // tap(v => component.onChangeProp1(v)),
        finalize(() => component.searchConditions$.complete()),
    ).subscribe();

    // adding our expectations.
    expect(component.results$).toBeObservable(cold('-r-----r-----r-|', {
        r: TestData.results,
    }));
});

现在我们不改变我们的测试单元,只改变它的依赖(service.search)。

于 2020-05-16T11:33:27.570 回答