How to mock store state in angular 16 using ngxs

69 Views Asked by At

We introduce store in our ongoing project using ngxs I'm unable to mock state that have same service dependencies. Any help would be appreciate.

I have setup one simple example to understand:

// State of the store
export class TutorialState {

    constructor() {}

    @Selector()
    static getTutorials(state: TutorialStateModel) {
        return state.tutorials;
    }

    @Action(AddTutorial) 
    add({ getState, patchState }: StateContext<TutorialStateModel>, { payload }: AddTutorial) {
        const state = getState();
        patchState({
            tutorials: [...state.tutorials, payload]
        })
    }

    @Action(RemoveTutorial) 
    remove({ getState, patchState }: StateContext<TutorialStateModel>, { payload }: RemoveTutorial) {
        patchState({
            tutorials: getState().tutorials.filter(a => a.name != payload)
        })
    }
    
}



// spec file of the component
class MockAuthenticationService {
  userSubject = new BehaviorSubject({});
  public get userValue() {
    return this.userSubject?.value||null;
  }
}
fdescribe('ReadComponent', () => {
  let component: ReadComponent;
  let fixture: ComponentFixture<ReadComponent>;
  let store: Store;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [],
      imports: [ReadComponent, NgxsModule.forRoot([TutorialState])],
      providers: [
        { provide: AuthenticationService, useClass: MockAuthenticationService }
      ]
    });

    store = TestBed.inject(Store);
    TestBed.inject(AuthenticationService)
    fixture = TestBed.createComponent(ReadComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

Currently getting this error: Can't resolve all parameters for TutorialState: (?).

Currently, I try to follow NGXS documentation but I didn't any example for complete store mocking or dependencies in the state: Share any link will be much appreciated.

0

There are 0 best solutions below