首页 > 解决方案 > 如何测试 Firebase signInWithEmailAndPassword?

问题描述

我想测试我的操作,看看是否调用了 dispatch 和 history spy。但它每次都会在 catch ( dispatch GET_ERRORS ) 中结束。

它给了我这个错误:

code: 'auth/network-request-failed',
message:  'A network error (such as timeout, interrupted connection or unreachable host) has occurred.'

行动:

export const startLogin = ( { email, password } ) => {
    return ( dispatch ) => {
        dispatch( setAuthLoading() );

        return firebase
            .auth()
            .signInWithEmailAndPassword( email, password )
            .then( ( user ) => {  
                const userData = {
                    uid: user.uid,
                    name: user.displayName,
                    email: user.email
                }

                // This is what I want to test. If dispatch, history was called
                dispatch( setCurrentUser( userData ) );
                history.push( '/' );
            })
            .catch( ( err ) => {
                // It ends up everytime here, why?
                dispatch({
                    type: GET_ERRORS,
                    payload: err 
                });

                dispatch( clearAuthLoading() );
            });
    };
};

测试:

test( 'should start login with correct data', ( done ) => {
    const pushSpy = jest.spyOn( history, 'push' );
    const store = createMockStore({});
    const name = 'Josh';
    const loginData = {
        email: 'test@gmail.com',
        password: '123456password'
    };  

    store.dispatch( startLogin( loginData ) ).then(() => {
        const actions = store.getActions();

        expect( actions[0] ).toEqual({
            type: SET_CURRENT_USER,
            user: {
                name,
                email: loginData,
                psasword: loginData.password
            }
        });

        expect( pushSpy ).toHaveBeenLastCalledWith( '/' );

        done();
    });
});

可能是firebase安全问题吗?我尝试了一切,但最终还是被抓住了。

谢谢

标签: reactjsfirebasereduxjestjsredux-thunk

解决方案


推荐阅读