Skip to content

Commit

Permalink
Creación de test unitarios historia 1 70% Stmts
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Aug 18, 2020
1 parent fbd0ad4 commit 506229f
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 14 deletions.
42 changes: 42 additions & 0 deletions _mocks_/mock_auth.js
Original file line number Diff line number Diff line change
@@ -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;
})
12 changes: 9 additions & 3 deletions src/lib/firebaseAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
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;
}
}

// login
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;
Expand Down Expand Up @@ -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');
}
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 3 additions & 2 deletions src/views/createAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion src/views/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default () => {
home.addEventListener('click', () => {
window.location = '#login';
});

return home;
};

Expand Down
4 changes: 2 additions & 2 deletions src/views/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default () => {

const login = document.createElement('section');
login.setAttribute('id', 'login');
login.innerHTML = `<img src="img/icon.png" alt="logo_image" class="logo"/>`;
login.innerHTML = '<img src="img/icon.png" alt="logo_image" class="logo"/>';

const input = document.createElement('form');
input.setAttribute('class', 'input-container');
Expand Down Expand Up @@ -37,6 +37,6 @@ export default () => {
const provider = new firebase.auth.GoogleAuthProvider();
logInGoogle(provider);
});

return signin;
};
2 changes: 1 addition & 1 deletion src/views/recover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 57 additions & 4 deletions test/firebaseAuth.spec.js
Original file line number Diff line number Diff line change
@@ -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 protected] , contraseña1234', async () => {
const newUser = await signUp('[email protected]', 'contraseña1234');
expect(newUser).toBe('nuevo usuario [email protected], 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 protected]', '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 protected] , contraseña1234', async () => {
const user = await logIn('[email protected]', 'contraseña1234');
console.log(user);
expect(user).toBe('usuario [email protected], 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 protected] para cambiar la contraseña', async () => {
const message = document.createElement('p');
const recover = await recoverPass(message, '[email protected]');
expect(recover).toBe('Hemos enviado un email a [email protected] 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 protected] para cambiar la contraseña', async () => {
const outUser = await signOut();
expect(outUser).toBe(undefined);
});
});

0 comments on commit 506229f

Please sign in to comment.