Skip to content

Commit 6d50c00

Browse files
committed
solo-coder-phase4: 2b1e69fae-5b00-4c9e-a3e5-08fa6276051c / beta / round1 (.115908579437896:8eb0280d7bbd747b6bcf6ebc44354dbb_69e3912187d9eec1bd049513.69e3913f87d9eec1bd049517.6560e216e0604f3e898d6ee0:Trae CN.T(2026/4/18 22:12:15))
1 parent 3919f37 commit 6d50c00

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

lib/todo-app.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,16 @@ function subscriptions (signal) {
329329
}
330330
break;
331331
case ESCAPE_KEY:
332-
signal('CANCEL')();
332+
var editing = document.getElementsByClassName('editing');
333+
if (editing && editing.length > 0) {
334+
signal('CANCEL')();
335+
} else {
336+
var new_todo = document.getElementById('new-todo');
337+
if (new_todo.value.length > 0) {
338+
new_todo.value = '';
339+
new_todo.focus();
340+
}
341+
}
333342
break;
334343
}
335344
});

test/todo-app.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,90 @@ test('5.5 CANCEL should cancel edits on escape', function (t) {
547547
t.end();
548548
});
549549

550+
test('5.6 ESCAPE in new-todo with content should clear input and keep focus', function (t) {
551+
elmish.empty(document.getElementById(id));
552+
localStorage.removeItem('todos-elmish_' + id);
553+
const model = {
554+
todos: [
555+
{ id: 0, title: "Make something people want.", done: false },
556+
{ id: 1, title: "Let's solve our own problem", done: false }
557+
],
558+
hash: '#/'
559+
};
560+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
561+
562+
const new_todo = document.getElementById('new-todo');
563+
new_todo.value = 'This is a test todo';
564+
t.equal(new_todo.value, 'This is a test todo', 'new-todo has content before ESC');
565+
566+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 27}));
567+
568+
t.equal(new_todo.value, '', 'new-todo should be cleared after ESC');
569+
t.equal(document.activeElement, new_todo, 'new-todo should keep focus after ESC');
570+
t.equal(document.querySelectorAll('.editing').length, 0, 'not in editing mode');
571+
t.equal(document.querySelectorAll('.view').length, 2, 'todos remain unchanged');
572+
573+
elmish.empty(document.getElementById(id));
574+
localStorage.removeItem('todos-elmish_' + id);
575+
t.end();
576+
});
577+
578+
test('5.7 ESCAPE in new-todo with empty content should do nothing', function (t) {
579+
elmish.empty(document.getElementById(id));
580+
localStorage.removeItem('todos-elmish_' + id);
581+
const model = {
582+
todos: [
583+
{ id: 0, title: "Make something people want.", done: false },
584+
{ id: 1, title: "Let's solve our own problem", done: false }
585+
],
586+
hash: '#/'
587+
};
588+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
589+
590+
const new_todo = document.getElementById('new-todo');
591+
t.equal(new_todo.value, '', 'new-todo is empty before ESC');
592+
593+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 27}));
594+
595+
t.equal(new_todo.value, '', 'new-todo remains empty after ESC');
596+
t.equal(document.querySelectorAll('.view').length, 2, 'todos remain unchanged');
597+
598+
elmish.empty(document.getElementById(id));
599+
localStorage.removeItem('todos-elmish_' + id);
600+
t.end();
601+
});
602+
603+
test('5.8 ESCAPE in editing mode should cancel edit and not affect new-todo', function (t) {
604+
elmish.empty(document.getElementById(id));
605+
localStorage.removeItem('todos-elmish_' + id);
606+
const model = {
607+
todos: [
608+
{ id: 0, title: "Make something people want.", done: false },
609+
{ id: 1, title: "Let's solve our own problem", done: false }
610+
],
611+
hash: '#/',
612+
editing: 1
613+
};
614+
elmish.mount(model, app.update, app.view, id, app.subscriptions);
615+
616+
const new_todo = document.getElementById('new-todo');
617+
new_todo.value = 'New todo content';
618+
619+
t.equal(document.querySelectorAll('.editing').length, 1, 'in editing mode');
620+
t.equal(new_todo.value, 'New todo content', 'new-todo has content');
621+
622+
document.dispatchEvent(new KeyboardEvent('keyup', {'keyCode': 27}));
623+
624+
t.equal(document.querySelectorAll('.editing').length, 0, 'editing mode canceled');
625+
t.equal(document.querySelectorAll('.view > label')[1].textContent,
626+
model.todos[1].title, 'todo item title unchanged');
627+
t.equal(new_todo.value, 'New todo content', 'new-todo content preserved');
628+
629+
elmish.empty(document.getElementById(id));
630+
localStorage.removeItem('todos-elmish_' + id);
631+
t.end();
632+
});
633+
550634
test('6. Counter > should display the current number of todo items',
551635
function (t) {
552636
elmish.empty(document.getElementById(id));

0 commit comments

Comments
 (0)