测试Angular Effect + graphql apollo客户端

时间:2019-02-06 14:56:15

标签: angular testing jasmine ngrx apollo

我正在尝试测试一种效果,但我不知道如何监视watch方法的valueChanges属性

@Effect()
signup$: Observable<Action> = this.actions$.pipe(
    ofType(AuthActionTypes.SIGNUP),
    map((action: Signup) => action.payload),
    switchMap((user: any) => {
        this.userSignup = user;
        return this.createUserGQL.mutate({ userInput: user });
    }),
    switchMap(() => {
        return this.signInGQL.watch({
            email: this.userSignup.email,
            password: this.userSignup.password
        }).valueChanges.pipe(map((login: any) => {
            const authData = login.data.login;
            return authData;
        }));
    }),
    mergeMap((res: any) => {
        const token = res.token;
        this.localStorageService.setItem(AUTH_KEY, { token: token, isAuthenticated: true });
        return [{ type: AuthActionTypes.SIGNUP_SUCCESS }, { type: AuthActionTypes.SET_TOKEN, payload: token }];
    }),
    catchError(err => {
        return of(new RetrieveError({ error: err }));
    })
);

在我的规格文件中,我有这个

describe('AuthEffects', () => {
let localStorageService: jasmine.SpyObj<LocalStorageService>;
let router: jasmine.SpyObj<Router>;
let createUserGQL: jasmine.SpyObj<CreateUserGQL>;
let signInGQL: jasmine.SpyObj<SignInGQL>;

beforeEach(() => {
    localStorageService = jasmine.createSpyObj('LocalStorageService', [
        'setItem'
    ]);
    router = jasmine.createSpyObj('Router', ['navigateByUrl']);
    createUserGQL = jasmine.createSpyObj('CreateUserGQL', ['mutate']);
    signInGQL = jasmine.createSpyObj('SignInGQL', ['watch']);
});

describe('signup', () => {
    it('should emit SignupSuccess and SetToken on success', () => {
        const userData: UserInputData = { name: 'test', email: 'test@test.com', password: 'secretsecret' };
        const tokenData = 'tokensecretsecret';
        const mutationCreateUser = {
            id: '5c50364149012d0c8cf0fb37',
            name: 'test',
            email: 'test@test.com'
        };
        const watchLogin = {
            token: '1234566787',
            userId: '5c50364149012d0c8cf0fb37'
        };
        const signupAction = new Signup(userData);
        const expectedAction = [{ type: AuthActionTypes.SIGNUP_SUCCESS }, { type: AuthActionTypes.SET_TOKEN, payload: tokenData }];
        const expectedValues = {
            b: expectedAction
        };
        const source = cold('a', { a: signupAction });
        const expected = cold('b', expectedValues);
        const actions = new Actions(source);
        createUserGQL.mutate.and.returnValue(of(mutationCreateUser));
        signInGQL.watch.and.returnValue(of(watchLogin));
        const effect = new AuthEffects(actions, router, localStorageService, createUserGQL, signInGQL);
        expect(effect.signup$).toBeObservable(expected);

    });
});
});

但是关于未定义.pipe的错误,因为我没有valueChanges的returnValue。

工作测试。需要传递一个属性名称为value的对象,而不是直接传递该值。

@Effect()
signup$: Observable<Action> = this.actions$.pipe(
    ofType(AuthActionTypes.SIGNUP),
    map((action: Signup) => action.payload),
    switchMap((user: any) => {
        this.userSignup = user;
        return this.createUserGQL.mutate({ userInput: user });
    }),
    switchMap(() => {
        return this.signInGQL.watch({
            email: this.userSignup.email,
            password: this.userSignup.password
        }).valueChanges.pipe(map((login: any) => {
            const authData = login.data.login;
            return authData;
        }));
    }),
    mergeMap((res: any) => {
        const token = res.token;
        this.localStorageService.setItem(AUTH_KEY, { token: token, isAuthenticated: true });
        return [new SignupSuccess, new SetToken(token)];
    }),
    catchError(err => {
        return of(new RetrieveError({ error: err }));
    })
);



 describe('signup', () => {
    it('should emit SignupSuccess and SetToken on success', () => {
        const userData: UserInputData = { name: 'test', email: 'test@test.com', password: 'secretsecret' };
        const tokenData = 'tokensecretsecret';
        const mutationCreateUser = {
            id: '5c50364149012d0c8cf0fb37',
            name: 'test',
            email: 'test@test.com'
        };
        const watchLogin = {
            data: {
                login: {
                    token: '1234566787',
                    userId: '5c50364149012d0c8cf0fb37'
                }
            }
        };
        const signupAction = new Signup(userData);
        const expectedValues = {
            b: new SignupSuccess,
            c: new SetToken(watchLogin.data.login.token)
        };
        const source = cold('a', { a: signupAction });
        const expected = cold('(bc)', expectedValues);
        const actions = new Actions(source);
        createUserGQL.mutate.and.returnValue(of(mutationCreateUser));
        signInGQL.watch.and.returnValue({
            valueChanges: of(watchLogin)
        });
        const effect = new AuthEffects(actions, router, localStorageService, createUserGQL, signInGQL);
        expect(effect.signup$).toBeObservable(expected);
    });
});

1 个答案:

答案 0 :(得分:0)

试图帮助...
可能是缺少“ valueChanges”属性的问题?

const signInGQL = jasmine.createSpyObj('SignInGQL', [ 'watch' ]);
signInGQL.watch.and.returnValue({
  valueChanges: of(watchLogin)  // instead of "of(watchLogin)"
});