Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop - test2 #20

Merged
merged 2 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/lib/firebase/__mocks__/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ export const createUserWithEmailAndPassword = jest.fn(() => Promise.resolve({}))
// mockeando el envio de un email de verificación
export const sendEmailVerification = jest.fn(() => Promise.resolve({}));

// mockeando la base de datos
export const db = {};

// mockeando inicio de sesión
export const signInWithEmailAndPassword = jest.fn(() => Promise.resolve({}));

// mockeando cerrar sesión
export const signOut = jest.fn(() => Promise.resolve({}));

// mockeando la inicio de sesión con google
export const signInWithPopup = jest.fn((_auth_, provider) => Promise.resolve({ provider }));

export const addDoc = jest.fn((documents, values) => (values));
export const colleccion = jest.fn();
// mockeando función de restablecimiento de contraseña
// mockeando la base de datos
export const db = jest.fn();

export const sendPasswordResetEmail = jest.fn(() => Promise.resolve());

// mockeando la funciones de firestore
// mokendo collection
export const collection = jest.fn((_db_, _collection_) => _collection_);

// mockeando addDoc
export const addDoc = jest.fn((Collection, data) => Promise.resolve({ [Collection]: data }));
3 changes: 2 additions & 1 deletion src/lib/firebase/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
query,
orderBy,
arrayUnion,
// arrayRemove,
arrayRemove,

} from 'https://www.gstatic.com/firebasejs/9.6.1/firebase-firestore.js';

Expand Down Expand Up @@ -118,6 +118,7 @@ export {
query,
orderBy,
arrayUnion,
arrayRemove,
// arrayRemove,
};

Expand Down
7 changes: 5 additions & 2 deletions src/lib/firebase/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
where,
query,
arrayUnion,
// arrayRemove,
arrayRemove,

// orderBy,
} from './config.js';
Expand Down Expand Up @@ -127,7 +127,9 @@ const setLikes = async (idDocPost, idUserLike) => updateDoc(doc(db, 'posts', idD
likePost: arrayUnion(idUserLike),
});


const removeLikes = async (idDocPost, idUserLike) => updateDoc(doc(db, 'posts', idDocPost), {
likePost: arrayRemove(idUserLike),
});
export {
saveUser,
saveUserProfile,
Expand All @@ -142,5 +144,6 @@ export {
// usuario
// getUsers,
setLikes,
removeLikes,
getDataPost,
};
4 changes: 2 additions & 2 deletions src/view/news.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable import/named */
/* eslint-disable no-console */
import { viewHeader } from './header.js';
Expand Down Expand Up @@ -150,13 +151,12 @@ window.addEventListener('DOMContentLoaded', async () => {

if (arraylike.indexOf(uidUser) !== -1) {
// iconLike.style.color = 'blue';
setLikes(result.id, uidUser);
setLikes(result.id, uidUser).FieldValue;
console.log(arraylike);
} else {
iconLike.style.color = 'black';
}
});

});
});

Expand Down
80 changes: 59 additions & 21 deletions test/auth.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
// Importamos las funciones de firebase
import {
createUserWithEmailAndPassword,
Expand All @@ -6,14 +7,23 @@ import {
signOut,
sendEmailVerification,
signInWithPopup,
sendPasswordResetEmail,
} from '../src/lib/firebase/config';

// Importamos las funciones que vamos a testear
import { createUser, loginUser, userStateChange } from '../src/lib/firebase/auth';
// import { saveUser } from '../src/lib/firebase/firestore.js';
jest.mock('../src/lib/firebase/config.js');
import {
createUser,
loginUser,
userStateChange,
logOutUser,
signInGoogle,
resetPassword,
emailVerification,
} from '../src/lib/firebase/auth';

// Llamamos a la función que mockea las funciones de firebase
jest.mock('../src/lib/firebase/config.js');

// Testeando el registro de un usuario con firebase - auth
describe('probar la función createUser', () => {
it('Deberia ser una funcion', () => {
Expand All @@ -24,53 +34,81 @@ describe('probar la función createUser', () => {
expect(createUserWithEmailAndPassword.mock.calls[0][2]).toBe('12355687');
}));
});

// testeando el inicio de sesión del usuario
describe('probar la función loginUser', () => {
it('Deberia ser una funcion', () => {
expect(typeof loginUser).toBe('function');
});
it('Debería permitir que el usuario entre a la app', () => {
loginUser('marita@gmail.com', '12355687').then(() => {
expect(signInWithEmailAndPassword.mock.calls[0][1]).toBe('marita@gmail.com');
expect(signInWithEmailAndPassword.mock.calls[0][2]).toBe('12355687');
});
});
});

// testeando el estado del usuario
describe('probar la función userStateChange', () => {
it('Deberia ser una funcion', () => {
expect(typeof userStateChange).toBe('function');
});
it('Debería permitir obtener los datos del usuario', () => {
const user = () => {};
userStateChange(user).then(() => {
expect(onAuthStateChanged.mock.calls[0][1]).toBe(user);
});
});
});

// testeando la función para cerrar sesión
describe('probar la función signOut', () => {
it('Debería salir de sesión', () => {
signOut().then(() => {
expect(signOut.mock.calls[0][1]).toBe();
it('Deberia ser una funcion', () => {
expect(typeof logOutUser).toBe('function');
});
it('Debería salir de sesión', (done) => {
logOutUser().then(() => {
expect(signOut.mock.calls[0][1]).toBe(undefined);
});
done();
});
});

// testeando el envio de un link al correo para que el usuario pueda iniciar sesión
describe('probar la función sendEmailVerification', () => {
it('Debería enviar un email de verificación al usuario', () => {
it('Deberia ser una funcion', () => {
expect(typeof emailVerification).toBe('function');
});
it('Debería enviar un email de verificación al usuario', (done) => {
sendEmailVerification().then(() => {
expect(sendEmailVerification.mock.calls[0][1]).toBe();
expect(sendEmailVerification.mock.calls[0][1]).toBe(undefined);
});
done();
});
});

// testeando el inicio de sesión con google
describe('probar la función signInWithPopup', () => {
const result = { correo: 'kaly@gmail.com', id: 123456 };
it('Deberia ser una funcion', () => {
expect(typeof signInGoogle).toBe('function');
});
it('Debería abrir el popup de google', () => {
signInWithPopup().then(() => {
expect(signInWithPopup.mock.calls[0][1]).toBe(result);
signInWithPopup().then((result) => {
expect(signInWithPopup.mock.calls[0][1]).toBe(result.user);
});
});
});
/* describe('probar la función createUser', () => {
it('Debería retornar al usuario creado', async () => {
const result = await saveUser('karenberrio@gmail.com', '123456',
'PsCjKz4DgTMRVTy6POdDYzmL2bD3', 'karen');
expect(result).toEqual({
email: 'karenberrio@gmail.com',
password: '123456',
uid: 'PsCjKz4DgTMRVTy6POdDYzmL2bD3',
user: 'karen',

// testeando una funcion que envia un link al correo para que restablezca su contraseña
describe('probar la funcion de restablecimiento de contraseña', () => {
it('Deberia ser una funcion', () => {
expect(typeof resetPassword).toBe('function');
});
it('Envia un link al correo electronico para restablecer su contraseña', (done) => {
const res = sendPasswordResetEmail('alcantarakaly@gmail.com');
res.then(() => {
expect(sendPasswordResetEmail.mock.calls[0][1]).toBe(undefined);
});
done();
});
}); */
});
23 changes: 23 additions & 0 deletions test/firestore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Importamos las funciones de firestore
// import { addDoc, collection, db } from '../src/lib/firebase/config';

// Importamos las funciones que vamos a testear
// import { saveUser } from '../src/lib/firebase/firestore.js';

// Llamamos a la función que mockea las funciones de firebase
jest.mock('../src/lib/firebase/config.js');

// Testeando el registro de un usuario con firebase - auth
// describe('probar la función saveUser', () => {
// it('Deberia subir data a la coleccion saveUsers', () => saveUser('karenberrio@gmail.com', '123456', 'karen', 'PsCjKz4DgTMRVTy6POdDYzmL2bD3'));

// it('Debería retornar al usuario creado', async () => {
// const result = await addDoc(collection(db, 'saveUsers'));
// expect(result).toEqual({
// email: 'karenberrio@gmail.com',
// password: '123456',
// uid: 'PsCjKz4DgTMRVTy6POdDYzmL2bD3',
// user: 'karen',
// });
// });
// });