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 To Do list: part 2 #7

Merged
merged 4 commits into from
Jun 9, 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
94 changes: 94 additions & 0 deletions __test__/updateClear.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable */

import crud from '../src/js/CRUD';
import TaskList from '../src/js/taskList';
import CompletedTask from '../src/js/CompletedTasks';

// super class mock initiate
jest.mock('../src/js/taskList');

let localStorage = [
{
index: 1,
completed: false,
description: 'task1',
},
{
index: 2,
completed: false,
description: 'task2',
},
{
index: 3,
completed: false,
description: 'task3',
}
];

describe('Update task test', () => {
const cr = new crud();

const mockSetList = jest.fn((tasks) => localStorage = tasks);
TaskList.prototype.setList = mockSetList;

test('update task description test', () => {
// Arrange
cr.tasks = localStorage;
const description = "Added new description";

// ACT
const updatedTask = cr.updateTask(0, description);

// Assert
expect(description).toBe(updatedTask[0].description);
})
});

describe('Remove task and clear completed tests', () => {
const ct = new CompletedTask();

// mock set local storage
const mockSetList = jest.fn((tasks) => localStorage = tasks);

// mock reorder index function
const mockReOrderTaskIndex = jest.fn((tasks) => {
tasks.forEach((task, i) => task.index = i + 1 );
return localStorage = tasks;
});

TaskList.prototype.setList = mockSetList;
TaskList.prototype.reOrderTaskIndex = mockReOrderTaskIndex;

test('Update 1 completed task', () => {
// Arrange
ct.tasks = localStorage;

// ACT
ct.completedTask(2,true);

// Assert
expect(ct.tasks[2].completed).toBeTruthy();

});

test('Clear all completed tasks test', () => {
// Arrange
const checkTaskTwo = { index: 1, completed: false, description: 'task2' };

ct.tasks = localStorage.map((task) => {
if(task.index % 2 !== 0) {
task.completed = true;
}
return task;
});

// Act
const nonCompletedTask = ct.completedMultipleTasks();

// Assert
expect(ct.tasks).toHaveLength(3);
expect(nonCompletedTask).toHaveLength(1);
expect(nonCompletedTask[0].index).toEqual(1);
expect(nonCompletedTask[0]).toEqual(checkTaskTwo);
});
});
37 changes: 0 additions & 37 deletions dist/index.html

This file was deleted.

6 changes: 4 additions & 2 deletions src/js/CompletedTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export default class extends TaskList {

completedTask(listIndex = -1, completed = false) {
this.tasks[listIndex].completed = completed;
return super.setList(this.tasks);
super.setList(this.tasks);
return this.tasks;
}

completedMultipleTasks() {
const tasks = this.tasks.filter(({ completed }) => !completed);
return super.reOrderTaskIndex(tasks);
super.reOrderTaskIndex(tasks);
return tasks;
}
}