piral-forms 1.3.3-beta.6187 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "piral-forms",
3
- "version": "1.3.3-beta.6187",
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.6187",
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": "3da0780821a0aeef693b36d08dcf45eeb462d599"
70
+ "gitHead": "6e9f4e6f83514b5c38960ce015e073efc668f3d6"
71
71
  }
@@ -1,15 +1,35 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
1
4
  import create from 'zustand';
2
- import { createListener } from 'piral-base';
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,
@@ -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: jest.fn(),
13
- off: jest.fn(),
14
- emit: jest.fn(),
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 = jest.fn();
32
+ container.context.updateFormState = vitest.fn();
29
33
  const api = createFormsApi()(container.context) as any;
30
34
  const create = api.createForm({
31
35
  emptyData: {},
@@ -1,8 +1,11 @@
1
- import create from 'zustand';
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
2
4
  import * as React from 'react';
3
- import { StateContext } from 'piral-core';
4
- import { DefaultErrorInfo } from 'piral-core/lib/defaults/DefaultErrorInfo.js';
5
- import { render } from '@testing-library/react';
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
- (React as any).useMemo = (cb) => cb();
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
- <DefaultErrorInfo type="form" error="foo" />
37
+ <SwitchErrorInfo type="form" error="foo" />
28
38
  </StateContext.Provider>,
29
39
  );
30
40
  expect(node.getAllByRole('form_error').length).toBe(1);
@@ -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
- (React as any).useState = jest.fn((idOrFn) => [typeof idOrFn === 'function' ? idOrFn() : idOrFn]);
4
- (React as any).useEffect = jest.fn((cb) => cb());
7
+ vitest.mock('react');
5
8
 
6
- const useGlobalState = jest.fn();
7
- const usePrompt = jest.fn();
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 piralCore = jest.mock('piral-core', () => ({
12
- ...jest.requireActual('piral-core'),
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('Returns the current data and not changed initially', () => {
24
- const { useForm } = require('./useForm');
33
+ it(
34
+ 'Returns the current data and not changed initially',
35
+ async () => {
36
+ const { useForm } = await import('./useForm');
25
37
 
26
- useGlobalState.mockImplementation((select: any) =>
27
- select({
28
- forms: {
29
- foo: {
30
- changed: false,
31
- currentData: {},
32
- initialData: {},
33
- submitting: false,
34
- error: undefined,
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
- const options = {
41
- wait: false,
42
- silent: false,
43
- message: '',
44
- onSubmit() {
45
- return Promise.resolve();
46
- },
47
- onChange: undefined,
48
- emptyData: {},
49
- };
50
- const { changed, submitting, formData } = useForm({}, undefined, options, 'foo');
51
- expect(changed).toBeFalsy();
52
- expect(submitting).toBeFalsy();
53
- expect(formData).toEqual({});
54
- expect(setStateFake.mock.calls[0][0].length).toBe(3);
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('Generates a new id if the old one is not provided', () => {
58
- const { useForm } = require('./useForm');
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
- useGlobalState.mockImplementation((select: any) =>
61
- select({
62
- forms: {},
63
- }),
64
- );
65
- const options = {
66
- wait: false,
67
- silent: false,
68
- message: '',
69
- onSubmit() {
70
- return Promise.resolve();
71
- },
72
- onChange: undefined,
73
- emptyData: {},
74
- };
75
- const { changed, submitting, formData } = useForm({}, undefined, options);
76
- expect(changed).toBeFalsy();
77
- expect(submitting).toBeFalsy();
78
- expect(formData).toEqual({});
79
- expect(setStateFake.mock.calls[0][0].length).toBe(36);
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('Submit with no changed data does nothing', () => {
83
- const { useForm } = require('./useForm');
84
- const onSubmit = jest.fn(() => Promise.resolve());
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
- useGlobalState.mockImplementation((select: any) =>
87
- select({
88
- forms: {},
89
- }),
90
- );
91
- const options = {
92
- wait: false,
93
- silent: false,
94
- message: '',
95
- onSubmit,
96
- onChange: undefined,
97
- emptyData: {},
98
- };
99
- const { changed, submitting, formData, submit } = useForm({}, undefined, options);
100
- submit();
101
- expect(changed).toBeFalsy();
102
- expect(submitting).toBeFalsy();
103
- expect(onSubmit).not.toHaveBeenCalled();
104
- expect(formData).toEqual({});
105
- expect(setStateFake.mock.calls[0][0].length).toBe(36);
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('Submit with changed data submits successfully', () => {
109
- const { useForm } = require('./useForm');
110
- const onSubmit = jest.fn(() => Promise.resolve());
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
- useGlobalState.mockImplementation((select: any) =>
113
- select({
114
- forms: {
115
- foo: {
116
- changed: true,
117
- currentData: {
118
- a: 'foo',
119
- },
120
- initialData: {
121
- a: '',
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
- const options = {
130
- wait: false,
131
- silent: false,
132
- message: '',
133
- onSubmit,
134
- onChange: undefined,
135
- emptyData: {},
136
- };
137
- const { changed, formData, submit } = useForm({}, undefined, options, 'foo');
138
- const preventDefault = jest.fn();
139
- (submit as any)({ preventDefault });
140
- expect(changed).toBeTruthy();
141
- expect(preventDefault).toBeCalled();
142
- expect(onSubmit).toHaveBeenCalledWith({
143
- a: 'foo',
144
- });
145
- expect(formData).toEqual({
146
- a: 'foo',
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('Submit with changed data running into an error', () => {
151
- const { useForm } = require('./useForm');
152
- const onSubmit = jest.fn(() => Promise.reject('My error'));
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
- useGlobalState.mockImplementation((select: any) =>
155
- select({
156
- forms: {
157
- foo: {
158
- changed: true,
159
- currentData: {
160
- a: 'foo',
161
- },
162
- initialData: {
163
- a: '',
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
- const options = {
172
- wait: false,
173
- silent: false,
174
- message: '',
175
- onSubmit,
176
- onChange: undefined,
177
- emptyData: {},
178
- };
179
- const { changed, formData, submit } = useForm({}, undefined, options, 'foo');
180
- submit();
181
- expect(changed).toBeTruthy();
182
- expect(onSubmit).toHaveBeenCalledWith({
183
- a: 'foo',
184
- });
185
- expect(formData).toEqual({
186
- a: 'foo',
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('Sets new data on changeForm', () => {
191
- const { useForm } = require('./useForm');
220
+ it(
221
+ 'Sets new data on changeForm',
222
+ async () => {
223
+ const { useForm } = await import('./useForm');
192
224
 
193
- useGlobalState.mockImplementation((select: any) =>
194
- select({
195
- forms: {},
196
- }),
197
- );
198
- const options = {
199
- wait: false,
200
- silent: false,
201
- message: '',
202
- onSubmit() {
203
- return Promise.resolve();
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
- changed: true,
232
- error: undefined,
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('Sets new data on setFormData', () => {
238
- const { useForm } = require('./useForm');
271
+ it(
272
+ 'Sets new data on setFormData',
273
+ async () => {
274
+ const { useForm } = await import('./useForm');
239
275
 
240
- useGlobalState.mockImplementation((select: any) =>
241
- select({
242
- forms: {},
243
- }),
244
- );
245
- const options = {
246
- wait: false,
247
- silent: false,
248
- message: '',
249
- onSubmit() {
250
- return Promise.resolve();
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
- changed: true,
278
- error: undefined,
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('Resets changes to initial data', () => {
284
- const { useForm } = require('./useForm');
321
+ it(
322
+ 'Resets changes to initial data',
323
+ async () => {
324
+ const { useForm } = await import('./useForm');
285
325
 
286
- useGlobalState.mockImplementation((select: any) =>
287
- select({
288
- forms: {
289
- id: {
290
- active: true,
291
- changed: true,
292
- currentData: {
293
- a: 'foo',
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
- const options = {
305
- wait: false,
306
- silent: false,
307
- message: '',
308
- onSubmit() {
309
- return Promise.resolve();
310
- },
311
- onChange: undefined,
312
- emptyData: {},
313
- };
314
- const { reset } = useForm({}, undefined, options, 'id');
315
- reset();
316
- expect(setStateFake).toHaveBeenCalledTimes(2);
317
- expect(setStateFake).toHaveBeenNthCalledWith(
318
- 2,
319
- 'id',
320
- {
321
- active: true,
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
- changed: true,
329
- submitting: false,
330
- error: undefined,
331
- },
332
- {
333
- currentData: {
334
- a: '',
372
+ {
373
+ currentData: {
374
+ a: '',
375
+ },
376
+ changed: false,
377
+ error: undefined,
335
378
  },
336
- changed: false,
337
- error: undefined,
338
- },
339
- );
340
- });
379
+ );
380
+ },
381
+ testOptions,
382
+ );
341
383
 
342
- it('onChange should be triggered with full data set', () => {
343
- const { useForm } = require('./useForm');
344
- const onChange = jest.fn((data) => Promise.resolve(data));
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
- useGlobalState.mockImplementation((select: any) =>
347
- select({
348
- forms: {
349
- id: {
350
- active: true,
351
- changed: true,
352
- currentData: {
353
- a: 'foo',
354
- },
355
- initialData: {
356
- a: '',
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
- const options = {
365
- wait: false,
366
- silent: false,
367
- message: '',
368
- onChange,
369
- onSubmit() {
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('onChange which fails should be handled gracefully', () => {
380
- const { useForm } = require('./useForm');
381
- const onChange = jest.fn((data) => Promise.reject('my error'));
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
- useGlobalState.mockImplementation((select: any) =>
384
- select({
385
- forms: {},
386
- }),
387
- );
388
- const options = {
389
- wait: false,
390
- silent: false,
391
- message: '',
392
- onChange,
393
- onSubmit() {
394
- return Promise.resolve();
395
- },
396
- emptyData: {},
397
- };
398
- const { setFormData } = useForm({}, undefined, options);
399
- setFormData({ a: 'b' });
400
- expect(onChange).toHaveBeenCalledWith({ a: 'b' });
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('cleanup sets active to false', () => {
404
- const { useForm } = require('./useForm');
405
- (React as any).useEffect = jest.fn((cb) => cb()());
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
- useGlobalState.mockImplementation((select: any) =>
408
- select({
409
- forms: {},
410
- }),
411
- );
412
- const options = {
413
- wait: false,
414
- silent: false,
415
- message: '',
416
- onChange: undefined,
417
- onSubmit() {
418
- return Promise.resolve();
419
- },
420
- emptyData: {},
421
- };
422
- useForm({}, undefined, options, 'id');
423
- expect(setStateFake).toHaveBeenCalledTimes(2);
424
- expect(setStateFake).toHaveBeenNthCalledWith(
425
- 2,
426
- 'id',
427
- {
428
- active: true,
429
- changed: false,
430
- currentData: {},
431
- error: undefined,
432
- initialData: {},
433
- submitting: false,
434
- },
435
- { active: false },
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
  });
@@ -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
- jest.mock('react');
8
+ vitest.mock('react');
5
9
 
6
10
  describe('Promise Module', () => {
7
11
  it('directly reports loading', () => {
8
- const usedEffect = jest.fn();
9
- const setState = jest.fn();
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 = jest.fn((fn) => fn());
22
- const setState = jest.fn();
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 = jest.fn((fn) => fn());
35
- const setState = jest.fn();
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 = jest.fn((fn) => fn());
52
- const setState = jest.fn();
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 = jest.fn((fn) => fn()());
69
- const setState = jest.fn();
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'));
@@ -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 = jest.fn();
9
- window.removeEventListener = jest.fn();
10
- const usedEffect = jest.fn();
14
+ window.addEventListener = vitest.fn();
15
+ window.removeEventListener = vitest.fn();
16
+ const usedEffect = vitest.fn();
11
17
  const history = {
12
- block: jest.fn(() => () => {}),
13
- listen: jest.fn(() => () => {}),
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 = jest.fn();
31
- window.removeEventListener = jest.fn();
32
- const usedEffect = jest.fn();
36
+ window.addEventListener = vitest.fn();
37
+ window.removeEventListener = vitest.fn();
38
+ const usedEffect = vitest.fn();
33
39
  const history = {
34
- block: jest.fn(() => () => {}),
35
- listen: jest.fn(() => () => {}),
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 = jest.fn();
53
- window.removeEventListener = jest.fn();
54
- const usedEffect = jest.fn();
58
+ window.addEventListener = vitest.fn();
59
+ window.removeEventListener = vitest.fn();
60
+ const usedEffect = vitest.fn();
55
61
  const history = {
56
- block: jest.fn(() => () => {}),
57
- listen: jest.fn(() => () => {}),
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 = jest.fn();
73
- window.removeEventListener = jest.fn();
74
- const usedEffect = jest.fn();
78
+ window.addEventListener = vitest.fn();
79
+ window.removeEventListener = vitest.fn();
80
+ const usedEffect = vitest.fn();
75
81
  const history = {
76
- block: jest.fn(() => () => {}),
77
- listen: jest.fn(() => () => {}),
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!');
@@ -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
- jest.mock('piral-core');
10
- jest.mock('./useForm');
11
- jest.mock('./usePromise');
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 = jest.fn(() => ({
39
+ const usedForm = vitest.fn(() => ({
36
40
  submit() {},
37
41
  }));
38
- const usedPromise = jest.fn(() => ({
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 = jest.fn(() => ({
56
+ const usedForm = vitest.fn(() => ({
53
57
  submit() {},
54
58
  }));
55
- const usedPromise = jest.fn(() => ({
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 = jest.fn(() => ({
73
+ const usedForm = vitest.fn(() => ({
70
74
  submit() {},
71
75
  }));
72
- const usedPromise = jest.fn(() => ({
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 = jest.fn(() => () => Promise.resolve({}));
86
- const usedForm = jest.fn(() => ({
89
+ const loadData = vitest.fn(() => () => Promise.resolve({}));
90
+ const usedForm = vitest.fn(() => ({
87
91
  submit() {},
88
92
  }));
89
- const usedPromise = jest.fn((fn) => {
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 = jest.fn(() => ({
112
+ const usedForm = vitest.fn(() => ({
109
113
  submit() {},
110
114
  }));
111
- const usedPromise = jest.fn((fn) => {
115
+ const usedPromise = vitest.fn((fn) => {
112
116
  const data = fn();
113
117
  return {
114
118
  loading: false,