@@ -18,7 +18,7 @@ function createMockTodos() {
1818}
1919
2020// A mock task to simulate user input
21- const theTask = { task : "The Task" , completed : false } ;
21+ const theTask = { task : "The Task" , completed : false , deadline : null } ;
2222
2323describe ( "addTask()" , ( ) => {
2424 test ( "Add a task to an empty ToDo list" , ( ) => {
@@ -29,28 +29,22 @@ describe("addTask()", () => {
2929 } ) ;
3030
3131 test ( "Should append a new task to the end of a ToDo list" , ( ) => {
32-
3332 const todos = createMockTodos ( ) ;
3433 const lengthBeforeAddition = todos . length ;
3534 Todos . addTask ( todos , theTask . task , theTask . completed ) ;
36- // todos should now have one more task
3735 expect ( todos ) . toHaveLength ( lengthBeforeAddition + 1 ) ;
38-
39- // New task should be appended to the todos
4036 expect ( todos [ todos . length - 1 ] ) . toEqual ( theTask ) ;
4137 } ) ;
4238} ) ;
4339
4440describe ( "deleteTask()" , ( ) => {
45-
4641 test ( "Delete the first task" , ( ) => {
4742 const todos = createMockTodos ( ) ;
4843 const todosBeforeDeletion = createMockTodos ( ) ;
4944 const lengthBeforeDeletion = todos . length ;
5045 Todos . deleteTask ( todos , 0 ) ;
5146
5247 expect ( todos ) . toHaveLength ( lengthBeforeDeletion - 1 ) ;
53-
5448 expect ( todos [ 0 ] ) . toEqual ( todosBeforeDeletion [ 1 ] ) ;
5549 expect ( todos [ 1 ] ) . toEqual ( todosBeforeDeletion [ 2 ] ) ;
5650 expect ( todos [ 2 ] ) . toEqual ( todosBeforeDeletion [ 3 ] ) ;
@@ -63,7 +57,6 @@ describe("deleteTask()", () => {
6357 Todos . deleteTask ( todos , 1 ) ;
6458
6559 expect ( todos ) . toHaveLength ( lengthBeforeDeletion - 1 ) ;
66-
6760 expect ( todos [ 0 ] ) . toEqual ( todosBeforeDeletion [ 0 ] ) ;
6861 expect ( todos [ 1 ] ) . toEqual ( todosBeforeDeletion [ 2 ] ) ;
6962 expect ( todos [ 2 ] ) . toEqual ( todosBeforeDeletion [ 3 ] ) ;
@@ -76,7 +69,6 @@ describe("deleteTask()", () => {
7669 Todos . deleteTask ( todos , todos . length - 1 ) ;
7770
7871 expect ( todos ) . toHaveLength ( lengthBeforeDeletion - 1 ) ;
79-
8072 expect ( todos [ 0 ] ) . toEqual ( todosBeforeDeletion [ 0 ] ) ;
8173 expect ( todos [ 1 ] ) . toEqual ( todosBeforeDeletion [ 1 ] ) ;
8274 expect ( todos [ 2 ] ) . toEqual ( todosBeforeDeletion [ 2 ] ) ;
@@ -94,15 +86,13 @@ describe("deleteTask()", () => {
9486} ) ;
9587
9688describe ( "toggleCompletedOnTask()" , ( ) => {
97-
9889 test ( "Expect the 'completed' property to toggle on an existing task" , ( ) => {
9990 const todos = createMockTodos ( ) ;
10091 const taskIndex = 1 ;
10192 const completedStateBeforeToggle = todos [ taskIndex ] . completed ;
10293 Todos . toggleCompletedOnTask ( todos , taskIndex ) ;
10394 expect ( todos [ taskIndex ] . completed ) . toEqual ( ! completedStateBeforeToggle ) ;
10495
105- // Toggle again
10696 Todos . toggleCompletedOnTask ( todos , taskIndex ) ;
10797 expect ( todos [ taskIndex ] . completed ) . toEqual ( completedStateBeforeToggle ) ;
10898 } ) ;
@@ -117,7 +107,6 @@ describe("toggleCompletedOnTask()", () => {
117107 expect ( todos [ 3 ] ) . toEqual ( todosBeforeToggle [ 3 ] ) ;
118108 } ) ;
119109
120-
121110 test ( "Expect no change when toggling on a non-existing task" , ( ) => {
122111 const todos = createMockTodos ( ) ;
123112 const todosBeforeToggle = createMockTodos ( ) ;
@@ -130,3 +119,71 @@ describe("toggleCompletedOnTask()", () => {
130119 } ) ;
131120} ) ;
132121
122+ describe ( "deleteCompleted()" , ( ) => {
123+ test ( "Should remove all completed tasks, keeping incomplete ones in order" , ( ) => {
124+ const todos = createMockTodos ( ) ;
125+ const todosBeforeDeletion = createMockTodos ( ) ;
126+ Todos . deleteCompleted ( todos ) ;
127+
128+ expect ( todos ) . toHaveLength ( 2 ) ;
129+ expect ( todos [ 0 ] ) . toEqual ( todosBeforeDeletion [ 1 ] ) ;
130+ expect ( todos [ 1 ] ) . toEqual ( todosBeforeDeletion [ 3 ] ) ;
131+ } ) ;
132+
133+ test ( "Should result in an empty list if all tasks are completed" , ( ) => {
134+ const todos = [
135+ { task : "Task A" , completed : true } ,
136+ { task : "Task B" , completed : true } ,
137+ ] ;
138+ Todos . deleteCompleted ( todos ) ;
139+ expect ( todos ) . toHaveLength ( 0 ) ;
140+ } ) ;
141+
142+ test ( "Should leave the list unchanged if no tasks are completed" , ( ) => {
143+ const todos = [
144+ { task : "Task A" , completed : false } ,
145+ { task : "Task B" , completed : false } ,
146+ ] ;
147+ const todosBeforeDeletion = [ ...todos ] ;
148+ Todos . deleteCompleted ( todos ) ;
149+ expect ( todos ) . toEqual ( todosBeforeDeletion ) ;
150+ } ) ;
151+
152+ test ( "Should do nothing on an empty ToDo list" , ( ) => {
153+ const todos = [ ] ;
154+ Todos . deleteCompleted ( todos ) ;
155+ expect ( todos ) . toEqual ( [ ] ) ;
156+ } ) ;
157+ } ) ;
158+
159+ describe ( "getDeadlineStatus()" , ( ) => {
160+ test ( "Should return null when there is no deadline" , ( ) => {
161+ expect ( Todos . getDeadlineStatus ( null ) ) . toBeNull ( ) ;
162+ } ) ;
163+
164+ test ( "Should return 'Due today' when deadline is today" , ( ) => {
165+ const today = new Date ( ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
166+ expect ( Todos . getDeadlineStatus ( today ) ) . toBe ( "Due today" ) ;
167+ } ) ;
168+
169+ test ( "Should return days remaining for a future deadline" , ( ) => {
170+ const future = new Date ( ) ;
171+ future . setDate ( future . getDate ( ) + 3 ) ;
172+ const futureStr = future . toISOString ( ) . split ( "T" ) [ 0 ] ;
173+ expect ( Todos . getDeadlineStatus ( futureStr ) ) . toBe ( "Due in 3 days" ) ;
174+ } ) ;
175+
176+ test ( "Should return singular 'day' when exactly 1 day left" , ( ) => {
177+ const tomorrow = new Date ( ) ;
178+ tomorrow . setDate ( tomorrow . getDate ( ) + 1 ) ;
179+ const tomorrowStr = tomorrow . toISOString ( ) . split ( "T" ) [ 0 ] ;
180+ expect ( Todos . getDeadlineStatus ( tomorrowStr ) ) . toBe ( "Due in 1 day" ) ;
181+ } ) ;
182+
183+ test ( "Should return overdue message for a past deadline" , ( ) => {
184+ const past = new Date ( ) ;
185+ past . setDate ( past . getDate ( ) - 2 ) ;
186+ const pastStr = past . toISOString ( ) . split ( "T" ) [ 0 ] ;
187+ expect ( Todos . getDeadlineStatus ( pastStr ) ) . toBe ( "Overdue by 2 days" ) ;
188+ } ) ;
189+ } ) ;
0 commit comments