Skip to content

Commit

Permalink
controlador de todos + sus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
merunga committed Apr 25, 2019
1 parent 447992d commit 4aaba51
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/controller/__mocks__/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let count = 0;

export const uuid = () => {
count += 1;
return count;
};
26 changes: 26 additions & 0 deletions src/controller/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { uuid } from './utils.js';

/**
* Inserta un nuevo todo al final del original
* @param {*} todos Array en donde insertar el nuevo todo
* @param {*} label Label del nuevo todo
*/
export const todoInsertar = (todos, label) => {
const todo = {
id: uuid(),
label,
};
return [
...todos,
todo,
];
};

/**
* Elimina un todo del array basado en el id
* @param {*} todos Array de donde quitar el todo
* @param {*} id Id del todo a eliminar
*/
export const todoEliminar = (todos, id) => (
todos.filter(t => t.id !== id)
);
4 changes: 4 additions & 0 deletions src/controller/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Crea un id unico
*/
export const uuid = () => Date.now();
28 changes: 28 additions & 0 deletions test/controller/todo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { todoInsertar, todoEliminar } from '../../src/controller/todo';

jest.mock('../../src/controller/utils');

describe('todo controller', () => {
it('agrega al final', () => {
const todos = [{ id: 0, label: 'existente' }];
const label = 'nuevo';
const newTodos = todoInsertar(todos, label);

expect(newTodos).toEqual([
{ id: 0, label: 'existente' },
{ id: 1, label: 'nuevo' },
]);
});
it('elimina', () => {
const todos = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
];
const newTodos = todoEliminar(todos, 2);
expect(newTodos).toEqual([
{ id: 1 },
{ id: 3 },
]);
});
});
12 changes: 12 additions & 0 deletions test/controller/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable no-extend-native */
import { uuid } from '../../src/controller/utils';

describe('utils', () => {
it('uuid devuelve timestamp tomado desde `Date.now`', () => {
const now = 1556232924512;
// mockeamos `Date.now`
Date.now = jest.fn().mockReturnValue(now);

expect(uuid()).toBe(now);
});
});

0 comments on commit 4aaba51

Please # to comment.