Add a template for ts-jest
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
"@babel/preset-env",
|
||||||
|
"babel-preset-solid",
|
||||||
|
// only if you use TS with solid-jest
|
||||||
|
"@babel/preset-typescript"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
node_modules
|
||||||
|
dist
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<meta name="theme-color" content="#000000" />
|
||||||
|
<link rel="shortcut icon" type="image/ico" href="/src/assets/favicon.ico" />
|
||||||
|
<title>Solid App</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
|
<div id="root"></div>
|
||||||
|
|
||||||
|
<script src="/src/index.tsx" type="module"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import "regenerator-runtime/runtime";
|
||||||
|
import "@testing-library/jest-dom";
|
||||||
|
import "@testing-library/jest-dom/extend-expect";
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"name": "ts-jest-template-solid",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"description": "",
|
||||||
|
"scripts": {
|
||||||
|
"start": "vite",
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "vite build",
|
||||||
|
"serve": "vite preview",
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"jest": {
|
||||||
|
"preset": "solid-jest/preset/browser",
|
||||||
|
"setupFilesAfterEnv": [
|
||||||
|
"<rootDir>/jest-setup.ts"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "^7.17.10",
|
||||||
|
"@babel/preset-env": "^7.17.10",
|
||||||
|
"@babel/preset-typescript": "^7.16.7",
|
||||||
|
"@testing-library/jest-dom": "^5.16.4",
|
||||||
|
"@types/jest": "^27.5.0",
|
||||||
|
"babel-preset-jest": "^28.0.2",
|
||||||
|
"babel-preset-solid": "^1.3.17",
|
||||||
|
"babel-jest": "^28.1.0",
|
||||||
|
"jest": "^28.1.0",
|
||||||
|
"jest-environment-jsdom": "^28.1.0",
|
||||||
|
"jsdom": "^19.0.0",
|
||||||
|
"regenerator-runtime": "0.13.9",
|
||||||
|
"solid-jest": "^0.2.0",
|
||||||
|
"solid-testing-library": "^0.3.0",
|
||||||
|
"typescript": "^4.6.4",
|
||||||
|
"vite": "^2.9.8",
|
||||||
|
"vite-plugin-solid": "^2.2.6"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"solid-js": "^1.3.17"
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+4599
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,5 @@
|
|||||||
|
import { render } from 'solid-js/web';
|
||||||
|
|
||||||
|
import { TodoList } from './todo-list';
|
||||||
|
|
||||||
|
render(() => <TodoList />, document.getElementById('root'));
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"types": ["jest", "@testing-library/jest-dom"],
|
||||||
|
"compilerOptions": {
|
||||||
|
"strict": true,
|
||||||
|
"target": "ESNext",
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"jsx": "preserve",
|
||||||
|
"jsxImportSource": "solid-js",
|
||||||
|
"types": ["vite/client"],
|
||||||
|
"noEmit": true,
|
||||||
|
"isolatedModules": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { defineConfig } from 'vite';
|
||||||
|
import solidPlugin from 'vite-plugin-solid';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [solidPlugin()],
|
||||||
|
build: {
|
||||||
|
target: 'esnext',
|
||||||
|
polyfillDynamicImport: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user