Fresh install

This commit is contained in:
vdv committed 2022-11-25 16:33:59 +01:00
1 parent 4a3be05321
commit fd354b4e51
14 files changed
+7425

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
import { render } from 'solid-js/web';
import { TodoList } from './todo-list';
render(() => <TodoList />, document.getElementById('root'));
+46
View File
@@ -0,0 +1,46 @@
import { describe, expect, test } from 'vitest';
import { render, fireEvent } from 'solid-testing-library';
import { TodoList } from './todo-list';
describe('<TodoList />', () => {
test('it will render an text input and a button', () => {
const { getByPlaceholderText, getByText, unmount } = render(() => (
<TodoList />
));
expect(getByPlaceholderText('new todo here')).toBeInTheDocument();
expect(getByText('Add Todo')).toBeInTheDocument();
unmount();
});
test('it will add a new todo', async () => {
const { getByPlaceholderText, getByText, unmount } = render(() => (
<TodoList />
));
const input = getByPlaceholderText('new todo here') as HTMLInputElement;
const button = getByText('Add Todo');
input.value = 'test new todo';
fireEvent.click(button as HTMLInputElement);
expect(input.value).toBe('');
expect(getByText(/test new todo/)).toBeInTheDocument();
unmount();
});
test('it will mark a todo as completed', async () => {
const { getByPlaceholderText, findByRole, getByText, unmount } = render(
() => <TodoList />,
);
const input = getByPlaceholderText('new todo here') as HTMLInputElement;
const button = getByText('Add Todo') as HTMLButtonElement;
input.value = 'mark new todo as completed';
fireEvent.click(button);
const completed = (await findByRole('checkbox')) as HTMLInputElement;
expect(completed?.checked).toBe(false);
fireEvent.click(completed);
expect(completed?.checked).toBe(true);
const text = getByText('mark new todo as completed') as HTMLSpanElement;
expect(text).toHaveStyle({ 'text-decoration': 'line-through' });
unmount();
});
});
+57
View File
@@ -0,0 +1,57 @@
import { For, createSignal } from 'solid-js';
type Todo = { id: number; text: string; completed: boolean };
export const TodoList = () => {
let input!: HTMLInputElement;
let todoId = 0;
const [todos, setTodos] = createSignal<Todo[]>([]);
const addTodo = (text: string) => {
setTodos([...todos(), { id: ++todoId, text, completed: false }]);
};
const toggleTodo = (id: number) => {
setTodos(
todos().map((todo) =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo,
),
);
};
return (
<>
<div>
<input placeholder="new todo here" ref={input} />
<button
onClick={() => {
if (!input.value.trim()) return;
addTodo(input.value);
input.value = '';
}}
>
Add Todo
</button>
</div>
<For each={todos()}>
{(todo) => {
const { id, text } = todo;
return (
<div>
<input
type="checkbox"
checked={todo.completed}
onchange={[toggleTodo, id]}
/>
<span
style={{
'text-decoration': todo.completed ? 'line-through' : 'none',
}}
>
{text}
</span>
</div>
);
}}
</For>
</>
);
};