From 506229f2e201ffd70e3145e15b94d9faf5a00fa0 Mon Sep 17 00:00:00 2001 From: dcoa Date: Mon, 17 Aug 2020 20:59:23 -0500 Subject: [PATCH] =?UTF-8?q?Creaci=C3=B3n=20de=20test=20unitarios=20histori?= =?UTF-8?q?a=201=2070%=20Stmts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _mocks_/mock_auth.js | 42 ++++++++++++++++++++++++++ src/lib/firebaseAuth.js | 12 ++++++-- src/main.js | 2 +- src/views/createAccount.js | 5 ++-- src/views/home.js | 2 +- src/views/login.js | 4 +-- src/views/recover.js | 2 +- test/firebaseAuth.spec.js | 61 +++++++++++++++++++++++++++++++++++--- 8 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 _mocks_/mock_auth.js diff --git a/_mocks_/mock_auth.js b/_mocks_/mock_auth.js new file mode 100644 index 00000000..c0ea264f --- /dev/null +++ b/_mocks_/mock_auth.js @@ -0,0 +1,42 @@ +const auth = { + createUserWithEmailAndPassword: (email, password) => { + const emailChar = /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/; + const passChar = /^.{6,}$/; + return new Promise ((resolve, reject) => { + const user = { + email: email, + password: password + }; + if (emailChar.test(email) && passChar.test(password)) { + user.email; + user.password; + } else { + reject('error'); + }; + + resolve(`nuevo usuario ${user.email}, ${user.password}`); + }) + }, + signInWithEmailAndPassword: (email, password) => { + return new Promise ((resolve, reject) => { + resolve(`usuario ${email}, ${password}`); + reject('error') + }) + }, + sendPasswordResetEmail: (email) => { + return new Promise ((resolve, reject) => { + resolve(email); + reject('error'); + }) + }, + signOut: () => { + return new Promise ((resolve, reject) => { + resolve(); + reject('error'); + }) + } +} + +export default jest.fn(() => { + return auth; +}) diff --git a/src/lib/firebaseAuth.js b/src/lib/firebaseAuth.js index 832f9dce..32017d21 100644 --- a/src/lib/firebaseAuth.js +++ b/src/lib/firebaseAuth.js @@ -2,13 +2,17 @@ export async function signUp(email, password) { try { const newUser = await auth.createUserWithEmailAndPassword(email, password); + window.location.hash = "#thankAccount"; console.log(newUser.user); - window.location = '#thankAccount'; + return newUser; + + } catch (error) { // Handle Errors here. const errorCode = error.code; const errorMessage = error.message; console.log(errorCode, errorMessage); + return error; } } @@ -16,6 +20,7 @@ export async function signUp(email, password) { export async function logIn(email, password) { try { const userLogIn = await auth.signInWithEmailAndPassword(email, password); + return userLogIn; } catch (error) { // Handle Errors here. const errorCode = error.code; @@ -48,9 +53,9 @@ export async function logInGoogle(provider) { export async function recoverPass(message, email) { try { const sendEmail = await auth.sendPasswordResetEmail(email); - message.innerHTML = `Hemos enviado un email a ${email} para cambiar la contraseña`; + return message.innerHTML = `Hemos enviado un email a ${email} para cambiar la contraseña`; } catch (error) { - message.innerHTML = 'No se ha podido enviar el correo de verificación'; + return message.innerHTML = 'No se ha podido enviar el correo de verificación'; console.log('No se ha podido enviar el correo de verificación'); } } @@ -59,6 +64,7 @@ export async function recoverPass(message, email) { export async function signOut() { try { const userOut = await auth.signOut(); + return userOut; console.log('user out'); } catch (e) { console.log(e); diff --git a/src/main.js b/src/main.js index 1e230f0b..f8388822 100644 --- a/src/main.js +++ b/src/main.js @@ -9,7 +9,7 @@ import otherThank from './views/thankAccount.js'; const body = document.getElementById('root'); const header = document.getElementById('header'); -auth.onAuthStateChanged(function (user) { +auth.onAuthStateChanged((user) => { if (user) { header.style.display = 'block'; console.log('esta dentro'); diff --git a/src/views/createAccount.js b/src/views/createAccount.js index dcc9041c..eb17bc81 100644 --- a/src/views/createAccount.js +++ b/src/views/createAccount.js @@ -33,9 +33,10 @@ export default () => { const email = form.correo.value; const password = form.contraseña.value; const birthday = form.birthday.value; - console.log(nameUser, email, password, birthday); signUp(email, password); + window.location.hash = '#thankAccount'; + }); - + return createAccount; }; diff --git a/src/views/home.js b/src/views/home.js index 9db5a247..1288c72f 100644 --- a/src/views/home.js +++ b/src/views/home.js @@ -7,7 +7,7 @@ export default () => { home.addEventListener('click', () => { window.location = '#login'; }); - + return home; }; diff --git a/src/views/login.js b/src/views/login.js index a8d65ab4..8d640293 100644 --- a/src/views/login.js +++ b/src/views/login.js @@ -6,7 +6,7 @@ export default () => { const login = document.createElement('section'); login.setAttribute('id', 'login'); - login.innerHTML = ``; + login.innerHTML = ''; const input = document.createElement('form'); input.setAttribute('class', 'input-container'); @@ -37,6 +37,6 @@ export default () => { const provider = new firebase.auth.GoogleAuthProvider(); logInGoogle(provider); }); - + return signin; }; diff --git a/src/views/recover.js b/src/views/recover.js index a10cb02c..ebefe988 100644 --- a/src/views/recover.js +++ b/src/views/recover.js @@ -15,7 +15,7 @@ export default () => { const send = recover.querySelector('.send').addEventListener('click', () => { const email = recover.querySelector('#email').value; - let message = recover.querySelector('.message'); + const message = recover.querySelector('.message'); message.style.display = 'block'; console.log(email); recoverPass(message, email); diff --git a/test/firebaseAuth.spec.js b/test/firebaseAuth.spec.js index 071d9998..7e620508 100644 --- a/test/firebaseAuth.spec.js +++ b/test/firebaseAuth.spec.js @@ -1,8 +1,61 @@ -// importamos la funcion que vamos a testear -import { signUp, logIn, logInGoogle, recoverPass, signOut } from "../src/lib/index"; +// importamos la funciones firebase.auth que vamos a testear +import authmock from '../_mocks_/mock_auth.js'; -describe('myFunction', () => { +import { + signUp, logIn, logInGoogle, recoverPass, signOut, +} from '../src/lib/firebaseAuth.js'; + +global.auth = authmock(); + +describe('signUp', () => { + it('debería ser una función', () => { + expect(typeof signUp).toBe('function'); + }); + + it('debería retornar email@email.com , contraseña1234', async () => { + const newUser = await signUp('email@email.com', 'contraseña1234'); + expect(newUser).toBe('nuevo usuario email@email.com, contraseña1234'); + }); + + it('debería retornar error;', async () => { + const newUser = await signUp('email@emailcom', 'contraseña123'); + expect(newUser).toBe('error'); + }); + + it('debería retornar error;', async () => { + const newUser = await signUp('email@email.com', 'abc'); + expect(newUser).toBe('error'); + }); +}); + +describe('logIn', () => { it('debería ser una función', () => { - expect(typeof myFunction).toBe('function'); + expect(typeof logIn).toBe('function'); + }); + it('debería retornar email@email.com , contraseña1234', async () => { + const user = await logIn('email@email.com', 'contraseña1234'); + console.log(user); + expect(user).toBe('usuario email@email.com, contraseña1234'); + }); +}); + +describe('recoverPass', () => { + it('debería ser una función', () => { + expect(typeof recoverPass).toBe('function'); + }); + it('debería retornar Hemos enviado un email a email@email.com para cambiar la contraseña', async () => { + const message = document.createElement('p'); + const recover = await recoverPass(message, 'email@email.com'); + expect(recover).toBe('Hemos enviado un email a email@email.com para cambiar la contraseña'); + }); +}); + +describe('signOut', () => { + it('debería ser una función', () => { + expect(typeof signOut).toBe('function'); + }); + it('debería retornar Hemos enviado un email a email@email.com para cambiar la contraseña', async () => { + const outUser = await signOut(); + expect(outUser).toBe(undefined); }); });