Mika
Mika

Reputation: 73

NGRX Entity test in component

I have a component with an entity selector 'selectAllProperties' in ngOnInit, and I want to test this component:

ngOnInit() {
    this.store.dispatch(new LoadPropertiesRequested());
    this.properties$ = this.store.pipe(select(selectAllProperties));
    this.loading$ = this.store.pipe(select(selectPropertiesLoading));
    this.logs$ = this.store.pipe(select(selectPropertiesLogs));
  }

In my spec file, i initialized the store like in the ngrx doc:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          ...fromRoot.reducers,
          feature: combineReducers(fromProperties.reducer),
        })
      ],
      declarations: [
        SidebarPropertiesComponent,
        SidebarElementComponent
      ]
    })
    .compileComponents();
  }));

Thanks

Upvotes: 2

Views: 846

Answers (1)

Mika
Mika

Reputation: 73

I have found the problem, in TestBed.configureTestingModule

instead of

  imports: [
    StoreModule.forRoot({
      ...fromRoot.reducers,
      feature: combineReducers(fromProperties.reducer),
    })
  ],

use

  imports: [
    StoreModule.forRoot(reducers, { metaReducers }),
    StoreModule.forFeature('properties', fromProperties.reducer),
  ],
  • No more 'TypeError: Cannot read property 'ids' of undefined'
  • I can mock the properties
 it('should have 2 properties elements', () => {
   store.dispatch(new LoadPropertiesSuccess({properties: propertiesMock}));
   fixture.detectChanges();

   const list = debugElement.queryAll(By.css('li'));
   expect(list.length).toBe(2);
 });

Upvotes: 1

Related Questions