Skip to content

Commit

Permalink
Merge pull request #4 from iLynette/testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
iLynette committed Feb 24, 2022
2 parents ea1fcce + 2ac341e commit 703ac9c
Show file tree
Hide file tree
Showing 8 changed files with 1,651 additions and 128 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"rules": {
"no-shadow": "off",
"no-use-before-define": "off",
"no-unused-vars": "off",
"no-param-reassign": "off",
"eol-last": "off",
"import/extensions": [ 1, {
Expand Down
1,684 changes: 1,566 additions & 118 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "![](https://img.shields.io/badge/Microverse-blueviolet)",
"private": "true",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest --env=jsdom",
"build": "webpack",
"start": "webpack serve --open"
},
Expand All @@ -20,13 +20,15 @@
},
"homepage": "https://github.com/iLynette/todo-list#readme",
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
"babel-eslint": "^10.1.0",
"css-loader": "^6.6.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.4",
"hint": "^6.1.9",
"html-webpack-plugin": "^5.5.0",
"jest": "^27.5.1",
"style-loader": "^3.3.1",
"stylelint": "^13.13.1",
"stylelint-config-standard": "^21.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash'; //eslint-disable-line
import './style.css';
import gin from './modules/utils.js';
import * as gin from './modules/utils.js';

gin();
gin.genesis();
1 change: 1 addition & 0 deletions src/modules/todoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class TodoList {
this.todos = this.todos.filter((t) => t.id !== id);
this.sort();
this.save();
return id;
};

sort= () => {
Expand Down
15 changes: 8 additions & 7 deletions src/modules/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import TodoList from './todoList.js';

const todo = new TodoList();
let todo = new TodoList();
const todoList = document.getElementById('dynamic-list');
const form = document.getElementById('form');
const allTasks = () => {
todoList.innerHTML = '';
export const allTasks = (T) => {
if (T !== null && T !== undefined) {
todo = T;
}
document.getElementById('dynamic-list').innerHTML = '';
todo.todos
.sort((a, b) => a.index - b.index)
.map((todos) => {
Expand All @@ -30,7 +33,7 @@ const allTasks = () => {
icon.classList.add('fas', 'fa-ellipsis-v');
button.appendChild(icon);
todoCard.appendChild(button);
todoList.appendChild(todoCard);
document.getElementById('dynamic-list').appendChild(todoCard);

return todoCard;
});
Expand Down Expand Up @@ -93,7 +96,7 @@ const addNewToDo = () => {
allTasks();
}
};
const genesis = () => {
export const genesis = () => {
checkDb();
form.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
Expand All @@ -105,5 +108,3 @@ const genesis = () => {
allTasks();
};
};

export default genesis;
63 changes: 63 additions & 0 deletions todoList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import TodoList from './src/modules/todoList.js';
import * as gin from './src/modules/utils.js';

class LocalStorageMock {
constructor() {
this.store = {};
}

clear() {
this.store = {};
}

getItem(key) {
return this.store[key] || null;
}

setItem(key, value) {
this.store[key] = String(value);
}

removeItem(key) {
delete this.store[key];
}
}

global.localStorage = new LocalStorageMock();

document.body.innerHTML = `<div> +
<div id="dynamic-list"></div>
</div>`;
const todoList = new TodoList();

describe('Add task', () => {
test('Add one task expect tasks length to be 1', () => {
todoList.addTodo('go shopping');
todoList.addTodo('watch a movie');
expect(todoList.todos.length).toBe(2);
});
});

describe('Add the added todo to the DOM', () => {
test('One task has been added expect one task element in DOM', () => {
gin.allTasks(todoList);
const tasksList = document.getElementById('dynamic-list');
expect(tasksList.childNodes.length).toBe(2);
});
});

describe('Remove first todo from todoList', () => {
test('One task has been remove expect zero', () => {
const { id } = todoList.todos[0];
const taskCard = document.getElementById(id);
taskCard.parentNode.removeChild(taskCard);
expect(todoList.delete(id)).toBe(id);
});
});

describe('Check if task card had been removed from DOM', () => {
test('One card removed expect node children to be 0', () => {
const tasksList = document.getElementById('dynamic-list');
expect(tasksList.childNodes.length).toBe(1);
});
});

0 comments on commit 703ac9c

Please sign in to comment.