جافاسكريبت - كيفية اختبار إجراء تسجيل الدخول في Firebase (React/Jest)
أحاول إنشاء اختبار سيتحقق مما إذا كان قد تم استدعاء signIn، ثم يستمر إلى اختبار دالتي success وerror.
أنا أستخدم حزمة firebase-mock هنا: https://github.com/soumak77/firebase-mock/blob/master/tutorials/auth/authentication.md
فيما يلي إجراء تسجيل الدخول الخاص بي
// 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: '[email ',
password: 'abd123'
};
signIn(user.email, user.password);
console.log('signIn', signIn);
expect(signIn).toHaveBeenCalled();
});
});
رسالة خطأ
فشل client/actions/Login/index.test.js ● إجراءات تسجيل الدخول › يجب استدعاء signIn
expect(jest.fn())[.not].toHaveBeenCalled()
يجب أن تكون قيمة jest.fn() دالة محاكاة أو تجسس. تم الاستلام: دالة: [Function signIn]
at Object.<anonymous> (client/actions/Login/index.test.js:71:29)
الإجابة رقم 1
كنت أسخر بشكل غير صحيح من دالة خدمات Firebase، أدناه هو الكود الذي نجح معي، ومع ذلك أواجه مشكلة جديدة منشورة هنا: كيف يمكن اختبار ما إذا كان الكود داخل thenable في اختبار Jest يتم استدعاؤه؟
يجتاز الاختبار التالي، ومع ذلك لست متأكدًا من أن الكود داخل store.dispatch قابل للاستخدام مع then...
// 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: '[email ',
password: 'abd123'
};
store.dispatch(signIn(user.email, user.password)).then(() => {
expect(mockFirebaseService).toHaveBeenCalled();
});
});
});
