piral-forms 1.3.3-beta.6190 → 1.3.3-beta.6201
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/package.json +3 -3
- package/src/actions.test.ts +29 -9
- package/src/create.test.tsx +8 -4
- package/src/types.test.tsx +16 -6
- package/src/useForm.test.ts +440 -386
- package/src/usePromise.test.ts +15 -11
- package/src/usePrompt.test.ts +26 -20
- package/src/withForm.test.tsx +18 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-forms",
|
|
3
|
-
"version": "1.3.3-beta.
|
|
3
|
+
"version": "1.3.3-beta.6201",
|
|
4
4
|
"description": "Plugin for providing advanced form support in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -63,9 +63,9 @@
|
|
|
63
63
|
"@types/history": "^4.7.8",
|
|
64
64
|
"@types/react": "^18.0.0",
|
|
65
65
|
"@types/react-router-dom": "^5.1.6",
|
|
66
|
-
"piral-core": "1.3.3-beta.
|
|
66
|
+
"piral-core": "1.3.3-beta.6201",
|
|
67
67
|
"react": "^18.0.0",
|
|
68
68
|
"react-router-dom": "^5.2.0"
|
|
69
69
|
},
|
|
70
|
-
"gitHead": "
|
|
70
|
+
"gitHead": "6e9f4e6f83514b5c38960ce015e073efc668f3d6"
|
|
71
71
|
}
|
package/src/actions.test.ts
CHANGED
|
@@ -1,15 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import create from 'zustand';
|
|
2
|
-
import {
|
|
3
|
-
import { createActions } from 'piral-core';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
4
6
|
import { updateFormState } from './actions';
|
|
5
7
|
|
|
8
|
+
function createListener() {
|
|
9
|
+
return {
|
|
10
|
+
on: vitest.fn(),
|
|
11
|
+
off: vitest.fn(),
|
|
12
|
+
emit: vitest.fn(),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createActions(state, listener) {
|
|
17
|
+
return {
|
|
18
|
+
...listener,
|
|
19
|
+
state: state.getState(),
|
|
20
|
+
dispatch(change) {
|
|
21
|
+
state.setState(change(state.getState()));
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
describe('Forms Actions Module', () => {
|
|
7
27
|
it('updateFormState works on a fresh forms collection', () => {
|
|
8
28
|
const state: any = create(() => ({
|
|
9
29
|
foo: 5,
|
|
10
30
|
forms: {},
|
|
11
31
|
}));
|
|
12
|
-
const ctx = createActions(state, createListener(
|
|
32
|
+
const ctx = createActions(state, createListener());
|
|
13
33
|
updateFormState(ctx, 'a', { name: 'Foo', active: true }, { name: 'Bar' });
|
|
14
34
|
expect((state.getState())).toEqual({
|
|
15
35
|
foo: 5,
|
|
@@ -31,7 +51,7 @@ describe('Forms Actions Module', () => {
|
|
|
31
51
|
},
|
|
32
52
|
},
|
|
33
53
|
}));
|
|
34
|
-
const ctx = createActions(state, createListener(
|
|
54
|
+
const ctx = createActions(state, createListener());
|
|
35
55
|
updateFormState(ctx, 'a', { name: 'Foo', active: true }, {});
|
|
36
56
|
expect((state.getState())).toEqual({
|
|
37
57
|
foo: 5,
|
|
@@ -53,7 +73,7 @@ describe('Forms Actions Module', () => {
|
|
|
53
73
|
},
|
|
54
74
|
},
|
|
55
75
|
}));
|
|
56
|
-
const ctx = createActions(state, createListener(
|
|
76
|
+
const ctx = createActions(state, createListener());
|
|
57
77
|
updateFormState(ctx, 'a', { name: 'Foo', active: true }, { name: 'bazeol' });
|
|
58
78
|
expect((state.getState())).toEqual({
|
|
59
79
|
foo: 5,
|
|
@@ -75,7 +95,7 @@ describe('Forms Actions Module', () => {
|
|
|
75
95
|
},
|
|
76
96
|
},
|
|
77
97
|
}));
|
|
78
|
-
const ctx = createActions(state, createListener(
|
|
98
|
+
const ctx = createActions(state, createListener());
|
|
79
99
|
updateFormState(ctx, 'a', { name: 'Foo' }, { active: false });
|
|
80
100
|
expect((state.getState())).toEqual({
|
|
81
101
|
foo: 5,
|
|
@@ -92,7 +112,7 @@ describe('Forms Actions Module', () => {
|
|
|
92
112
|
},
|
|
93
113
|
},
|
|
94
114
|
}));
|
|
95
|
-
const ctx = createActions(state, createListener(
|
|
115
|
+
const ctx = createActions(state, createListener());
|
|
96
116
|
updateFormState(ctx, 'a', { name: 'Foo', submitting: true }, { active: false });
|
|
97
117
|
expect((state.getState())).toEqual({
|
|
98
118
|
foo: 5,
|
|
@@ -115,7 +135,7 @@ describe('Forms Actions Module', () => {
|
|
|
115
135
|
},
|
|
116
136
|
},
|
|
117
137
|
}));
|
|
118
|
-
const ctx = createActions(state, createListener(
|
|
138
|
+
const ctx = createActions(state, createListener());
|
|
119
139
|
updateFormState(ctx, 'a', { name: 'Foo', changed: true }, { submitting: false, active: '' });
|
|
120
140
|
expect((state.getState())).toEqual({
|
|
121
141
|
foo: 5,
|
|
@@ -139,7 +159,7 @@ describe('Forms Actions Module', () => {
|
|
|
139
159
|
},
|
|
140
160
|
},
|
|
141
161
|
}));
|
|
142
|
-
const ctx = createActions(state, createListener(
|
|
162
|
+
const ctx = createActions(state, createListener());
|
|
143
163
|
updateFormState(ctx, 'a', { name: 'Foo', changed: false, active: '' }, { submitting: false });
|
|
144
164
|
expect((state.getState())).toEqual({
|
|
145
165
|
foo: 5,
|
package/src/create.test.tsx
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import create from 'zustand';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
import { createElement, FC } from 'react';
|
|
3
7
|
import { createFormsApi } from './create';
|
|
4
8
|
|
|
@@ -9,9 +13,9 @@ function createMockContainer() {
|
|
|
9
13
|
const state = create(() => ({}));
|
|
10
14
|
return {
|
|
11
15
|
context: {
|
|
12
|
-
on:
|
|
13
|
-
off:
|
|
14
|
-
emit:
|
|
16
|
+
on: vitest.fn(),
|
|
17
|
+
off: vitest.fn(),
|
|
18
|
+
emit: vitest.fn(),
|
|
15
19
|
defineActions() {},
|
|
16
20
|
state,
|
|
17
21
|
dispatch(update) {
|
|
@@ -25,7 +29,7 @@ function createMockContainer() {
|
|
|
25
29
|
describe('Create Forms API Extensions', () => {
|
|
26
30
|
it('createCoreApi allows using the created form creator as a HOC', () => {
|
|
27
31
|
const container = createMockContainer();
|
|
28
|
-
container.context.updateFormState =
|
|
32
|
+
container.context.updateFormState = vitest.fn();
|
|
29
33
|
const api = createFormsApi()(container.context) as any;
|
|
30
34
|
const create = api.createForm({
|
|
31
35
|
emptyData: {},
|
package/src/types.test.tsx
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
2
4
|
import * as React from 'react';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import create from 'zustand';
|
|
6
|
+
import { describe, it, expect, vitest, afterEach } from 'vitest';
|
|
7
|
+
import { StateContext, SwitchErrorInfo } from 'piral-core';
|
|
8
|
+
import { render, cleanup } from '@testing-library/react';
|
|
6
9
|
import './types';
|
|
7
10
|
|
|
8
11
|
const FormErrorInfo = () => <div role="form_error" />;
|
|
@@ -18,13 +21,20 @@ const mockState = {
|
|
|
18
21
|
})),
|
|
19
22
|
};
|
|
20
23
|
|
|
21
|
-
(
|
|
24
|
+
vitest.mock('react', async () => ({
|
|
25
|
+
...(await vitest.importActual('react') as any),
|
|
26
|
+
useMemo: (cb) => cb(),
|
|
27
|
+
}));
|
|
22
28
|
|
|
23
29
|
describe('Extended Error Info Component for Forms', () => {
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
cleanup();
|
|
32
|
+
});
|
|
33
|
+
|
|
24
34
|
it('renders the switch-case in the form error case', () => {
|
|
25
35
|
const node = render(
|
|
26
36
|
<StateContext.Provider value={mockState as any}>
|
|
27
|
-
<
|
|
37
|
+
<SwitchErrorInfo type="form" error="foo" />
|
|
28
38
|
</StateContext.Provider>,
|
|
29
39
|
);
|
|
30
40
|
expect(node.getAllByRole('form_error').length).toBe(1);
|
package/src/useForm.test.ts
CHANGED
|
@@ -1,438 +1,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
5
|
+
import { describe, it, expect, vitest, beforeEach } from 'vitest';
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
(React as any).useEffect = jest.fn((cb) => cb());
|
|
7
|
+
vitest.mock('react');
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const setStateFake = jest.fn();
|
|
9
|
-
const useAction = jest.fn(() => setStateFake);
|
|
9
|
+
(React as any).useState = vitest.fn((idOrFn) => [typeof idOrFn === 'function' ? idOrFn() : idOrFn]);
|
|
10
|
+
(React as any).useEffect = vitest.fn((cb) => cb());
|
|
10
11
|
|
|
11
|
-
const
|
|
12
|
-
|
|
12
|
+
const useGlobalState = vitest.fn();
|
|
13
|
+
const usePrompt = vitest.fn();
|
|
14
|
+
const setStateFake = vitest.fn();
|
|
15
|
+
const useAction = vitest.fn(() => setStateFake);
|
|
16
|
+
|
|
17
|
+
vitest.mock('piral-core', async () => ({
|
|
18
|
+
...((await vitest.importActual('piral-core')) as any),
|
|
13
19
|
useGlobalState,
|
|
14
20
|
usePrompt,
|
|
15
21
|
useAction,
|
|
16
22
|
}));
|
|
17
23
|
|
|
24
|
+
const testOptions = {
|
|
25
|
+
timeout: 30000,
|
|
26
|
+
};
|
|
27
|
+
|
|
18
28
|
describe('Form Hook Module', () => {
|
|
19
29
|
beforeEach(() => {
|
|
20
30
|
setStateFake.mockReset();
|
|
21
31
|
});
|
|
22
32
|
|
|
23
|
-
it(
|
|
24
|
-
|
|
33
|
+
it(
|
|
34
|
+
'Returns the current data and not changed initially',
|
|
35
|
+
async () => {
|
|
36
|
+
const { useForm } = await import('./useForm');
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
39
|
+
select({
|
|
40
|
+
forms: {
|
|
41
|
+
foo: {
|
|
42
|
+
changed: false,
|
|
43
|
+
currentData: {},
|
|
44
|
+
initialData: {},
|
|
45
|
+
submitting: false,
|
|
46
|
+
error: undefined,
|
|
47
|
+
},
|
|
35
48
|
},
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
);
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
39
51
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
const options = {
|
|
53
|
+
wait: false,
|
|
54
|
+
silent: false,
|
|
55
|
+
message: '',
|
|
56
|
+
onSubmit() {
|
|
57
|
+
return Promise.resolve();
|
|
58
|
+
},
|
|
59
|
+
onChange: undefined,
|
|
60
|
+
emptyData: {},
|
|
61
|
+
};
|
|
62
|
+
const { changed, submitting, formData } = useForm({}, undefined as any, options, 'foo');
|
|
63
|
+
expect(changed).toBeFalsy();
|
|
64
|
+
expect(submitting).toBeFalsy();
|
|
65
|
+
expect(formData).toEqual({});
|
|
66
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(3);
|
|
67
|
+
},
|
|
68
|
+
testOptions,
|
|
69
|
+
);
|
|
56
70
|
|
|
57
|
-
it(
|
|
58
|
-
|
|
71
|
+
it(
|
|
72
|
+
'Generates a new id if the old one is not provided',
|
|
73
|
+
async () => {
|
|
74
|
+
const { useForm } = await import('./useForm');
|
|
59
75
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
77
|
+
select({
|
|
78
|
+
forms: {},
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
const options = {
|
|
82
|
+
wait: false,
|
|
83
|
+
silent: false,
|
|
84
|
+
message: '',
|
|
85
|
+
onSubmit() {
|
|
86
|
+
return Promise.resolve();
|
|
87
|
+
},
|
|
88
|
+
onChange: undefined,
|
|
89
|
+
emptyData: {},
|
|
90
|
+
};
|
|
91
|
+
const { changed, submitting, formData } = useForm({}, undefined as any, options);
|
|
92
|
+
expect(changed).toBeFalsy();
|
|
93
|
+
expect(submitting).toBeFalsy();
|
|
94
|
+
expect(formData).toEqual({});
|
|
95
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
96
|
+
},
|
|
97
|
+
testOptions,
|
|
98
|
+
);
|
|
81
99
|
|
|
82
|
-
it(
|
|
83
|
-
|
|
84
|
-
|
|
100
|
+
it(
|
|
101
|
+
'Submit with no changed data does nothing',
|
|
102
|
+
async () => {
|
|
103
|
+
const { useForm } = await import('./useForm');
|
|
104
|
+
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
85
105
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
107
|
+
select({
|
|
108
|
+
forms: {},
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
const options = {
|
|
112
|
+
wait: false,
|
|
113
|
+
silent: false,
|
|
114
|
+
message: '',
|
|
115
|
+
onSubmit,
|
|
116
|
+
onChange: undefined,
|
|
117
|
+
emptyData: {},
|
|
118
|
+
};
|
|
119
|
+
const { changed, submitting, formData, submit } = useForm({}, undefined as any, options);
|
|
120
|
+
submit();
|
|
121
|
+
expect(changed).toBeFalsy();
|
|
122
|
+
expect(submitting).toBeFalsy();
|
|
123
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
124
|
+
expect(formData).toEqual({});
|
|
125
|
+
expect(setStateFake.mock.calls[0][0].length).toBe(36);
|
|
126
|
+
},
|
|
127
|
+
testOptions,
|
|
128
|
+
);
|
|
107
129
|
|
|
108
|
-
it(
|
|
109
|
-
|
|
110
|
-
|
|
130
|
+
it(
|
|
131
|
+
'Submit with changed data submits successfully',
|
|
132
|
+
async () => {
|
|
133
|
+
const { useForm } = await import('./useForm');
|
|
134
|
+
const onSubmit = vitest.fn(() => Promise.resolve());
|
|
111
135
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
136
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
137
|
+
select({
|
|
138
|
+
forms: {
|
|
139
|
+
foo: {
|
|
140
|
+
changed: true,
|
|
141
|
+
currentData: {
|
|
142
|
+
a: 'foo',
|
|
143
|
+
},
|
|
144
|
+
initialData: {
|
|
145
|
+
a: '',
|
|
146
|
+
},
|
|
147
|
+
submitting: false,
|
|
148
|
+
error: undefined,
|
|
122
149
|
},
|
|
123
|
-
submitting: false,
|
|
124
|
-
error: undefined,
|
|
125
150
|
},
|
|
126
|
-
},
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
151
|
+
}),
|
|
152
|
+
);
|
|
153
|
+
const options = {
|
|
154
|
+
wait: false,
|
|
155
|
+
silent: false,
|
|
156
|
+
message: '',
|
|
157
|
+
onSubmit,
|
|
158
|
+
onChange: undefined,
|
|
159
|
+
emptyData: {},
|
|
160
|
+
};
|
|
161
|
+
const { changed, formData, submit } = useForm({}, undefined as any, options, 'foo');
|
|
162
|
+
const preventDefault = vitest.fn();
|
|
163
|
+
(submit as any)({ preventDefault });
|
|
164
|
+
expect(changed).toBeTruthy();
|
|
165
|
+
expect(preventDefault).toBeCalled();
|
|
166
|
+
expect(onSubmit).toHaveBeenCalledWith({
|
|
167
|
+
a: 'foo',
|
|
168
|
+
});
|
|
169
|
+
expect(formData).toEqual({
|
|
170
|
+
a: 'foo',
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
testOptions,
|
|
174
|
+
);
|
|
149
175
|
|
|
150
|
-
it(
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
it(
|
|
177
|
+
'Submit with changed data running into an error',
|
|
178
|
+
async () => {
|
|
179
|
+
const { useForm } = await import('./useForm');
|
|
180
|
+
const onSubmit = vitest.fn(() => Promise.reject('My error'));
|
|
153
181
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
182
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
183
|
+
select({
|
|
184
|
+
forms: {
|
|
185
|
+
foo: {
|
|
186
|
+
changed: true,
|
|
187
|
+
currentData: {
|
|
188
|
+
a: 'foo',
|
|
189
|
+
},
|
|
190
|
+
initialData: {
|
|
191
|
+
a: '',
|
|
192
|
+
},
|
|
193
|
+
submitting: false,
|
|
194
|
+
error: undefined,
|
|
164
195
|
},
|
|
165
|
-
submitting: false,
|
|
166
|
-
error: undefined,
|
|
167
196
|
},
|
|
168
|
-
},
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
const options = {
|
|
200
|
+
wait: false,
|
|
201
|
+
silent: false,
|
|
202
|
+
message: '',
|
|
203
|
+
onSubmit,
|
|
204
|
+
onChange: undefined,
|
|
205
|
+
emptyData: {},
|
|
206
|
+
};
|
|
207
|
+
const { changed, formData, submit } = useForm({}, undefined as any, options, 'foo');
|
|
208
|
+
submit();
|
|
209
|
+
expect(changed).toBeTruthy();
|
|
210
|
+
expect(onSubmit).toHaveBeenCalledWith({
|
|
211
|
+
a: 'foo',
|
|
212
|
+
});
|
|
213
|
+
expect(formData).toEqual({
|
|
214
|
+
a: 'foo',
|
|
215
|
+
});
|
|
216
|
+
},
|
|
217
|
+
testOptions,
|
|
218
|
+
);
|
|
189
219
|
|
|
190
|
-
it(
|
|
191
|
-
|
|
220
|
+
it(
|
|
221
|
+
'Sets new data on changeForm',
|
|
222
|
+
async () => {
|
|
223
|
+
const { useForm } = await import('./useForm');
|
|
192
224
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
},
|
|
205
|
-
onChange: undefined,
|
|
206
|
-
emptyData: {},
|
|
207
|
-
};
|
|
208
|
-
const { changeForm } = useForm({}, undefined, options, 'id');
|
|
209
|
-
changeForm({
|
|
210
|
-
target: {
|
|
211
|
-
name: 'foo',
|
|
212
|
-
value: 'bar',
|
|
213
|
-
},
|
|
214
|
-
} as any);
|
|
215
|
-
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
216
|
-
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
217
|
-
2,
|
|
218
|
-
'id',
|
|
219
|
-
{
|
|
220
|
-
active: true,
|
|
221
|
-
currentData: {},
|
|
222
|
-
initialData: {},
|
|
223
|
-
changed: false,
|
|
224
|
-
submitting: false,
|
|
225
|
-
error: undefined,
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
currentData: {
|
|
229
|
-
foo: 'bar',
|
|
225
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
226
|
+
select({
|
|
227
|
+
forms: {},
|
|
228
|
+
}),
|
|
229
|
+
);
|
|
230
|
+
const options = {
|
|
231
|
+
wait: false,
|
|
232
|
+
silent: false,
|
|
233
|
+
message: '',
|
|
234
|
+
onSubmit() {
|
|
235
|
+
return Promise.resolve();
|
|
230
236
|
},
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
237
|
+
onChange: undefined,
|
|
238
|
+
emptyData: {},
|
|
239
|
+
};
|
|
240
|
+
const { changeForm } = useForm({}, undefined as any, options, 'id');
|
|
241
|
+
changeForm({
|
|
242
|
+
target: {
|
|
243
|
+
name: 'foo',
|
|
244
|
+
value: 'bar',
|
|
245
|
+
},
|
|
246
|
+
} as any);
|
|
247
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
248
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
249
|
+
2,
|
|
250
|
+
'id',
|
|
251
|
+
{
|
|
252
|
+
active: true,
|
|
253
|
+
currentData: {},
|
|
254
|
+
initialData: {},
|
|
255
|
+
changed: false,
|
|
256
|
+
submitting: false,
|
|
257
|
+
error: undefined,
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
currentData: {
|
|
261
|
+
foo: 'bar',
|
|
262
|
+
},
|
|
263
|
+
changed: true,
|
|
264
|
+
error: undefined,
|
|
265
|
+
},
|
|
266
|
+
);
|
|
267
|
+
},
|
|
268
|
+
testOptions,
|
|
269
|
+
);
|
|
236
270
|
|
|
237
|
-
it(
|
|
238
|
-
|
|
271
|
+
it(
|
|
272
|
+
'Sets new data on setFormData',
|
|
273
|
+
async () => {
|
|
274
|
+
const { useForm } = await import('./useForm');
|
|
239
275
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
},
|
|
252
|
-
onChange: undefined,
|
|
253
|
-
emptyData: {},
|
|
254
|
-
};
|
|
255
|
-
const { setFormData } = useForm({}, undefined, options, 'id');
|
|
256
|
-
setFormData({
|
|
257
|
-
foo: 'a',
|
|
258
|
-
bar: 'b',
|
|
259
|
-
} as any);
|
|
260
|
-
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
261
|
-
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
262
|
-
2,
|
|
263
|
-
'id',
|
|
264
|
-
{
|
|
265
|
-
active: true,
|
|
266
|
-
currentData: {},
|
|
267
|
-
initialData: {},
|
|
268
|
-
changed: false,
|
|
269
|
-
submitting: false,
|
|
270
|
-
error: undefined,
|
|
271
|
-
},
|
|
272
|
-
{
|
|
273
|
-
currentData: {
|
|
274
|
-
foo: 'a',
|
|
275
|
-
bar: 'b',
|
|
276
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
277
|
+
select({
|
|
278
|
+
forms: {},
|
|
279
|
+
}),
|
|
280
|
+
);
|
|
281
|
+
const options = {
|
|
282
|
+
wait: false,
|
|
283
|
+
silent: false,
|
|
284
|
+
message: '',
|
|
285
|
+
onSubmit() {
|
|
286
|
+
return Promise.resolve();
|
|
276
287
|
},
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
|
|
288
|
+
onChange: undefined,
|
|
289
|
+
emptyData: {},
|
|
290
|
+
};
|
|
291
|
+
const { setFormData } = useForm({}, undefined as any, options, 'id');
|
|
292
|
+
setFormData({
|
|
293
|
+
foo: 'a',
|
|
294
|
+
bar: 'b',
|
|
295
|
+
} as any);
|
|
296
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
297
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
298
|
+
2,
|
|
299
|
+
'id',
|
|
300
|
+
{
|
|
301
|
+
active: true,
|
|
302
|
+
currentData: {},
|
|
303
|
+
initialData: {},
|
|
304
|
+
changed: false,
|
|
305
|
+
submitting: false,
|
|
306
|
+
error: undefined,
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
currentData: {
|
|
310
|
+
foo: 'a',
|
|
311
|
+
bar: 'b',
|
|
312
|
+
},
|
|
313
|
+
changed: true,
|
|
314
|
+
error: undefined,
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
},
|
|
318
|
+
testOptions,
|
|
319
|
+
);
|
|
282
320
|
|
|
283
|
-
it(
|
|
284
|
-
|
|
321
|
+
it(
|
|
322
|
+
'Resets changes to initial data',
|
|
323
|
+
async () => {
|
|
324
|
+
const { useForm } = await import('./useForm');
|
|
285
325
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
326
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
327
|
+
select({
|
|
328
|
+
forms: {
|
|
329
|
+
id: {
|
|
330
|
+
active: true,
|
|
331
|
+
changed: true,
|
|
332
|
+
currentData: {
|
|
333
|
+
a: 'foo',
|
|
334
|
+
},
|
|
335
|
+
initialData: {
|
|
336
|
+
a: '',
|
|
337
|
+
},
|
|
338
|
+
submitting: false,
|
|
339
|
+
error: undefined,
|
|
294
340
|
},
|
|
295
|
-
initialData: {
|
|
296
|
-
a: '',
|
|
297
|
-
},
|
|
298
|
-
submitting: false,
|
|
299
|
-
error: undefined,
|
|
300
341
|
},
|
|
342
|
+
}),
|
|
343
|
+
);
|
|
344
|
+
const options = {
|
|
345
|
+
wait: false,
|
|
346
|
+
silent: false,
|
|
347
|
+
message: '',
|
|
348
|
+
onSubmit() {
|
|
349
|
+
return Promise.resolve();
|
|
301
350
|
},
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
currentData: {
|
|
323
|
-
a: 'foo',
|
|
324
|
-
},
|
|
325
|
-
initialData: {
|
|
326
|
-
a: '',
|
|
351
|
+
onChange: undefined,
|
|
352
|
+
emptyData: {},
|
|
353
|
+
};
|
|
354
|
+
const { reset } = useForm({}, undefined as any, options, 'id');
|
|
355
|
+
reset();
|
|
356
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
357
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
358
|
+
2,
|
|
359
|
+
'id',
|
|
360
|
+
{
|
|
361
|
+
active: true,
|
|
362
|
+
currentData: {
|
|
363
|
+
a: 'foo',
|
|
364
|
+
},
|
|
365
|
+
initialData: {
|
|
366
|
+
a: '',
|
|
367
|
+
},
|
|
368
|
+
changed: true,
|
|
369
|
+
submitting: false,
|
|
370
|
+
error: undefined,
|
|
327
371
|
},
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
a: '',
|
|
372
|
+
{
|
|
373
|
+
currentData: {
|
|
374
|
+
a: '',
|
|
375
|
+
},
|
|
376
|
+
changed: false,
|
|
377
|
+
error: undefined,
|
|
335
378
|
},
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
});
|
|
379
|
+
);
|
|
380
|
+
},
|
|
381
|
+
testOptions,
|
|
382
|
+
);
|
|
341
383
|
|
|
342
|
-
it(
|
|
343
|
-
|
|
344
|
-
|
|
384
|
+
it(
|
|
385
|
+
'onChange should be triggered with full data set',
|
|
386
|
+
async () => {
|
|
387
|
+
const { useForm } = await import('./useForm');
|
|
388
|
+
const onChange = vitest.fn((data) => Promise.resolve(data));
|
|
345
389
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
390
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
391
|
+
select({
|
|
392
|
+
forms: {
|
|
393
|
+
id: {
|
|
394
|
+
active: true,
|
|
395
|
+
changed: true,
|
|
396
|
+
currentData: {
|
|
397
|
+
a: 'foo',
|
|
398
|
+
},
|
|
399
|
+
initialData: {
|
|
400
|
+
a: '',
|
|
401
|
+
},
|
|
402
|
+
submitting: false,
|
|
403
|
+
error: undefined,
|
|
357
404
|
},
|
|
358
|
-
submitting: false,
|
|
359
|
-
error: undefined,
|
|
360
405
|
},
|
|
406
|
+
}),
|
|
407
|
+
);
|
|
408
|
+
const options = {
|
|
409
|
+
wait: false,
|
|
410
|
+
silent: false,
|
|
411
|
+
message: '',
|
|
412
|
+
onChange,
|
|
413
|
+
onSubmit() {
|
|
414
|
+
return Promise.resolve();
|
|
361
415
|
},
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
return Promise.resolve();
|
|
371
|
-
},
|
|
372
|
-
emptyData: {},
|
|
373
|
-
};
|
|
374
|
-
const { setFormData } = useForm({}, undefined, options, 'id');
|
|
375
|
-
setFormData({ a: 'b' });
|
|
376
|
-
expect(onChange).toHaveBeenCalledWith({ a: 'b' });
|
|
377
|
-
});
|
|
416
|
+
emptyData: {},
|
|
417
|
+
};
|
|
418
|
+
const { setFormData } = useForm({}, undefined as any, options, 'id');
|
|
419
|
+
setFormData({ a: 'b' });
|
|
420
|
+
expect(onChange).toHaveBeenCalledWith({ a: 'b' });
|
|
421
|
+
},
|
|
422
|
+
testOptions,
|
|
423
|
+
);
|
|
378
424
|
|
|
379
|
-
it(
|
|
380
|
-
|
|
381
|
-
|
|
425
|
+
it(
|
|
426
|
+
'onChange which fails should be handled gracefully',
|
|
427
|
+
async () => {
|
|
428
|
+
const { useForm } = await import('./useForm');
|
|
429
|
+
const onChange = vitest.fn((data) => Promise.reject('my error'));
|
|
382
430
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
431
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
432
|
+
select({
|
|
433
|
+
forms: {},
|
|
434
|
+
}),
|
|
435
|
+
);
|
|
436
|
+
const options = {
|
|
437
|
+
wait: false,
|
|
438
|
+
silent: false,
|
|
439
|
+
message: '',
|
|
440
|
+
onChange,
|
|
441
|
+
onSubmit() {
|
|
442
|
+
return Promise.resolve();
|
|
443
|
+
},
|
|
444
|
+
emptyData: {},
|
|
445
|
+
};
|
|
446
|
+
const { setFormData } = useForm({}, undefined as any, options);
|
|
447
|
+
setFormData({ a: 'b' });
|
|
448
|
+
expect(onChange).toHaveBeenCalledWith({ a: 'b' });
|
|
449
|
+
},
|
|
450
|
+
testOptions,
|
|
451
|
+
);
|
|
402
452
|
|
|
403
|
-
it(
|
|
404
|
-
|
|
405
|
-
|
|
453
|
+
it(
|
|
454
|
+
'cleanup sets active to false',
|
|
455
|
+
async () => {
|
|
456
|
+
const { useForm } = await import('./useForm');
|
|
457
|
+
(React as any).useEffect = vitest.fn((cb) => cb()());
|
|
406
458
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
459
|
+
useGlobalState.mockImplementation((select: any) =>
|
|
460
|
+
select({
|
|
461
|
+
forms: {},
|
|
462
|
+
}),
|
|
463
|
+
);
|
|
464
|
+
const options = {
|
|
465
|
+
wait: false,
|
|
466
|
+
silent: false,
|
|
467
|
+
message: '',
|
|
468
|
+
onChange: undefined,
|
|
469
|
+
onSubmit() {
|
|
470
|
+
return Promise.resolve();
|
|
471
|
+
},
|
|
472
|
+
emptyData: {},
|
|
473
|
+
};
|
|
474
|
+
useForm({}, undefined as any, options, 'id');
|
|
475
|
+
expect(setStateFake).toHaveBeenCalledTimes(2);
|
|
476
|
+
expect(setStateFake).toHaveBeenNthCalledWith(
|
|
477
|
+
2,
|
|
478
|
+
'id',
|
|
479
|
+
{
|
|
480
|
+
active: true,
|
|
481
|
+
changed: false,
|
|
482
|
+
currentData: {},
|
|
483
|
+
error: undefined,
|
|
484
|
+
initialData: {},
|
|
485
|
+
submitting: false,
|
|
486
|
+
},
|
|
487
|
+
{ active: false },
|
|
488
|
+
);
|
|
489
|
+
},
|
|
490
|
+
testOptions,
|
|
491
|
+
);
|
|
438
492
|
});
|
package/src/usePromise.test.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
import { usePromise } from './usePromise';
|
|
3
7
|
|
|
4
|
-
|
|
8
|
+
vitest.mock('react');
|
|
5
9
|
|
|
6
10
|
describe('Promise Module', () => {
|
|
7
11
|
it('directly reports loading', () => {
|
|
8
|
-
const usedEffect =
|
|
9
|
-
const setState =
|
|
12
|
+
const usedEffect = vitest.fn();
|
|
13
|
+
const setState = vitest.fn();
|
|
10
14
|
(React as any).useEffect = usedEffect;
|
|
11
15
|
(React as any).useState = (current) => [current, setState];
|
|
12
16
|
const result = usePromise(() => Promise.resolve());
|
|
@@ -18,8 +22,8 @@ describe('Promise Module', () => {
|
|
|
18
22
|
});
|
|
19
23
|
|
|
20
24
|
it('reports loading after the effect is done', () => {
|
|
21
|
-
const usedEffect =
|
|
22
|
-
const setState =
|
|
25
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
26
|
+
const setState = vitest.fn();
|
|
23
27
|
(React as any).useEffect = usedEffect;
|
|
24
28
|
(React as any).useState = (current) => [current, setState];
|
|
25
29
|
const result = usePromise(() => Promise.resolve());
|
|
@@ -31,8 +35,8 @@ describe('Promise Module', () => {
|
|
|
31
35
|
});
|
|
32
36
|
|
|
33
37
|
it('reports done when the loading finished', async () => {
|
|
34
|
-
const usedEffect =
|
|
35
|
-
const setState =
|
|
38
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
39
|
+
const setState = vitest.fn();
|
|
36
40
|
(React as any).useEffect = usedEffect;
|
|
37
41
|
(React as any).useState = (current) => [current, setState];
|
|
38
42
|
usePromise(() => Promise.resolve('123'));
|
|
@@ -48,8 +52,8 @@ describe('Promise Module', () => {
|
|
|
48
52
|
});
|
|
49
53
|
|
|
50
54
|
it('reports error when the loading finished', async () => {
|
|
51
|
-
const usedEffect =
|
|
52
|
-
const setState =
|
|
55
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
56
|
+
const setState = vitest.fn();
|
|
53
57
|
(React as any).useEffect = usedEffect;
|
|
54
58
|
(React as any).useState = (current) => [current, setState];
|
|
55
59
|
usePromise(() => Promise.reject('123'));
|
|
@@ -65,8 +69,8 @@ describe('Promise Module', () => {
|
|
|
65
69
|
});
|
|
66
70
|
|
|
67
71
|
it('does not set the state on cancel', async () => {
|
|
68
|
-
const usedEffect =
|
|
69
|
-
const setState =
|
|
72
|
+
const usedEffect = vitest.fn((fn) => fn()());
|
|
73
|
+
const setState = vitest.fn();
|
|
70
74
|
(React as any).useEffect = usedEffect;
|
|
71
75
|
(React as any).useState = (current) => [current, setState];
|
|
72
76
|
usePromise(() => Promise.reject('123'));
|
package/src/usePrompt.test.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
import { usePrompt } from './usePrompt';
|
|
3
7
|
|
|
8
|
+
vitest.mock('react');
|
|
9
|
+
|
|
4
10
|
describe('Prompt Hook Module', () => {
|
|
5
11
|
it('does not do anything when its not active', () => {
|
|
6
12
|
const originalAdd = window.addEventListener;
|
|
7
13
|
const originalRemove = window.addEventListener;
|
|
8
|
-
window.addEventListener =
|
|
9
|
-
window.removeEventListener =
|
|
10
|
-
const usedEffect =
|
|
14
|
+
window.addEventListener = vitest.fn();
|
|
15
|
+
window.removeEventListener = vitest.fn();
|
|
16
|
+
const usedEffect = vitest.fn();
|
|
11
17
|
const history = {
|
|
12
|
-
block:
|
|
13
|
-
listen:
|
|
18
|
+
block: vitest.fn(() => () => {}),
|
|
19
|
+
listen: vitest.fn(() => () => {}),
|
|
14
20
|
};
|
|
15
21
|
(React as any).useEffect = usedEffect;
|
|
16
22
|
usePrompt(false, history as any, 'blocked');
|
|
@@ -27,12 +33,12 @@ describe('Prompt Hook Module', () => {
|
|
|
27
33
|
it('does integrate blockers when its active', () => {
|
|
28
34
|
const originalAdd = window.addEventListener;
|
|
29
35
|
const originalRemove = window.addEventListener;
|
|
30
|
-
window.addEventListener =
|
|
31
|
-
window.removeEventListener =
|
|
32
|
-
const usedEffect =
|
|
36
|
+
window.addEventListener = vitest.fn();
|
|
37
|
+
window.removeEventListener = vitest.fn();
|
|
38
|
+
const usedEffect = vitest.fn();
|
|
33
39
|
const history = {
|
|
34
|
-
block:
|
|
35
|
-
listen:
|
|
40
|
+
block: vitest.fn(() => () => {}),
|
|
41
|
+
listen: vitest.fn(() => () => {}),
|
|
36
42
|
};
|
|
37
43
|
(React as any).useEffect = usedEffect;
|
|
38
44
|
usePrompt(true, history as any, 'blocked');
|
|
@@ -49,12 +55,12 @@ describe('Prompt Hook Module', () => {
|
|
|
49
55
|
it('returns with the provided message when the blocker is triggered', () => {
|
|
50
56
|
const originalAdd = window.addEventListener;
|
|
51
57
|
const originalRemove = window.addEventListener;
|
|
52
|
-
window.addEventListener =
|
|
53
|
-
window.removeEventListener =
|
|
54
|
-
const usedEffect =
|
|
58
|
+
window.addEventListener = vitest.fn();
|
|
59
|
+
window.removeEventListener = vitest.fn();
|
|
60
|
+
const usedEffect = vitest.fn();
|
|
55
61
|
const history = {
|
|
56
|
-
block:
|
|
57
|
-
listen:
|
|
62
|
+
block: vitest.fn(() => () => {}),
|
|
63
|
+
listen: vitest.fn(() => () => {}),
|
|
58
64
|
};
|
|
59
65
|
(React as any).useEffect = usedEffect;
|
|
60
66
|
usePrompt(true, history as any, 'blocked');
|
|
@@ -69,12 +75,12 @@ describe('Prompt Hook Module', () => {
|
|
|
69
75
|
it('returns with the message from the callback when the blocker is triggered', () => {
|
|
70
76
|
const originalAdd = window.addEventListener;
|
|
71
77
|
const originalRemove = window.addEventListener;
|
|
72
|
-
window.addEventListener =
|
|
73
|
-
window.removeEventListener =
|
|
74
|
-
const usedEffect =
|
|
78
|
+
window.addEventListener = vitest.fn();
|
|
79
|
+
window.removeEventListener = vitest.fn();
|
|
80
|
+
const usedEffect = vitest.fn();
|
|
75
81
|
const history = {
|
|
76
|
-
block:
|
|
77
|
-
listen:
|
|
82
|
+
block: vitest.fn(() => () => {}),
|
|
83
|
+
listen: vitest.fn(() => () => {}),
|
|
78
84
|
};
|
|
79
85
|
(React as any).useEffect = usedEffect;
|
|
80
86
|
usePrompt(true, history as any, () => 'block!');
|
package/src/withForm.test.tsx
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
2
5
|
import * as piralCore from 'piral-core';
|
|
3
6
|
import * as useForm from './useForm';
|
|
4
7
|
import * as usePromise from './usePromise';
|
|
8
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
5
9
|
import { MemoryRouter } from 'react-router-dom';
|
|
6
10
|
import { render } from '@testing-library/react';
|
|
7
11
|
import { withForm } from './withForm';
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
vitest.mock('piral-core');
|
|
14
|
+
vitest.mock('./useForm');
|
|
15
|
+
vitest.mock('./usePromise');
|
|
12
16
|
|
|
13
17
|
const mountWithRouter = (node, url = '/') =>
|
|
14
18
|
render(
|
|
@@ -32,10 +36,10 @@ ErrorComponent.displayName = 'ErrorComponent';
|
|
|
32
36
|
describe('withForm Module', () => {
|
|
33
37
|
it('shows error component if nothing is loading and no data is available', () => {
|
|
34
38
|
const options: any = { emptyData: {} };
|
|
35
|
-
const usedForm =
|
|
39
|
+
const usedForm = vitest.fn(() => ({
|
|
36
40
|
submit() {},
|
|
37
41
|
}));
|
|
38
|
-
const usedPromise =
|
|
42
|
+
const usedPromise = vitest.fn(() => ({
|
|
39
43
|
loading: false,
|
|
40
44
|
data: undefined,
|
|
41
45
|
error: undefined,
|
|
@@ -49,10 +53,10 @@ describe('withForm Module', () => {
|
|
|
49
53
|
|
|
50
54
|
it('shows data component if nothing is loading and data is available', () => {
|
|
51
55
|
const options: any = { emptyData: {} };
|
|
52
|
-
const usedForm =
|
|
56
|
+
const usedForm = vitest.fn(() => ({
|
|
53
57
|
submit() {},
|
|
54
58
|
}));
|
|
55
|
-
const usedPromise =
|
|
59
|
+
const usedPromise = vitest.fn(() => ({
|
|
56
60
|
loading: false,
|
|
57
61
|
data: {},
|
|
58
62
|
error: undefined,
|
|
@@ -66,10 +70,10 @@ describe('withForm Module', () => {
|
|
|
66
70
|
|
|
67
71
|
it('shows loading component if it is loading', () => {
|
|
68
72
|
const options: any = { emptyData: {} };
|
|
69
|
-
const usedForm =
|
|
73
|
+
const usedForm = vitest.fn(() => ({
|
|
70
74
|
submit() {},
|
|
71
75
|
}));
|
|
72
|
-
const usedPromise =
|
|
76
|
+
const usedPromise = vitest.fn(() => ({
|
|
73
77
|
loading: true,
|
|
74
78
|
data: undefined,
|
|
75
79
|
error: undefined,
|
|
@@ -82,11 +86,11 @@ describe('withForm Module', () => {
|
|
|
82
86
|
});
|
|
83
87
|
|
|
84
88
|
it('calls load data if its there', () => {
|
|
85
|
-
const loadData =
|
|
86
|
-
const usedForm =
|
|
89
|
+
const loadData = vitest.fn(() => () => Promise.resolve({}));
|
|
90
|
+
const usedForm = vitest.fn(() => ({
|
|
87
91
|
submit() {},
|
|
88
92
|
}));
|
|
89
|
-
const usedPromise =
|
|
93
|
+
const usedPromise = vitest.fn((fn) => {
|
|
90
94
|
fn();
|
|
91
95
|
return {
|
|
92
96
|
loading: false,
|
|
@@ -105,10 +109,10 @@ describe('withForm Module', () => {
|
|
|
105
109
|
|
|
106
110
|
it('does not call load data if its missing', () => {
|
|
107
111
|
const loadData = undefined;
|
|
108
|
-
const usedForm =
|
|
112
|
+
const usedForm = vitest.fn(() => ({
|
|
109
113
|
submit() {},
|
|
110
114
|
}));
|
|
111
|
-
const usedPromise =
|
|
115
|
+
const usedPromise = vitest.fn((fn) => {
|
|
112
116
|
const data = fn();
|
|
113
117
|
return {
|
|
114
118
|
loading: false,
|