@shopgate/pwa-unit-test 7.31.6-beta.1 → 7.31.6
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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This module is only meant to be imported from test files, since it uses the jest globals.
|
|
5
5
|
*/
|
|
6
|
-
type
|
|
6
|
+
import type { Store } from 'redux';
|
|
7
7
|
/** An action which was dispatched to the store. */
|
|
8
8
|
export interface MockAction {
|
|
9
9
|
type: string;
|
|
@@ -11,13 +11,13 @@ export interface MockAction {
|
|
|
11
11
|
}
|
|
12
12
|
/** Reducer which is applied to the dispatched actions. */
|
|
13
13
|
export type MockReducer<S> = (state: S, action: MockAction) => S;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* A store which satisfies the Store contract of redux, so that it can be passed to the Provider
|
|
16
|
+
* of react-redux without a cast, extended by the helpers which are useful within tests.
|
|
17
|
+
*/
|
|
18
|
+
export interface MockStore<S> extends Store<S, MockAction> {
|
|
17
19
|
/** Records every dispatched action. Assert on it like on any other jest mock. */
|
|
18
|
-
dispatch: jest.Mock<MockAction, [MockAction]
|
|
19
|
-
/** Registers a listener which is invoked whenever the state changed. Returns an unsubscribe. */
|
|
20
|
-
subscribe: (listener: Listener) => () => void;
|
|
20
|
+
dispatch: jest.Mock<MockAction, [MockAction]> & Store<S, MockAction>['dispatch'];
|
|
21
21
|
/** Replaces the state and notifies the subscribers, so connected components re-render. */
|
|
22
22
|
setState: (next: S | ((current: S) => S)) => void;
|
|
23
23
|
/** Returns the dispatched actions in the order they were dispatched. */
|
|
@@ -37,4 +37,3 @@ export interface MockStore<S> {
|
|
|
37
37
|
* @returns The mocked store.
|
|
38
38
|
*/
|
|
39
39
|
export declare const createMockStore: <S>(initialState: S, reducer?: MockReducer<S>) => MockStore<S>;
|
|
40
|
-
export {};
|
|
@@ -4,10 +4,22 @@
|
|
|
4
4
|
* This module is only meant to be imported from test files, since it uses the jest globals.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Symbol.observable is not part of the language, so redux resolves it with a string fallback. The
|
|
9
|
+
* same fallback is needed here - without it the computed property key would be undefined, and the
|
|
10
|
+
* store wouldn't fulfill the contract it claims to.
|
|
11
|
+
*/
|
|
12
|
+
const $$observable = typeof Symbol === 'function' && Symbol.observable || '@@observable';
|
|
13
|
+
|
|
7
14
|
/** An action which was dispatched to the store. */
|
|
8
15
|
|
|
9
16
|
/** Reducer which is applied to the dispatched actions. */
|
|
10
17
|
|
|
18
|
+
/**
|
|
19
|
+
* A store which satisfies the Store contract of redux, so that it can be passed to the Provider
|
|
20
|
+
* of react-redux without a cast, extended by the helpers which are useful within tests.
|
|
21
|
+
*/
|
|
22
|
+
|
|
11
23
|
/**
|
|
12
24
|
* Creates a Redux store for component tests. It implements the contract that react-redux relies on
|
|
13
25
|
* - getState, dispatch and subscribe - without pulling in the reducers of the application.
|
|
@@ -21,6 +33,7 @@
|
|
|
21
33
|
*/
|
|
22
34
|
export const createMockStore = (initialState, reducer) => {
|
|
23
35
|
let state = initialState;
|
|
36
|
+
let currentReducer = reducer;
|
|
24
37
|
const listeners = new Set();
|
|
25
38
|
|
|
26
39
|
/**
|
|
@@ -32,21 +45,59 @@ export const createMockStore = (initialState, reducer) => {
|
|
|
32
45
|
});
|
|
33
46
|
};
|
|
34
47
|
const dispatch = jest.fn(action => {
|
|
35
|
-
if (
|
|
36
|
-
state =
|
|
48
|
+
if (currentReducer) {
|
|
49
|
+
state = currentReducer(state, action);
|
|
37
50
|
notify();
|
|
38
51
|
}
|
|
39
52
|
return action;
|
|
40
53
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
const subscribe = listener => {
|
|
55
|
+
listeners.add(listener);
|
|
56
|
+
return () => {
|
|
57
|
+
listeners.delete(listener);
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const getState = () => state;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Part of the redux Store contract. Tests don't observe the store, so this only satisfies the
|
|
64
|
+
* interface.
|
|
65
|
+
* @returns The observable of the store.
|
|
66
|
+
*/
|
|
67
|
+
const observable = () => ({
|
|
68
|
+
subscribe: observer => {
|
|
69
|
+
/**
|
|
70
|
+
* Forwards the current state to the observer.
|
|
71
|
+
*/
|
|
72
|
+
const handleChange = () => {
|
|
73
|
+
if (observer.next) {
|
|
74
|
+
observer.next(getState());
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
handleChange();
|
|
78
|
+
return {
|
|
79
|
+
unsubscribe: subscribe(handleChange)
|
|
48
80
|
};
|
|
49
81
|
},
|
|
82
|
+
[$$observable]() {
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* redux declares Symbol.observable as a plain symbol, so TypeScript widens the computed key to
|
|
89
|
+
* a symbol index and can't verify that this exact member is implemented. The assertion is only
|
|
90
|
+
* needed for that key - the store is verified against the contract in the spec.
|
|
91
|
+
*/
|
|
92
|
+
return {
|
|
93
|
+
getState,
|
|
94
|
+
dispatch: dispatch,
|
|
95
|
+
subscribe,
|
|
96
|
+
replaceReducer: nextReducer => {
|
|
97
|
+
currentReducer = nextReducer;
|
|
98
|
+
notify();
|
|
99
|
+
},
|
|
100
|
+
[$$observable]: observable,
|
|
50
101
|
setState: next => {
|
|
51
102
|
state = typeof next === 'function' ? next(state) : next;
|
|
52
103
|
notify();
|