我们可以在Angular中使用Jasmine / Karma测试ngrx流吗?

时间:2018-07-18 05:04:38

标签: javascript angular unit-testing karma-jasmine ngrx

我已将ngrx用于我的角度应用程序。现在,我正在尝试使用Jasmine和业力工具进行单元测试,但是我找不到合适的方法来测试ngrx流。

请帮助我提供任何方法,否则来源也很好,谢谢。

请找到我要测试的以下组件。

login-form.component.ts


import { Component, OnInit, AfterViewInit, AfterViewChecked, AfterContentInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { ModalService } from '../../../@carbon/modal';
import { LoginResponse } from '../../../@core/login/login.contract';
import { LoginService } from '../../../@core/login/login.service';
import { SessionStorageService } from '../../../@core/session-storage/session-storage.service';
import { LocalStorageService } from '../../../@core/local-storage/local-storage.service';
import { Store } from '@ngrx/store';
import { AppState } from '../../../@models/app-state';
import { ActionSignIn, selectorLogin } from '../../../@core/login/login.reducer';
import { LoginState } from '../../../@core/login/login.state';
import { BusyState } from '../../../@shared/busy-indicator/busy-state';
import {Router} from '@angular/router';


@Component({
  selector: 'eose-login-form',
  templateUrl: './login-form.component.html',
  styleUrls: ['./login-form.component.scss']
})
export class LoginFormComponent implements OnInit {

 // loading: BusyState;
  loading: boolean = false;
  loadingMsg: string = '';
  authForm: FormGroup;
  changeForm: FormGroup
  constructor(
    private store: Store<AppState>,
    private fb: FormBuilder,
    private modalService: ModalService
  ) {
    this.authForm = this.fb.group({
      'env': new FormControl('cdtdevdir'),
      'email': new FormControl('', Validators.required),
      'password': new FormControl('', Validators.required),
      'newpassword': new FormControl(''),
      'confirmpassword': new FormControl('')
    })
  }

  handleLoadingChange(loadingState: BusyState) {
   // this.loading = loadingState;
  }


  ngOnInit() {
    this.store.select(selectorLogin)
    .subscribe((loginState: LoginState) => {
      this.loading = loginState.loading;
      this.loadingMsg = loginState.loadingMsg;
    })
  }

  openModal() {
    this.modalService.open('sign-in');
  }

  onLogin() {
     this.store.select(selectorLogin)
     .subscribe((loginState: LoginState) => {
       this.loading = loginState.loading;
     })
    console.log('Login Payload', this.authForm.value);
   this.store.dispatch(new ActionSignIn(this.authForm.value));
  }
}

1 个答案:

答案 0 :(得分:0)

您可以 spyOn dispatch并检查是否已发送动作

let actionSignIn = new ActionSignIn(your input);
expect(store.dispatch).toHaveBeenCalldWith(actionSignIn);