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

TESTING MINIMALIST 2 #9

Merged
merged 4 commits into from
Mar 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
144 changes: 82 additions & 62 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
const Task = require('./modules/Task.js');
const { saveToLocalStorage } = require('./modules/Storage.js');
const { saveToLocalStorage, getFromLocalStorage } = require('./modules/Storage.js');

describe('updateIndex', () => {
const array = [
{
description: 'Go to work',
index: 1,
completed: false,
},
{
description: 'Go to School',
index: 2,
completed: false,
},
{
description: 'Go to home',
index: 3,
completed: false,
},
];
const task = new Task();
const array = [
{
description: 'Go to work',
index: 1,
completed: false,
},
{
description: 'Go to School',
index: 2,
completed: true,
},
{
description: 'Go to home',
index: 3,
completed: false,
},
];

const testTask = new Task();
beforeEach(() => {
saveToLocalStorage('tasks', array);
});

describe('updateIndex', () => {
test('Test updateIndex', () => {
const result1 = {
description: 'Go to home',
Expand All @@ -35,54 +38,71 @@ describe('updateIndex', () => {
completed: false,
};

expect(testTask.updateIndex(0, array)).toContainEqual(result1);
expect(testTask.updateIndex(1, array)).toContainEqual(result2);
expect(task.updateIndex(0, array)).toContainEqual(result1);
expect(task.updateIndex(1, array)).toContainEqual(result2);
});
});

describe('add', () => {
const mockLocalStorage = (() => {
let store = {};

return {
getItem(key) {
return store[key] || null;
},
setItem(key, value) {
store[key] = value.toString();
},
clear() {
store = {};
},
};
})();
describe('add list to task in localStorage', () => {
test('save to localStorage', () => {
saveToLocalStorage('tasks', array);
expect(JSON.parse(window.localStorage.getItem('tasks'))).toEqual(array);
});
});

const array = [
{
description: 'Go to work',
index: 1,
completed: false,
},
{
description: 'Go to School',
index: 2,
completed: false,
},
{
description: 'Go to home',
index: 3,
completed: false,
},
];
describe('delete task', () => {
const ul = document.createElement('ul');
ul.className = 'list-container';
ul.innerHTML = `
<li> Go to work </li>
<li> Buy groceries </li>
`;

beforeAll(() => {
Object.defineProperty(window, 'localStorage', {
value: mockLocalStorage,
});
test('remove all task from list', () => {
document.body.appendChild(ul);
expect(task.removeChild()).toBe(undefined);
});

test('save to localStorage', () => {
saveToLocalStorage('tasks', array);
expect(JSON.parse(window.localStorage.getItem('tasks'))).toEqual(array);
const result = {
description: 'Go to work',
index: 1,
completed: false,
};

test('remove completed task from list', () => {
const testTask = new Task();
expect(testTask.removeCompleted()).toContainEqual(result);
});
});

describe('update', () => {
test('update task description of second list', () => {
const updated = task.update('Take a break', 1);
const tasks = getFromLocalStorage('tasks');

expect(tasks[1]).toEqual(updated);
});

test('update task description of first list', () => {
const updated = task.update('Walk the dog', 0);
const tasks = getFromLocalStorage('tasks');

expect(tasks[0]).toEqual(updated);
});
});

describe('update completed status', () => {
test('check completed status of first list to true', () => {
const status = task.checked(true, 1);
const tasks = getFromLocalStorage('tasks');

expect(status).toEqual(tasks[1].completed);
});

test('check completed status of second list to false', () => {
const status = task.checked(false, 1);
const tasks = getFromLocalStorage('tasks');

expect(status).toEqual(tasks[1].completed);
});
});
7 changes: 7 additions & 0 deletions modules/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Task {

saveToLocalStorage('tasks', this.tasks);
this.refresh();

return this.tasks[i];
}

updateIndex = (index, array) => {
Expand Down Expand Up @@ -112,6 +114,8 @@ class Task {
const tasks = getFromLocalStorage('tasks');
tasks[i].completed = option;
saveToLocalStorage('tasks', tasks);

return tasks[i].completed;
}

createList = (description, i) => {
Expand Down Expand Up @@ -154,6 +158,9 @@ class Task {
const tasks = getFromLocalStorage('tasks');
tasks.filter((item) => item.completed === true)
.forEach((item) => this.delete(item.description, tasks));

const task = getFromLocalStorage('tasks');
return task;
}

clearCompleted() {
Expand Down