js-vitest template

small update for fix-jest-dom
This commit is contained in:
Alex Lohr
2022-03-14 21:39:38 +01:00
parent a29501c437
commit 5930402172
13 changed files with 1942 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
import { render } from 'solid-js/web';
import { TodoList } from './todo-list';
render(() => <TodoList />, document.getElementById('root'));
+55
View File
@@ -0,0 +1,55 @@
import { For, createSignal } from 'solid-js';
export const TodoList = () => {
let input;
let todoId = 0;
const [todos, setTodos] = createSignal([]);
const addTodo = (text) => {
setTodos([...todos(), { id: ++todoId, text, completed: false }]);
};
const toggleTodo = (id) => {
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>
</>
);
};
+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');
const button = getByText('Add Todo');
input.value = 'test new todo';
fireEvent.click(button);
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');
const button = getByText('Add Todo');
input.value = 'mark new todo as completed';
fireEvent.click(button);
const completed = await findByRole('checkbox');
expect(completed?.checked).toBe(false);
fireEvent.click(completed);
expect(completed?.checked).toBe(true);
const text = getByText('mark new todo as completed');
expect(text).toHaveStyle({ 'text-decoration': 'line-through' });
unmount();
});
});