-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.test.js
198 lines (192 loc) · 6.58 KB
/
crud.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require('fs');
const {
createTask,
tasks,
deleteAndRemoveTask,
updateTask,
changeTaskStatus,
clearAllCompletedTasks,
} = require('./src/modules/helpers');
const createTaskforTest = (task) => {
const newItemElem = document.getElementById('add-item');
newItemElem.value = task;
const event = { constructor: { name: 'PointerEvent' } };
createTask(event);
return newItemElem;
};
describe('createTask', () => {
beforeAll(() => {
document.body.innerHTML = fs.readFileSync('build/index.html');
});
beforeEach(() => {
const newItemElem = document.getElementById('add-item');
newItemElem.value = 'Aaron';
const event = { constructor: { name: 'PointerEvent' } };
createTask(event);
});
afterEach(() => {
tasks.splice(0);
localStorage.clear();
});
it('adds a new task object into the data array', () => {
expect(tasks.length).toBe(1);
});
it('saves the task into the local storage', () => {
expect(localStorage['todo-tasks']).toBeTruthy();
});
it('renders task on the DOM', () => {
expect(document.querySelector('.todo-item')).toBeTruthy();
});
});
describe('deleteAndRemoveTask', () => {
beforeAll(() => {
document.body.innerHTML = fs.readFileSync('build/index.html');
});
beforeEach(() => {
const newItemElem = document.getElementById('add-item');
newItemElem.value = 'Aaron';
const event = { constructor: { name: 'PointerEvent' } };
createTask(event);
const e = { target: document.getElementById('triple-dot') };
deleteAndRemoveTask(e);
});
it('removes a task object out of the data array', () => {
expect(tasks.length).toBe(0);
});
it('checks to see if the added todo item is removed from local storage', () => {
expect(localStorage['todo-tasks']).toBe('[]');
});
it('checks to see if the added toto task was removed from the dom', () => {
expect(document.querySelector('.todo-item')).toBeFalsy();
});
});
describe('updateTask', () => {
beforeAll(() => {
document.body.innerHTML = fs.readFileSync('build/index.html');
});
beforeEach(() => {
// create task
const newItemElem = document.getElementById('add-item');
newItemElem.value = 'Henry';
const event = { constructor: { name: 'PointerEvent' } };
createTask(event);
// update Task
const todoElement = document.querySelector('.todo-item');
const expectedValue = 'You are cool';
todoElement.children[1].value = expectedValue;
updateTask(todoElement);
});
afterEach(() => {
const e = { target: document.getElementById('triple-dot') };
deleteAndRemoveTask(e);
});
it('Updated DOM input value is stored in the data array', () => {
const expectedValue = 'You are cool';
expect(tasks.at(0).description).toBe(expectedValue);
});
it('Value is updated with a new value', () => {
const inputElement = document.querySelector('.description');
expect(tasks.at(0).description).toBe(inputElement.value);
});
it('Local Storage has updated value', () => {
const expectedValue = 'You are cool';
const tasksFromStorage = JSON.parse(localStorage.getItem('todo-tasks'));
const updatedValue = tasksFromStorage[0].description;
expect(updatedValue).toBe(expectedValue);
});
});
describe('changeTaskStatus', () => {
beforeAll(() => {
document.body.innerHTML = fs.readFileSync('build/index.html');
});
beforeEach(() => {
// create task
const newItemElem = document.getElementById('add-item');
newItemElem.value = 'Laundry';
const event = { constructor: { name: 'PointerEvent' } };
createTask(event);
// update Task
const todoElement = document.querySelector('.todo-item');
const e = { target: todoElement.children[0] };
changeTaskStatus(e);
});
afterEach(() => {
const e = { target: document.getElementById('triple-dot') };
deleteAndRemoveTask(e);
});
it('Task completion value is toggled in local memory', () => {
expect(tasks.at(0).completed).toBe(true);
});
it('Task completion value is toggled in local storage', () => {
const tasksFromStorage = JSON.parse(localStorage.getItem('todo-tasks'));
const completion = tasksFromStorage[0].completed;
expect(completion).toBe(true);
});
it('Task toggles completion back to false in memory when ran again', () => {
// update Task to not completed
const todoElement = document.querySelector('.todo-item');
const e = { target: todoElement.children[0] };
changeTaskStatus(e);
expect(tasks.at(0).completed).toBe(false);
});
it('Task toggles completion back to false in local Storage when ran again', () => {
// update Task to not completed
const todoElement = document.querySelector('.todo-item');
const e = { target: todoElement.children[0] };
changeTaskStatus(e);
const tasksFromStorage = JSON.parse(localStorage.getItem('todo-tasks'));
const completion = tasksFromStorage[0].completed;
expect(completion).toBe(false);
});
});
describe('clearAllCompletedTasks', () => {
beforeAll(() => {
document.body.innerHTML = fs.readFileSync('build/index.html');
});
beforeEach(() => {
// create task
createTaskforTest('Take dog to vet');
createTaskforTest('Go to see Batman');
// mark first task as completed
const todoElement = document.querySelector('.todo-item');
const e = { target: todoElement.children[0] };
changeTaskStatus(e);
// clear completed tasks
clearAllCompletedTasks();
});
afterEach(() => {
const e = { target: document.getElementById('triple-dot') };
deleteAndRemoveTask(e);
});
it('First completed task is cleared from DOM', () => {
const taskElements = document.querySelectorAll('.todo-item');
expect(taskElements).toHaveLength(1);
});
it('Completed task is removed from local memory', () => {
const expectedValue = 'Go to see Batman';
expect(tasks.at(0).description).toBe(expectedValue);
});
it('Completed task is removed from local storage', () => {
const expectedValue = 'Go to see Batman';
const tasksFromStorage = JSON.parse(localStorage.getItem('todo-tasks'));
const updatedValue = tasksFromStorage[0].description;
expect(updatedValue).toBe(expectedValue);
});
it('Only incomplete items remain after calling function', () => {
const todos = [
'Buy shampoo',
'Eat laundry',
'Take lizard on a walk',
'Buy time',
'Take doctor to vet',
"Change lawyer's diapers",
'Buy the ocean',
];
todos.forEach((todo) => {
createTaskforTest(todo);
});
clearAllCompletedTasks();
const allAreIncomplete = tasks.every((task) => !task.completed);
expect(allAreIncomplete).toBe(true);
});
});