Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngrx/store - testing, throw TypeError: Cannot read property 'pipe' of undefined

Here is my ts file:

this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
            if (data && data.length) {
                this.allRegCategories = data;
            }
        });

when i go for testing getting error as:

this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {

TypeError: Cannot read property 'pipe' of undefined

how to provide the pipe here? what is the correct way to solve this?

Here is my test spec file:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Store, select } from '@ngrx/store';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { ShellViewProgMgmtComponent } from './shell-view-prog-mgmt.component';
import { ViewProgMgmtComponent } from './../../components/view-prog-mgmt/view-prog-mgmt.component';
import * as actions from './../../state/actions/setup-config.actions';

describe('ShellViewProgMgmtComponent', () => {
    let component: ShellViewProgMgmtComponent;
    let fixture: ComponentFixture<ShellViewProgMgmtComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [ShellViewProgMgmtComponent, ViewProgMgmtComponent],
            imports: [HttpClientModule, RouterTestingModule],
            providers: [
            {
                provide: Store,
                useValue: {
                    dispatch: jest.fn(),
                    pipe: jest.fn()
                }
            }
            ]
        })
        .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(ShellViewProgMgmtComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

    describe('ngOnInit()', () => {

        it('should dispatch an event resetEditPage action in ngOnit lifecycle', () => {

            const store = TestBed.get(Store);
            const action = actions.resetEditPage();
            const spy = jest.spyOn(store, 'dispatch');

            fixture.detectChanges();
            expect(spy).toHaveBeenCalledWith(action);

        });

    });
});
like image 862
user2024080 Avatar asked Sep 07 '25 15:09

user2024080


1 Answers

Please don't mock your own Store.

Instead use MockStore and/or mock selectors, as it will make your life easier. You can take a look at the implementation to find how you have to create a mock store, if this is what you want.

For more info about testing projects that use NgRx, see Testing an NgRx project.

like image 143
timdeschryver Avatar answered Sep 09 '25 04:09

timdeschryver