mayoiga 0.1.7 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/component.d.ts +19 -3
- package/dist/component.js +20 -3
- package/dist/index.d.ts +153 -10
- package/dist/index.js +112 -23
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +5 -0
- package/package.json +34 -35
- package/.eslintrc.js +0 -111
- package/.github/workflows/checker.yml +0 -42
- package/.gitignore +0 -217
- package/.prettierrc +0 -5
- package/dist/component.test.d.ts +0 -1
- package/dist/component.test.js +0 -112
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -113
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/icons.css +0 -1043
- package/docs/assets/icons.png +0 -0
- package/docs/assets/icons@2x.png +0 -0
- package/docs/assets/main.js +0 -52
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1414
- package/docs/assets/widgets.png +0 -0
- package/docs/assets/widgets@2x.png +0 -0
- package/docs/index.html +0 -41
- package/docs/modules.html +0 -4
- package/example/src/demo-app.tsx +0 -123
- package/example/src/index.tsx +0 -15
- package/setup/vite-global-setup.js +0 -1
- package/setup/vite-set-global.js +0 -0
- package/src/component.test.tsx +0 -204
- package/src/component.tsx +0 -23
- package/src/index.test.tsx +0 -176
- package/src/index.ts +0 -352
- package/src/utils.ts +0 -8
- package/tsconfig.json +0 -38
- package/vite.config.js +0 -10
- package/yarn.lock +0 -2649
package/.eslintrc.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
parser: '@typescript-eslint/parser',
|
|
3
|
-
plugins: ['@typescript-eslint/eslint-plugin', "testing-library"],
|
|
4
|
-
extends: [
|
|
5
|
-
'eslint:recommended',
|
|
6
|
-
'plugin:@typescript-eslint/recommended',
|
|
7
|
-
// Lintの時間があまりに長いようなら無効化も考える
|
|
8
|
-
'plugin:@typescript-eslint/recommended-requiring-type-checking',
|
|
9
|
-
|
|
10
|
-
'plugin:import/errors',
|
|
11
|
-
'plugin:import/typescript',
|
|
12
|
-
'plugin:testing-library/react',
|
|
13
|
-
],
|
|
14
|
-
parserOptions: {
|
|
15
|
-
project: './tsconfig.json',
|
|
16
|
-
ecmaVersion: 'next',
|
|
17
|
-
sourceType: 'module',
|
|
18
|
-
ecaFeatures: {
|
|
19
|
-
jsx: true,
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
rules: {
|
|
23
|
-
curly: 'error',
|
|
24
|
-
eqeqeq: ['error', 'smart'],
|
|
25
|
-
|
|
26
|
-
'no-return-await': 'error',
|
|
27
|
-
'require-await': 'error',
|
|
28
|
-
'no-await-in-loop': 'error',
|
|
29
|
-
|
|
30
|
-
'@typescript-eslint/prefer-readonly': 'error',
|
|
31
|
-
// '@typescript-eslint/prefer-readonly-parameter-types': ["error", {ignoreInferredTypes: true}],
|
|
32
|
-
'@typescript-eslint/array-type': ['error', {default: 'generic', readonly: 'generic'}],
|
|
33
|
-
'@typescript-eslint/consistent-type-imports': [
|
|
34
|
-
'error',
|
|
35
|
-
{
|
|
36
|
-
prefer: 'type-imports',
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
|
|
40
|
-
'padding-line-between-statements': [
|
|
41
|
-
'error',
|
|
42
|
-
{blankLine: 'always', prev: 'multiline-const', next: 'multiline-const'},
|
|
43
|
-
{blankLine: 'always', prev: '*', next: 'function'},
|
|
44
|
-
{blankLine: 'always', prev: '*', next: 'class'},
|
|
45
|
-
{blankLine: 'always', prev: '*', next: 'export'},
|
|
46
|
-
],
|
|
47
|
-
|
|
48
|
-
'no-use-before-define': 'off',
|
|
49
|
-
'@typescript-eslint/no-use-before-define': ['error'],
|
|
50
|
-
|
|
51
|
-
'no-unused-vars': 'off',
|
|
52
|
-
'@typescript-eslint/no-unused-vars': [
|
|
53
|
-
'error',
|
|
54
|
-
{
|
|
55
|
-
vars: 'all',
|
|
56
|
-
args: 'none',
|
|
57
|
-
ignoreRestSiblings: true,
|
|
58
|
-
|
|
59
|
-
argsIgnorePattern: '^_',
|
|
60
|
-
varsIgnorePattern: '^_',
|
|
61
|
-
},
|
|
62
|
-
],
|
|
63
|
-
|
|
64
|
-
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
65
|
-
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
66
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
|
67
|
-
|
|
68
|
-
// 要検討
|
|
69
|
-
'@typescript-eslint/no-unsafe-assignment': 'off',
|
|
70
|
-
// voidとのunionで微妙な挙動をするので一旦停止
|
|
71
|
-
'@typescript-eslint/no-misused-promises': [
|
|
72
|
-
'error',
|
|
73
|
-
{
|
|
74
|
-
checksVoidReturn: false,
|
|
75
|
-
},
|
|
76
|
-
],
|
|
77
|
-
|
|
78
|
-
'import/newline-after-import': 'error',
|
|
79
|
-
'import/named': 'off',
|
|
80
|
-
'import/no-named-default': 'error',
|
|
81
|
-
'import/no-default-export': 'off',
|
|
82
|
-
'import/default': 'off',
|
|
83
|
-
'import/no-useless-path-segments': 'error',
|
|
84
|
-
'import/order': [
|
|
85
|
-
'error',
|
|
86
|
-
{
|
|
87
|
-
groups: ['builtin', 'external', 'internal', 'parent', ['index', 'sibling'], 'type'],
|
|
88
|
-
'newlines-between': 'always-and-inside-groups',
|
|
89
|
-
alphabetize: {
|
|
90
|
-
order: 'asc' /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */,
|
|
91
|
-
caseInsensitive: true /* ignore case. Options: [true, false] */,
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
],
|
|
95
|
-
|
|
96
|
-
},
|
|
97
|
-
settings: {
|
|
98
|
-
'import/parsers': {
|
|
99
|
-
'@typescript-eslint/parser': ['.js', '.jsx', '.ts', '.tsx'],
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
'import/ignore': ['node_modules'],
|
|
103
|
-
},
|
|
104
|
-
"overrides": [
|
|
105
|
-
{
|
|
106
|
-
// 3) Now we enable eslint-plugin-testing-library rules or preset only for matching files!
|
|
107
|
-
"files": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
|
|
108
|
-
"extends": ["plugin:testing-library/react"]
|
|
109
|
-
},
|
|
110
|
-
]
|
|
111
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
name: Lint Check
|
|
2
|
-
|
|
3
|
-
on: [push]
|
|
4
|
-
|
|
5
|
-
jobs:
|
|
6
|
-
build:
|
|
7
|
-
|
|
8
|
-
runs-on: ubuntu-latest
|
|
9
|
-
|
|
10
|
-
strategy:
|
|
11
|
-
matrix:
|
|
12
|
-
node-version: [16.x]
|
|
13
|
-
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v1
|
|
16
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
17
|
-
uses: actions/setup-node@v1
|
|
18
|
-
with:
|
|
19
|
-
node-version: ${{ matrix.node-version }}
|
|
20
|
-
|
|
21
|
-
- uses: actions/checkout@v1
|
|
22
|
-
- name: Cache node_modules
|
|
23
|
-
uses: actions/cache@preview
|
|
24
|
-
with:
|
|
25
|
-
path: ~/.cache/yarn
|
|
26
|
-
key: ${{ runner.os }}-projectname-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
|
|
27
|
-
restore-keys:
|
|
28
|
-
${{ runner.os }}-projectname-
|
|
29
|
-
|
|
30
|
-
- name: Install node_modules
|
|
31
|
-
if: steps.cache.outputs.cache-hit != 'true'
|
|
32
|
-
run: |
|
|
33
|
-
npm install -g yarn
|
|
34
|
-
yarn install
|
|
35
|
-
|
|
36
|
-
- name: Lint
|
|
37
|
-
run: |
|
|
38
|
-
yarn check-all
|
|
39
|
-
|
|
40
|
-
- name: Test
|
|
41
|
-
run: |
|
|
42
|
-
yarn test
|
package/.gitignore
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
# Created by https://www.toptal.com/developers/gitignore/api/macos,node,visualstudiocode,vim
|
|
3
|
-
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,node,visualstudiocode,vim
|
|
4
|
-
|
|
5
|
-
### macOS ###
|
|
6
|
-
# General
|
|
7
|
-
.DS_Store
|
|
8
|
-
.AppleDouble
|
|
9
|
-
.LSOverride
|
|
10
|
-
|
|
11
|
-
# Icon must end with two \r
|
|
12
|
-
Icon
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
# Thumbnails
|
|
16
|
-
._*
|
|
17
|
-
|
|
18
|
-
# Files that might appear in the root of a volume
|
|
19
|
-
.DocumentRevisions-V100
|
|
20
|
-
.fseventsd
|
|
21
|
-
.Spotlight-V100
|
|
22
|
-
.TemporaryItems
|
|
23
|
-
.Trashes
|
|
24
|
-
.VolumeIcon.icns
|
|
25
|
-
.com.apple.timemachine.donotpresent
|
|
26
|
-
|
|
27
|
-
# Directories potentially created on remote AFP share
|
|
28
|
-
.AppleDB
|
|
29
|
-
.AppleDesktop
|
|
30
|
-
Network Trash Folder
|
|
31
|
-
Temporary Items
|
|
32
|
-
.apdisk
|
|
33
|
-
|
|
34
|
-
### Node ###
|
|
35
|
-
# Logs
|
|
36
|
-
logs
|
|
37
|
-
*.log
|
|
38
|
-
npm-debug.log*
|
|
39
|
-
yarn-debug.log*
|
|
40
|
-
yarn-error.log*
|
|
41
|
-
lerna-debug.log*
|
|
42
|
-
.pnpm-debug.log*
|
|
43
|
-
|
|
44
|
-
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
45
|
-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
46
|
-
|
|
47
|
-
# Runtime data
|
|
48
|
-
pids
|
|
49
|
-
*.pid
|
|
50
|
-
*.seed
|
|
51
|
-
*.pid.lock
|
|
52
|
-
|
|
53
|
-
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
54
|
-
lib-cov
|
|
55
|
-
|
|
56
|
-
# Coverage directory used by tools like istanbul
|
|
57
|
-
coverage
|
|
58
|
-
*.lcov
|
|
59
|
-
|
|
60
|
-
# nyc test coverage
|
|
61
|
-
.nyc_output
|
|
62
|
-
|
|
63
|
-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
64
|
-
.grunt
|
|
65
|
-
|
|
66
|
-
# Bower dependency directory (https://bower.io/)
|
|
67
|
-
bower_components
|
|
68
|
-
|
|
69
|
-
# node-waf configuration
|
|
70
|
-
.lock-wscript
|
|
71
|
-
|
|
72
|
-
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
73
|
-
build/Release
|
|
74
|
-
|
|
75
|
-
# Dependency directories
|
|
76
|
-
node_modules/
|
|
77
|
-
jspm_packages/
|
|
78
|
-
|
|
79
|
-
# Snowpack dependency directory (https://snowpack.dev/)
|
|
80
|
-
web_modules/
|
|
81
|
-
|
|
82
|
-
# TypeScript cache
|
|
83
|
-
*.tsbuildinfo
|
|
84
|
-
|
|
85
|
-
# Optional npm cache directory
|
|
86
|
-
.npm
|
|
87
|
-
|
|
88
|
-
# Optional eslint cache
|
|
89
|
-
.eslintcache
|
|
90
|
-
|
|
91
|
-
# Optional stylelint cache
|
|
92
|
-
.stylelintcache
|
|
93
|
-
|
|
94
|
-
# Microbundle cache
|
|
95
|
-
.rpt2_cache/
|
|
96
|
-
.rts2_cache_cjs/
|
|
97
|
-
.rts2_cache_es/
|
|
98
|
-
.rts2_cache_umd/
|
|
99
|
-
|
|
100
|
-
# Optional REPL history
|
|
101
|
-
.node_repl_history
|
|
102
|
-
|
|
103
|
-
# Output of 'npm pack'
|
|
104
|
-
*.tgz
|
|
105
|
-
|
|
106
|
-
# Yarn Integrity file
|
|
107
|
-
.yarn-integrity
|
|
108
|
-
|
|
109
|
-
# dotenv environment variable files
|
|
110
|
-
.env
|
|
111
|
-
.env.development.local
|
|
112
|
-
.env.test.local
|
|
113
|
-
.env.production.local
|
|
114
|
-
.env.local
|
|
115
|
-
|
|
116
|
-
# parcel-bundler cache (https://parceljs.org/)
|
|
117
|
-
.cache
|
|
118
|
-
.parcel-cache
|
|
119
|
-
|
|
120
|
-
# Next.js build output
|
|
121
|
-
.next
|
|
122
|
-
out
|
|
123
|
-
|
|
124
|
-
# Nuxt.js build / generate output
|
|
125
|
-
.nuxt
|
|
126
|
-
dist
|
|
127
|
-
|
|
128
|
-
# Gatsby files
|
|
129
|
-
.cache/
|
|
130
|
-
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
131
|
-
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
132
|
-
# public
|
|
133
|
-
|
|
134
|
-
# vuepress build output
|
|
135
|
-
.vuepress/dist
|
|
136
|
-
|
|
137
|
-
# vuepress v2.x temp and cache directory
|
|
138
|
-
.temp
|
|
139
|
-
|
|
140
|
-
# Docusaurus cache and generated files
|
|
141
|
-
.docusaurus
|
|
142
|
-
|
|
143
|
-
# Serverless directories
|
|
144
|
-
.serverless/
|
|
145
|
-
|
|
146
|
-
# FuseBox cache
|
|
147
|
-
.fusebox/
|
|
148
|
-
|
|
149
|
-
# DynamoDB Local files
|
|
150
|
-
.dynamodb/
|
|
151
|
-
|
|
152
|
-
# TernJS port file
|
|
153
|
-
.tern-port
|
|
154
|
-
|
|
155
|
-
# Stores VSCode versions used for testing VSCode extensions
|
|
156
|
-
.vscode-test
|
|
157
|
-
|
|
158
|
-
# yarn v2
|
|
159
|
-
.yarn/cache
|
|
160
|
-
.yarn/unplugged
|
|
161
|
-
.yarn/build-state.yml
|
|
162
|
-
.yarn/install-state.gz
|
|
163
|
-
.pnp.*
|
|
164
|
-
|
|
165
|
-
### Node Patch ###
|
|
166
|
-
# Serverless Webpack directories
|
|
167
|
-
.webpack/
|
|
168
|
-
|
|
169
|
-
# Optional stylelint cache
|
|
170
|
-
|
|
171
|
-
# SvelteKit build / generate output
|
|
172
|
-
.svelte-kit
|
|
173
|
-
|
|
174
|
-
### Vim ###
|
|
175
|
-
# Swap
|
|
176
|
-
[._]*.s[a-v][a-z]
|
|
177
|
-
!*.svg # comment out if you don't need vector files
|
|
178
|
-
[._]*.sw[a-p]
|
|
179
|
-
[._]s[a-rt-v][a-z]
|
|
180
|
-
[._]ss[a-gi-z]
|
|
181
|
-
[._]sw[a-p]
|
|
182
|
-
|
|
183
|
-
# Session
|
|
184
|
-
Session.vim
|
|
185
|
-
Sessionx.vim
|
|
186
|
-
|
|
187
|
-
# Temporary
|
|
188
|
-
.netrwhist
|
|
189
|
-
*~
|
|
190
|
-
# Auto-generated tag files
|
|
191
|
-
tags
|
|
192
|
-
# Persistent undo
|
|
193
|
-
[._]*.un~
|
|
194
|
-
|
|
195
|
-
### VisualStudioCode ###
|
|
196
|
-
.vscode/*
|
|
197
|
-
!.vscode/settings.json
|
|
198
|
-
!.vscode/tasks.json
|
|
199
|
-
!.vscode/launch.json
|
|
200
|
-
!.vscode/extensions.json
|
|
201
|
-
!.vscode/*.code-snippets
|
|
202
|
-
|
|
203
|
-
# Local History for Visual Studio Code
|
|
204
|
-
.history/
|
|
205
|
-
|
|
206
|
-
# Built Visual Studio Code Extensions
|
|
207
|
-
*.vsix
|
|
208
|
-
|
|
209
|
-
### VisualStudioCode Patch ###
|
|
210
|
-
# Ignore all local history of files
|
|
211
|
-
.history
|
|
212
|
-
.ionide
|
|
213
|
-
|
|
214
|
-
# Support for Project snippet scope
|
|
215
|
-
|
|
216
|
-
# End of https://www.toptal.com/developers/gitignore/api/macos,node,visualstudiocode,vim
|
|
217
|
-
|
package/.prettierrc
DELETED
package/dist/component.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/component.test.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { render, screen, waitFor } from '@testing-library/react';
|
|
2
|
-
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { test, expect } from 'vitest';
|
|
5
|
-
import * as zod from 'zod';
|
|
6
|
-
import { Field, Slicer } from './component';
|
|
7
|
-
import { createFormHook } from './index';
|
|
8
|
-
const testSchema = zod.object({
|
|
9
|
-
name: zod.string(),
|
|
10
|
-
description: zod.string(),
|
|
11
|
-
age: zod
|
|
12
|
-
.string()
|
|
13
|
-
.transform((val) => Number(val))
|
|
14
|
-
.refine((val) => (Number.isNaN(val) ? 0 : val)),
|
|
15
|
-
marked: zod.boolean(),
|
|
16
|
-
});
|
|
17
|
-
const createTestHook = () => createFormHook({ name: '', description: '', age: '', marked: false }, testSchema);
|
|
18
|
-
// afterEach(cleanup);
|
|
19
|
-
test('Field component works fine in basic usage.', async () => {
|
|
20
|
-
const TestFormHook = createTestHook();
|
|
21
|
-
const Top = () => {
|
|
22
|
-
return (React.createElement("div", null,
|
|
23
|
-
React.createElement(Field, { controller: TestFormHook.controller, name: "age" }, (tool) => React.createElement("input", { "data-testid": "age-input", ...tool }))));
|
|
24
|
-
};
|
|
25
|
-
render(React.createElement(Top, null));
|
|
26
|
-
const INPUT_AGE = '42';
|
|
27
|
-
// calling `userEvent` in here will cause race condition...
|
|
28
|
-
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
29
|
-
await waitFor(() => {
|
|
30
|
-
const ageInput = screen.getByTestId('age-input');
|
|
31
|
-
expect(ageInput.value).toBe(INPUT_AGE);
|
|
32
|
-
TestFormHook.api.actions.peek((s) => {
|
|
33
|
-
expect(s.value.age).toBe(INPUT_AGE);
|
|
34
|
-
expect(s.errors.age).toBeNull();
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
test('Slice component works fine in basic usage.', async () => {
|
|
39
|
-
const TestFormHook = createTestHook();
|
|
40
|
-
const Top = () => {
|
|
41
|
-
return (React.createElement("div", null,
|
|
42
|
-
React.createElement(Slicer, { controller: TestFormHook.controller, selector: (s) => [s.value.age] }, (tool, age) => (React.createElement("input", { "data-testid": "age-input", name: "age", value: age, onChange: (e) => tool.handleChange('age', e.target.value) })))));
|
|
43
|
-
};
|
|
44
|
-
render(React.createElement(Top, null));
|
|
45
|
-
const INPUT_AGE = '42';
|
|
46
|
-
// calling `userEvent` in here will cause race condition...
|
|
47
|
-
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
48
|
-
await waitFor(() => {
|
|
49
|
-
const ageInput = screen.getByTestId('age-input');
|
|
50
|
-
expect(ageInput.value).toBe(INPUT_AGE);
|
|
51
|
-
TestFormHook.api.actions.peek((s) => {
|
|
52
|
-
expect(s.value.age).toBe(INPUT_AGE);
|
|
53
|
-
expect(s.errors.age).toBeNull();
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
test('A user able to create a reusable component', async () => {
|
|
58
|
-
const TestFormHook = createTestHook();
|
|
59
|
-
function NarrowTypedExpected(props) {
|
|
60
|
-
return (React.createElement(Field, { controller: props.controller, name: "age" }, (tool) => React.createElement("input", { "data-testid": "age-input", ...tool })));
|
|
61
|
-
}
|
|
62
|
-
function ErrorDisplayer(props) {
|
|
63
|
-
const { controller } = props;
|
|
64
|
-
return (React.createElement(Slicer, { controller: controller, selector: (s) => [s.value.age] }, (tool, age) => (React.createElement("button", { onClick: (e) => {
|
|
65
|
-
controller.actions.pushFormErrors((_state) => ({ age: age === '42' ? 'yes!!!' : 'noooo' }));
|
|
66
|
-
} }))));
|
|
67
|
-
}
|
|
68
|
-
render(React.createElement("div", null,
|
|
69
|
-
React.createElement(NarrowTypedExpected, { controller: TestFormHook.controller }),
|
|
70
|
-
React.createElement(ErrorDisplayer, { controller: TestFormHook.controller })));
|
|
71
|
-
const INPUT_AGE = '42';
|
|
72
|
-
// calling `userEvent` in here will cause race condition...
|
|
73
|
-
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
74
|
-
await userEvent.click(screen.getByRole('button'));
|
|
75
|
-
await waitFor(() => {
|
|
76
|
-
const ageInput = screen.getByTestId('age-input');
|
|
77
|
-
expect(ageInput.value).toBe(INPUT_AGE);
|
|
78
|
-
TestFormHook.api.actions.peek((s) => {
|
|
79
|
-
expect(s.value.age).toBe(INPUT_AGE);
|
|
80
|
-
expect(s.errors.age).toBe('yes!!!');
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
test('`deps` field could destruct memoization', async () => {
|
|
85
|
-
const TestFormHook = createTestHook();
|
|
86
|
-
// intentional sample to replicate the situation
|
|
87
|
-
const OtherFormHook = createFormHook({ minAge: '30' }, zod.object({ minAge: zod.string() }));
|
|
88
|
-
const DATA_ID_RE_RENDER_BY_DEPS = 'refresh-by-deps';
|
|
89
|
-
const DATA_ID_NOT_RERENDER_NO_DEPS = 'no-refersh-no-deps';
|
|
90
|
-
function App() {
|
|
91
|
-
const minAge = OtherFormHook.useSelector((s) => Number(s.value.minAge));
|
|
92
|
-
return (React.createElement("div", null,
|
|
93
|
-
React.createElement(Field, { controller: OtherFormHook.controller, name: "minAge" }, (tool) => React.createElement("input", { "data-testid": "minAgeInput", type: "number", ...tool })),
|
|
94
|
-
React.createElement(Field, { controller: TestFormHook.controller, name: "age" }, (tool) => React.createElement("input", { "data-testid": "ageInput", type: "number", ...tool })),
|
|
95
|
-
React.createElement(Slicer, { controller: TestFormHook.controller, selector: (s) => [Number(s.value.age)], deps: [minAge] }, (_tool, age) => (React.createElement("div", { "data-testid": DATA_ID_RE_RENDER_BY_DEPS }, age >= minAge ? 'You can buy this content' : 'go home'))),
|
|
96
|
-
React.createElement(Slicer, { controller: TestFormHook.controller, selector: (s) => [Number(s.value.age)] }, (_tool, age) => (React.createElement("div", { "data-testid": DATA_ID_NOT_RERENDER_NO_DEPS }, age >= minAge ? 'You can buy this content' : 'go home')))));
|
|
97
|
-
}
|
|
98
|
-
render(React.createElement(App, null));
|
|
99
|
-
await userEvent.type(screen.getByTestId('ageInput'), '16');
|
|
100
|
-
await waitFor(() => {
|
|
101
|
-
expect(screen.getByTestId(DATA_ID_RE_RENDER_BY_DEPS).textContent).toBe('go home');
|
|
102
|
-
// expect(await screen.findByText('You can buy this content')).toBeDefined();
|
|
103
|
-
});
|
|
104
|
-
await userEvent.clear(screen.getByTestId('minAgeInput'));
|
|
105
|
-
await userEvent.type(screen.getByTestId('minAgeInput'), '10');
|
|
106
|
-
await waitFor(() => {
|
|
107
|
-
expect(screen.getByTestId(DATA_ID_RE_RENDER_BY_DEPS).textContent, 'Slicer should re-execute renderProps if `deps` are changed').toBe('You can buy this content');
|
|
108
|
-
});
|
|
109
|
-
await waitFor(() => {
|
|
110
|
-
expect(screen.getByTestId(DATA_ID_NOT_RERENDER_NO_DEPS).textContent, 'Same logic but would not refresh because external dependencies are not applied').toBe('go home');
|
|
111
|
-
});
|
|
112
|
-
});
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { render, screen, waitFor } from '@testing-library/react';
|
|
2
|
-
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { test, expect } from 'vitest';
|
|
5
|
-
import * as zod from 'zod';
|
|
6
|
-
import { createFormHook, useFormSlice } from './index';
|
|
7
|
-
const testSchema = zod.object({
|
|
8
|
-
name: zod.string(),
|
|
9
|
-
description: zod.string(),
|
|
10
|
-
age: zod
|
|
11
|
-
.string()
|
|
12
|
-
.transform((val) => Number(val))
|
|
13
|
-
.refine((val) => (Number.isNaN(val) ? 0 : val)),
|
|
14
|
-
marked: zod.boolean(),
|
|
15
|
-
});
|
|
16
|
-
const initialState = { name: '', description: '', age: '', marked: false };
|
|
17
|
-
const createTestHook = () => createFormHook(initialState, testSchema);
|
|
18
|
-
const AgeInputComponent = (props) => {
|
|
19
|
-
const { controller } = props;
|
|
20
|
-
const [[age, errorMessage], { handleChange }] = useFormSlice(controller, (s) => [s.value.age, s.errors.age]);
|
|
21
|
-
return (React.createElement("div", null,
|
|
22
|
-
React.createElement("input", { "data-testid": "age-input", value: age, name: "age", onChange: (e) => {
|
|
23
|
-
handleChange('age', e.currentTarget.value);
|
|
24
|
-
} }),
|
|
25
|
-
React.createElement("span", { "data-testid": "age-error" }, errorMessage)));
|
|
26
|
-
};
|
|
27
|
-
// afterEach(cleanup);
|
|
28
|
-
test('useSelector can select the fields', async () => {
|
|
29
|
-
const TestFormHook = createTestHook();
|
|
30
|
-
const Top = () => {
|
|
31
|
-
return (React.createElement("div", null,
|
|
32
|
-
React.createElement(AgeInputComponent, { controller: TestFormHook.controller })));
|
|
33
|
-
};
|
|
34
|
-
render(React.createElement(Top, null));
|
|
35
|
-
const INPUT_AGE = '4';
|
|
36
|
-
// fireEvent.change(screen.getByTestId('age-input') , {target: {value: INPUT_AGE}})
|
|
37
|
-
// calling `userEvent` in here will cause race condition...
|
|
38
|
-
await userEvent.type(screen.getByTestId('age-input'), INPUT_AGE);
|
|
39
|
-
// TestFormHook.actions.handleBulkChange(prev => ({...prev, name: 'test', description: 'desc'}));
|
|
40
|
-
await waitFor(() => {
|
|
41
|
-
const ageInput = screen.getByTestId('age-input');
|
|
42
|
-
expect(ageInput.value).toBe(INPUT_AGE);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
test('zod parse value before submit', async () => {
|
|
46
|
-
const TestFormHook = createTestHook();
|
|
47
|
-
const INPUT_AGE = '9';
|
|
48
|
-
const PARSED_RESULT = Number(INPUT_AGE);
|
|
49
|
-
const handleSubmitTester = TestFormHook.handleSubmit((e) => (result) => {
|
|
50
|
-
if (result.success) {
|
|
51
|
-
expect(result.data.age).toBe(PARSED_RESULT);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
expect('should not called').toBe(result.error);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
const Top = () => {
|
|
58
|
-
return (React.createElement("div", null,
|
|
59
|
-
React.createElement(AgeInputComponent, { controller: TestFormHook }),
|
|
60
|
-
React.createElement("button", { "data-testid": "submit-button", onClick: handleSubmitTester })));
|
|
61
|
-
};
|
|
62
|
-
render(React.createElement(Top, null));
|
|
63
|
-
const ageInput = screen.getByTestId('age-input');
|
|
64
|
-
await userEvent.type(ageInput, INPUT_AGE);
|
|
65
|
-
await waitFor(() => {
|
|
66
|
-
const ageInput = screen.getByTestId('age-input');
|
|
67
|
-
expect(ageInput.value).toBe(INPUT_AGE);
|
|
68
|
-
});
|
|
69
|
-
const submitButton = screen.getByTestId('submit-button');
|
|
70
|
-
await userEvent.click(submitButton);
|
|
71
|
-
await waitFor(() => {
|
|
72
|
-
console.log('waiting');
|
|
73
|
-
TestFormHook.api.actions.peek((s) => {
|
|
74
|
-
expect(s.value.age).toBe(INPUT_AGE);
|
|
75
|
-
expect(s.errors.age).toBeNull();
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
test('User is able to re-initialize the form state', async () => {
|
|
80
|
-
const TestFormHook = createTestHook();
|
|
81
|
-
const Top = () => {
|
|
82
|
-
return (React.createElement("div", null,
|
|
83
|
-
React.createElement(AgeInputComponent, { controller: TestFormHook.controller })));
|
|
84
|
-
};
|
|
85
|
-
render(React.createElement(Top, null));
|
|
86
|
-
const ageInput = screen.getByTestId('age-input');
|
|
87
|
-
await userEvent.type(ageInput, '9999');
|
|
88
|
-
expect(TestFormHook.api.getState().isDirty).toBeTruthy();
|
|
89
|
-
TestFormHook.actions.initializeForm({}, { cleanup: true });
|
|
90
|
-
expect(TestFormHook.api.getState().isDirty).toBeFalsy();
|
|
91
|
-
});
|
|
92
|
-
test('User is able to push server side validation', async () => {
|
|
93
|
-
const TestFormHook = createTestHook();
|
|
94
|
-
const Top = () => {
|
|
95
|
-
return (React.createElement("div", null,
|
|
96
|
-
React.createElement(AgeInputComponent, { controller: TestFormHook.controller })));
|
|
97
|
-
};
|
|
98
|
-
render(React.createElement(Top, null));
|
|
99
|
-
const ageInput = screen.getByTestId('age-input');
|
|
100
|
-
await userEvent.type(ageInput, '9999');
|
|
101
|
-
TestFormHook.actions.pushFormErrors((_s) => ({ age: 'server side error' }));
|
|
102
|
-
const state1 = TestFormHook.api.getState();
|
|
103
|
-
expect(state1.isDirty).toBeTruthy();
|
|
104
|
-
expect(state1.isValid).toBeFalsy();
|
|
105
|
-
expect(state1.errors.age).toEqual('server side error');
|
|
106
|
-
await userEvent.type(ageInput, 'hgoehoge');
|
|
107
|
-
// confirm empty field won't overwrite any messages
|
|
108
|
-
TestFormHook.actions.pushFormErrors((_s) => ({}));
|
|
109
|
-
const state2 = TestFormHook.api.getState();
|
|
110
|
-
expect(state2.isDirty).toBeTruthy();
|
|
111
|
-
expect(state2.isValid).toBeFalsy();
|
|
112
|
-
expect(state2.errors.age).toEqual('Invalid input');
|
|
113
|
-
});
|
package/docs/.nojekyll
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
|