Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to pass the store in the constructor in a typescript class?

I have this class sample that is using ngrx store an other services in the constructor, What is the best way to pass the store in the constructor when I am testing this class in jest?

This class is not a component or injectable.

export class Sample {
   private users;

   constructor(
      private readonly userService: UserService,
      private readonly store: Store
   ) { }

   public setUsersDataFromStore() {
       this.store.select(selectUsers).pipe(
           filter((user) => !!users),
           tap((users => (this.users = users)))
       ).subscribe();
   }
}

Unit test.

fit('should Sample Object defined', () => {
    const sampleObj = new Sample(
        mockUserService any,
        initialState as any
    );

    sampleObj.setUsersDataFromStore();

    expect(sampleObj.users).toBeDefined();
});

But the unit test is failing the selector is not getting the data from the initial state from the constructor, the initial state is a simple object, also I tried with the object in an observable of({initialState}), what is the best option to fix?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use getMockStore to create a mocked instance of the store, I briefly touch on it on this subject in my blog post Testing an NgRx project.

new Sample(
 mockUserService,
  getMockStore({
      initialState,
  });
)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!