Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda