@elementor/store 0.8.4 → 0.8.5
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/CHANGELOG.md +6 -0
- package/package.json +10 -3
- package/src/__tests__/store.test.tsx +0 -328
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.8.5](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.4...@elementor/store@0.8.5) (2024-08-05)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- publish only necessary files to npm ([#226](https://github.com/elementor/elementor-packages/issues/226)) ([d808e2f](https://github.com/elementor/elementor-packages/commit/d808e2f60eb7ca2d7b8560d0b79c0e62c2f969a8))
|
|
11
|
+
|
|
6
12
|
## [0.8.4](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.3...@elementor/store@0.8.4) (2024-07-16)
|
|
7
13
|
|
|
8
14
|
**Note:** Version bump only for package @elementor/store
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/store",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://github.com/elementor/elementor-packages.git",
|
|
21
|
+
"url": "git+https://github.com/elementor/elementor-packages.git",
|
|
22
22
|
"directory": "packages/libs/store"
|
|
23
23
|
},
|
|
24
24
|
"bugs": {
|
|
@@ -27,6 +27,13 @@
|
|
|
27
27
|
"publishConfig": {
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
|
+
"files": [
|
|
31
|
+
"README.md",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"/dist",
|
|
34
|
+
"/src",
|
|
35
|
+
"!**/__tests__"
|
|
36
|
+
],
|
|
30
37
|
"scripts": {
|
|
31
38
|
"build": "tsup --config=../../tsup.build.ts",
|
|
32
39
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
@@ -38,5 +45,5 @@
|
|
|
38
45
|
"@reduxjs/toolkit": "^1.9.7",
|
|
39
46
|
"react-redux": "^8.1.3"
|
|
40
47
|
},
|
|
41
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "f4ca33da0842a29d83736d0a173633085edddaee"
|
|
42
49
|
}
|
|
@@ -1,328 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { PropsWithChildren } from 'react';
|
|
3
|
-
import { renderHook } from '@testing-library/react';
|
|
4
|
-
import {
|
|
5
|
-
__createStore,
|
|
6
|
-
__getStore,
|
|
7
|
-
__createSlice,
|
|
8
|
-
__registerSlice,
|
|
9
|
-
__addMiddleware,
|
|
10
|
-
__dispatch,
|
|
11
|
-
__deleteStore,
|
|
12
|
-
__StoreProvider as StoreProvider,
|
|
13
|
-
__useSelector as useSelector,
|
|
14
|
-
__useDispatch as useDispatch,
|
|
15
|
-
__createAsyncThunk,
|
|
16
|
-
Dispatch,
|
|
17
|
-
AnyAction,
|
|
18
|
-
} from '../index';
|
|
19
|
-
|
|
20
|
-
interface SliceStateRoot {
|
|
21
|
-
slice: {
|
|
22
|
-
value?: number;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface Config {
|
|
27
|
-
initialValue?: SliceStateRoot[ 'slice' ][ 'value' ];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const createStoreEntities = ( { initialValue = 1 }: Config = {} ) => {
|
|
31
|
-
const slice = __createSlice( {
|
|
32
|
-
name: 'slice',
|
|
33
|
-
initialState: {
|
|
34
|
-
value: initialValue,
|
|
35
|
-
},
|
|
36
|
-
reducers: {
|
|
37
|
-
setValue: ( state, action ) => {
|
|
38
|
-
state.value = action.payload;
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
} );
|
|
42
|
-
|
|
43
|
-
__registerSlice( slice );
|
|
44
|
-
|
|
45
|
-
const store = __createStore();
|
|
46
|
-
|
|
47
|
-
const wrapper = ( { children }: PropsWithChildren ) => <StoreProvider store={ store }>{ children }</StoreProvider>;
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
slice,
|
|
51
|
-
store,
|
|
52
|
-
wrapper,
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
describe( '@elementor/store', () => {
|
|
57
|
-
it( 'should set an initial state of a slice', () => {
|
|
58
|
-
// Arrange.
|
|
59
|
-
const { wrapper } = createStoreEntities();
|
|
60
|
-
|
|
61
|
-
const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice.value ), {
|
|
62
|
-
wrapper,
|
|
63
|
-
} );
|
|
64
|
-
|
|
65
|
-
// Assert.
|
|
66
|
-
expect( result.current ).toBe( 1 );
|
|
67
|
-
} );
|
|
68
|
-
|
|
69
|
-
it( 'should update the state value of the slice', () => {
|
|
70
|
-
// Arrange.
|
|
71
|
-
const { slice, wrapper } = createStoreEntities();
|
|
72
|
-
|
|
73
|
-
// Act.
|
|
74
|
-
const { result } = renderHook(
|
|
75
|
-
() => {
|
|
76
|
-
const dispatchAction = useDispatch();
|
|
77
|
-
|
|
78
|
-
dispatchAction( slice.actions.setValue( 3 ) );
|
|
79
|
-
|
|
80
|
-
return useSelector( ( state: SliceStateRoot ) => state.slice.value );
|
|
81
|
-
},
|
|
82
|
-
{ wrapper }
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
// Assert.
|
|
86
|
-
expect( result.current ).toBe( 3 );
|
|
87
|
-
} );
|
|
88
|
-
|
|
89
|
-
it( 'should throw an error when trying to add a slice with the same name more than once', () => {
|
|
90
|
-
// Arrange.
|
|
91
|
-
const slice = __createSlice( {
|
|
92
|
-
name: 'slice',
|
|
93
|
-
initialState: {
|
|
94
|
-
value: 1,
|
|
95
|
-
},
|
|
96
|
-
reducers: {},
|
|
97
|
-
} );
|
|
98
|
-
|
|
99
|
-
__registerSlice( slice );
|
|
100
|
-
|
|
101
|
-
// Assert.
|
|
102
|
-
expect( () => {
|
|
103
|
-
__registerSlice( slice );
|
|
104
|
-
} ).toThrow( 'Slice with name "slice" already exists.' );
|
|
105
|
-
} );
|
|
106
|
-
|
|
107
|
-
it( 'should throw an error when trying to re-create the store', () => {
|
|
108
|
-
// Arrange.
|
|
109
|
-
const slice = __createSlice( {
|
|
110
|
-
name: 'slice',
|
|
111
|
-
initialState: {
|
|
112
|
-
value: 1,
|
|
113
|
-
},
|
|
114
|
-
reducers: {},
|
|
115
|
-
} );
|
|
116
|
-
|
|
117
|
-
__registerSlice( slice );
|
|
118
|
-
|
|
119
|
-
__createStore();
|
|
120
|
-
|
|
121
|
-
// Assert.
|
|
122
|
-
expect( () => __createStore() ).toThrow( 'The store instance already exists.' );
|
|
123
|
-
} );
|
|
124
|
-
|
|
125
|
-
it( 'should add a middleware that blocks the state update by not running next(action)', () => {
|
|
126
|
-
// Arrange.
|
|
127
|
-
__addMiddleware( () => () => () => {
|
|
128
|
-
return null;
|
|
129
|
-
} );
|
|
130
|
-
|
|
131
|
-
const { slice, wrapper } = createStoreEntities();
|
|
132
|
-
|
|
133
|
-
// Act.
|
|
134
|
-
const { result } = renderHook(
|
|
135
|
-
() => {
|
|
136
|
-
const dispatchAction = useDispatch();
|
|
137
|
-
|
|
138
|
-
dispatchAction( slice.actions.setValue( 4 ) );
|
|
139
|
-
|
|
140
|
-
return useSelector( ( state: SliceStateRoot ) => state.slice.value );
|
|
141
|
-
},
|
|
142
|
-
{ wrapper }
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
// Assert.
|
|
146
|
-
expect( result.current ).toBe( 1 );
|
|
147
|
-
} );
|
|
148
|
-
|
|
149
|
-
it( 'should add a middleware that does not interfere with the state update', () => {
|
|
150
|
-
// Arrange.
|
|
151
|
-
const middlewareNextAction = jest.fn();
|
|
152
|
-
|
|
153
|
-
__addMiddleware( () => ( next: Dispatch< AnyAction > ) => ( action: AnyAction ) => {
|
|
154
|
-
middlewareNextAction( action );
|
|
155
|
-
|
|
156
|
-
next( action );
|
|
157
|
-
} );
|
|
158
|
-
|
|
159
|
-
const { slice, wrapper } = createStoreEntities();
|
|
160
|
-
|
|
161
|
-
// Act.
|
|
162
|
-
const { result } = renderHook(
|
|
163
|
-
() => {
|
|
164
|
-
const dispatchAction = useDispatch();
|
|
165
|
-
|
|
166
|
-
dispatchAction( slice.actions.setValue( 4 ) );
|
|
167
|
-
|
|
168
|
-
return useSelector( ( state: SliceStateRoot ) => state.slice.value );
|
|
169
|
-
},
|
|
170
|
-
{ wrapper }
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
// Assert.
|
|
174
|
-
expect( result.current ).toBe( 4 );
|
|
175
|
-
|
|
176
|
-
expect( middlewareNextAction ).toHaveBeenCalledWith( slice.actions.setValue( 4 ) );
|
|
177
|
-
} );
|
|
178
|
-
|
|
179
|
-
it( 'should dispatch an action without using hooks', () => {
|
|
180
|
-
// Arrange.
|
|
181
|
-
const slice = __createSlice( {
|
|
182
|
-
name: 'slice',
|
|
183
|
-
initialState: {
|
|
184
|
-
value: 1,
|
|
185
|
-
},
|
|
186
|
-
reducers: {
|
|
187
|
-
setValue: ( state, action ) => {
|
|
188
|
-
state.value = action.payload;
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
} );
|
|
192
|
-
|
|
193
|
-
__registerSlice( slice );
|
|
194
|
-
|
|
195
|
-
const store = __createStore();
|
|
196
|
-
|
|
197
|
-
// Act.
|
|
198
|
-
store.dispatch( slice.actions.setValue( 6 ) );
|
|
199
|
-
|
|
200
|
-
const stateResult = store.getState().slice.value;
|
|
201
|
-
|
|
202
|
-
// Assert.
|
|
203
|
-
expect( stateResult ).toBe( 6 );
|
|
204
|
-
} );
|
|
205
|
-
|
|
206
|
-
it( 'should collect actions that are dispatched before the store exist, and run them when the store instance is created', () => {
|
|
207
|
-
// Act.
|
|
208
|
-
__dispatch( { type: 'slice/setValue', payload: 7 } );
|
|
209
|
-
|
|
210
|
-
const { wrapper } = createStoreEntities();
|
|
211
|
-
|
|
212
|
-
const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice.value ), {
|
|
213
|
-
wrapper,
|
|
214
|
-
} );
|
|
215
|
-
|
|
216
|
-
// Assert.
|
|
217
|
-
expect( result.current ).toBe( 7 );
|
|
218
|
-
} );
|
|
219
|
-
|
|
220
|
-
it( 'should delete the store instance', () => {
|
|
221
|
-
// Arrange.
|
|
222
|
-
const { store } = createStoreEntities();
|
|
223
|
-
|
|
224
|
-
let instance = __getStore();
|
|
225
|
-
|
|
226
|
-
// Assert.
|
|
227
|
-
expect( instance ).toEqual( store );
|
|
228
|
-
|
|
229
|
-
// Act.
|
|
230
|
-
__deleteStore();
|
|
231
|
-
|
|
232
|
-
instance = __getStore();
|
|
233
|
-
|
|
234
|
-
// Assert.
|
|
235
|
-
expect( instance ).toBeNull();
|
|
236
|
-
} );
|
|
237
|
-
|
|
238
|
-
it( 'should delete the added slices', () => {
|
|
239
|
-
// Arrange.
|
|
240
|
-
createStoreEntities();
|
|
241
|
-
|
|
242
|
-
// Act.
|
|
243
|
-
__deleteStore();
|
|
244
|
-
|
|
245
|
-
// Arrange.
|
|
246
|
-
const store = __createStore();
|
|
247
|
-
|
|
248
|
-
const wrapper = ( { children }: PropsWithChildren ) => (
|
|
249
|
-
<StoreProvider store={ store }>{ children }</StoreProvider>
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice ), { wrapper } );
|
|
253
|
-
|
|
254
|
-
// Assert.
|
|
255
|
-
expect( result.current ).toBeUndefined();
|
|
256
|
-
expect( console ).toHaveErrored();
|
|
257
|
-
} );
|
|
258
|
-
|
|
259
|
-
it( 'should delete the added middlewares', () => {
|
|
260
|
-
// Arrange.
|
|
261
|
-
// Registering a middleware that blocks the state update by not running next(action).
|
|
262
|
-
__addMiddleware( () => () => () => {
|
|
263
|
-
return null;
|
|
264
|
-
} );
|
|
265
|
-
|
|
266
|
-
createStoreEntities();
|
|
267
|
-
|
|
268
|
-
// Act.
|
|
269
|
-
__deleteStore();
|
|
270
|
-
|
|
271
|
-
const { slice, wrapper } = createStoreEntities();
|
|
272
|
-
|
|
273
|
-
const { result } = renderHook(
|
|
274
|
-
() => {
|
|
275
|
-
const dispatchAction = useDispatch();
|
|
276
|
-
|
|
277
|
-
dispatchAction( slice.actions.setValue( 8 ) );
|
|
278
|
-
|
|
279
|
-
return useSelector( ( state: SliceStateRoot ) => state.slice.value );
|
|
280
|
-
},
|
|
281
|
-
{ wrapper }
|
|
282
|
-
);
|
|
283
|
-
|
|
284
|
-
// Assert.
|
|
285
|
-
expect( result.current ).toBe( 8 );
|
|
286
|
-
} );
|
|
287
|
-
|
|
288
|
-
it( 'should support dispatching async actions', async () => {
|
|
289
|
-
// Arrange.
|
|
290
|
-
const incrementBy = __createAsyncThunk( 'value/incrementBy', async ( value: number ) => {
|
|
291
|
-
return value;
|
|
292
|
-
} );
|
|
293
|
-
|
|
294
|
-
const slice = __createSlice( {
|
|
295
|
-
name: 'slice',
|
|
296
|
-
initialState: {
|
|
297
|
-
value: 1,
|
|
298
|
-
},
|
|
299
|
-
reducers: {
|
|
300
|
-
increment: ( state ) => {
|
|
301
|
-
state.value += 1;
|
|
302
|
-
},
|
|
303
|
-
},
|
|
304
|
-
extraReducers: ( builder ) => {
|
|
305
|
-
builder.addCase( incrementBy.fulfilled, ( state, action ) => {
|
|
306
|
-
state.value += action.payload;
|
|
307
|
-
} );
|
|
308
|
-
},
|
|
309
|
-
} );
|
|
310
|
-
|
|
311
|
-
__registerSlice( slice );
|
|
312
|
-
|
|
313
|
-
const store = __createStore();
|
|
314
|
-
|
|
315
|
-
// Act.
|
|
316
|
-
jest.useFakeTimers();
|
|
317
|
-
|
|
318
|
-
store.dispatch( incrementBy( 2 ) );
|
|
319
|
-
|
|
320
|
-
// Regular reducer usage to ensure it works properly.
|
|
321
|
-
store.dispatch( slice.actions.increment() );
|
|
322
|
-
|
|
323
|
-
await jest.runAllTimersAsync();
|
|
324
|
-
|
|
325
|
-
// Assert.
|
|
326
|
-
expect( store.getState().slice.value ).toBe( 4 );
|
|
327
|
-
} );
|
|
328
|
-
} );
|