gpt4 book ai didi

javascript - 如何测试 Firebase 登录操作 (React/Jest)

转载 作者:搜寻专家 更新时间:2023-11-01 04:12:59 24 4
gpt4 key购买 nike

我正在尝试创建一个测试来查看是否已调用 signIn,然后继续进行成功错误 功能测试。

我在这里使用 firebase-mock 包: https://github.com/soumak77/firebase-mock/blob/master/tutorials/auth/authentication.md

下面是我的登录 Action

// Sign in action
export const signIn = (email, password, redirectUrl = ROUTEPATH_DEFAULT_PAGE) => (dispatch) => {
dispatch({ type: USER_LOGIN_PENDING });

firebase
.then(auth => auth.signInWithEmailAndPassword(email, password))
.catch((e) => {
console.error('actions/Login/signIn', e);
// Register a new user
if (e.code === LOGIN_USER_NOT_FOUND) {
dispatch(push(ROUTEPATH_FORBIDDEN));
dispatch(toggleNotification(true, e.message, 'error'));
} else {
dispatch(displayError(true, e.message));
setTimeout(() => {
dispatch(displayError(false, ''));
}, 5000);
throw e;
}
})
.then(res => res.getIdToken())
.then((idToken) => {
if (!idToken) {
dispatch(displayError(true, 'Sorry, there was an issue with getting your token.'));
}

dispatch(onCheckAuth(email));
dispatch(push(redirectUrl));
});
};

我的测试:

import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { MockFirebase } from 'firebase-mock';

// Login Actions
import { onCheckAuth, signIn } from 'actions';

// String Constants
import { LOGIN_USER_NOT_FOUND } from 'copy';

const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);

let mockProps;

describe('login actions', () => {
// console.log('MockFirebase', MockFirebase);
// console.log('onCheckAuth', onCheckAuth);
let mockAuth;

beforeEach(() => {
mockAuth = new MockFirebase();
console.log('mockAuth: ==>', mockAuth);

mockProps = {
signIn: jest.fn(),
signOut: jest.fn(),
checkAuth: jest.fn(),
createUser: jest.fn(),
resetPassword: jest.fn(),
verifyEmail: jest.fn()
};
});

it('signIn should be called', () => {
const user = {
email: 'first.last@yum.com',
password: 'abd123'
};

signIn(user.email, user.password);
console.log('signIn', signIn);

expect(signIn).toHaveBeenCalled();
});
});

错误信息

FAIL client/actions/Login/index.test.js ● login actions › signIn should be called

expect(jest.fn())[.not].toHaveBeenCalled()

jest.fn() value must be a mock function or spy. Received: function: [Function signIn]

at Object.<anonymous> (client/actions/Login/index.test.js:71:29)

enter image description here

最佳答案

我错误地模拟了 firebase 服务功能,下面是我开始工作的代码,但是遇到了一个发布在这里的新问题:How to test is code inside of thenable in jest test is getting called?

以下测试通过,但不确定 store.dispatch 中的代码是否可用...

// Mock all the exports in the module.
function mockFirebaseService() {
return new Promise(resolve => resolve(true));
}

// Since "services/firebase" is a dependency on this file that we are testing,
// we need to mock the child dependency.
jest.mock('services/firebase', () => new Promise(resolve => resolve(true)));

describe('login actions', () => {
let store;

beforeEach(() => {
store = mockStore({});
});

it('signIn should call firebase', () => {
const user = {
email: 'first.last@yum.com',
password: 'abd123'
};

store.dispatch(signIn(user.email, user.password)).then(() => {
expect(mockFirebaseService).toHaveBeenCalled();
});
});
});

关于javascript - 如何测试 Firebase 登录操作 (React/Jest),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com