我正在尝试使用 Kent C Dodds 的@testing-library/angular 来测试角度分量。我的组件依赖于 Angular 服务。我在 api 文档中没有看到有关如何注入模拟服务来为我的单元测试提供模拟数据的信息。
export class ItemCounterComponent implements OnInit {
public availableTodoCount: number;
constructor(private todoProviderService: TodoProviderService) {
this.todoProviderService.getTodos().subscribe(todos => {
this.availableTodoCount = todos.filter(todo => !todo.completed).length;
});
}
ngOnInit() {
}
}
describe('ItemCounterComponent', () => {
it('shows the count correctly', async () => {
const { getByText } = await render(ItemCounterComponent, { componentProperties: { availableTodoCount: 5 } });
expect (getByText('5 items left'));
});
});
````````````
Run Code Online (Sandbox Code Playgroud) 我正在使用 ngrx/component-store 并且到目前为止很喜欢它。拥有构建我自己的简单数组的先验商店知识,到目前为止,我唯一真正头痛的是当我必须更新数组并发现我必须始终为内部compare()管道创建一个新数组才能实现数组已更新。
无论如何,通读文档,它讨论了更新程序方法和 patchState。对我来说,他们做的事情完全相同,但他们的创作略有不同。您可以在方法内部调用 patchState,而 this.updater() 返回一个方法,为您提供可以在服务中公开的函数。每当我更新状态时,总是在网络调用之后。我假设在很多情况下您希望在没有网络调用的情况下更新状态,因此这就是为什么您希望有一个更新程序可供组件调用。问题是,如果更新程序和 patchState 确实在做同样的事情,那么在效果中调用更新程序或使用 patchState 是更好的做法,还是我在效果中放置了太多逻辑?
顺便说一句,文档说更新方法应该是一个纯函数。如果您使用它来将对象推送到数组上,那么它真的是纯粹的吗?
// adding the selectors so people know what components are subscribing to
readonly approvals$ = this.select(state => state.requestApprovals);
readonly registration$ = this.select(state => state);
readonly updateAssessment = this.effect(($judgement: Observable<{id: string, isApproved: boolean}>) => {
return $judgement.pipe(
switchMap((evaluation) => {
const state = this.get();
return this.requestApproval.patch(state.id, state.companyName, evaluation.id, evaluation.isApproved).pipe(
tapResponse(
(result) => {
// is it better to call patchState()?
this.patchState((state) => {
for(let i = …Run Code Online (Sandbox Code Playgroud)