ts-uvu starter template
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import { render } from 'solid-js/web';
|
||||
|
||||
import { TodoList } from './todo-list';
|
||||
|
||||
render(() => <TodoList />, document.getElementById('root'));
|
||||
@@ -0,0 +1,47 @@
|
||||
import { suite } from 'uvu';
|
||||
import * as assert from 'uvu/assert';
|
||||
|
||||
import { render, fireEvent, waitFor } from 'solid-testing-library';
|
||||
import { isInDocument, hasStyle } from 'solid-dom-testing'
|
||||
|
||||
import { TodoList } from './todo-list';
|
||||
|
||||
const test = suite<ReturnType<typeof render>>('<TodoList />');
|
||||
|
||||
test.before.each((context) => {
|
||||
const returnValue = render(() => <TodoList />);
|
||||
Object.getOwnPropertyNames(returnValue).forEach(name => {
|
||||
context[name] = returnValue[name];
|
||||
});
|
||||
});
|
||||
|
||||
test.after.each(({ unmount }) => unmount());
|
||||
|
||||
test('it will render an text input and a button', ({ getByPlaceholderText, getByText }) => {
|
||||
assert.ok(isInDocument(getByPlaceholderText('new todo here')));
|
||||
assert.ok(isInDocument(getByText('Add Todo')));
|
||||
});
|
||||
|
||||
test('it will add a new todo', async ({ getByPlaceholderText, getByText }) => {
|
||||
const input = getByPlaceholderText('new todo here') as HTMLInputElement;
|
||||
const button = getByText('Add Todo');
|
||||
input.value = 'test new todo';
|
||||
fireEvent.click(button as HTMLInputElement);
|
||||
assert.is(input.value, '');
|
||||
await waitFor(() => assert.ok(isInDocument(getByText(/test new todo/))));
|
||||
});
|
||||
|
||||
test('it will mark a todo as completed', async ({ getByPlaceholderText, findByRole, getByText }) => {
|
||||
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;
|
||||
assert.is(completed?.checked, false);
|
||||
fireEvent.click(completed);
|
||||
assert.is(completed?.checked, true);
|
||||
const text = getByText('mark new todo as completed') as HTMLSpanElement;
|
||||
assert.ok(hasStyle(text, { 'text-decoration': 'line-through' }));
|
||||
});
|
||||
|
||||
test.run();
|
||||
@@ -0,0 +1,49 @@
|
||||
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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user