Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda