Activated ルートから Id パラメータを読み取るコンポーネントをテストしようとしています。現在の目標は、コンポーネントを正しくレンダリングすることですが、テストが失敗する理由がわかりません。
同様のコンポーネントに対して同じ方法で ActivatedRoute を渡しましたが、問題なく動作しました。
成分:
@Component({
selector: 'app-recipe-detail',
templateUrl: './recipe-detail.component.html',
styleUrls: ['./recipe-detail.component.css']
})
export class RecipeDetailComponent implements OnInit {
recipeState: Observable<fromRecipe.State>;
id: number;
constructor(
private route: ActivatedRoute,
private router: Router,
private store: Store<fromRecipe.FeatureState>
) { }
// pass the "id" to return the index of the array
ngOnInit() {
this.route.params
.subscribe((params: Params) => {
this.id = +params['id'];
this.recipeState = this.store.select('recipes');
});
}
onAddToShopping() {
this.store.select('recipes').pipe(take(1)).subscribe((recipeState: fromRecipe.State) => {
this.store.dispatch(new ShoppingListAction.AddIngredients(recipeState.recipes[this.id].ingredients));
});
}
onEditRecipe() {
this.router.navigate(['edit'], {relativeTo: this.route});
}
onDeleteRecipe() {
this.store.dispatch(new RecipeActions.DeleteRecipe(this.id));
this.router.navigate(['./recipes']);
}
}
スペックファイル
describe('Recipe Detail Component should', () => {
let fixture: ComponentFixture<RecipeDetailComponent>;
let component: RecipeDetailComponent;
let store: TestStore<any>;
const router = {
navigate: jasmine.createSpy('navigate')
};
let dispatchSpy;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ RecipeDetailComponent ],
imports: [RouterTestingModule],
providers: [
{provide: Store, useClass: TestStore},
{ provide: Router, useValue: router },
{
provide: ActivatedRoute,
useValue: {
params: of({ id: 0 })
}
}],
}).compileComponents();
});
beforeEach(async(inject([Store], (testStore: TestStore<any>) => {
store = testStore;
testStore.setState({initialState});
fixture = TestBed.createComponent(RecipeDetailComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
})));
it('render correctly', () => {
console.log(component);
expect(component).toBeTruthy();
});
});