@tramvai/test-mocks 1.15.1 → 1.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tramvai test mocks
2
2
 
3
- Библиотека для создания моков для различных tramvai-сущностей
3
+ Library for creating mocks for various tramvai entities
4
4
 
5
5
  ## Подключение
6
6
 
@@ -12,7 +12,9 @@ npm i --save-dev @tramvai/test-mocks
12
12
 
13
13
  ### STORE_TOKEN
14
14
 
15
- Позволяет создать мок для токена STORE_TOKEN который используется в приложениях как общее хранилище всех сторов
15
+ Creates mock instance for token STORE_TOKEN which used in app as a common storage for store
16
+
17
+ #### Empty State
16
18
 
17
19
  ```ts
18
20
  import { createMockStore } from '@tramvai/test-mocks';
@@ -23,9 +25,30 @@ const state = store.getState();
23
25
  store.dispatch('event');
24
26
  ```
25
27
 
28
+ #### Initial State
29
+
30
+ ```ts
31
+ import { createMockStore } from '@tramvai/test-mocks';
32
+
33
+ const initialState = { a: 1, b: 2 };
34
+ const reducerC = createReducer('c', 3);
35
+ const store = createMockStore({ stores: [reducerC], initialState });
36
+
37
+ const state = store.getState();
38
+ ```
39
+
40
+ <p>
41
+ <details>
42
+ <summary>More examples</summary>
43
+
44
+ @inline src/store.spec.ts
45
+
46
+ </details>
47
+ </p>
48
+
26
49
  ### DI
27
50
 
28
- Позволяет создать инстанс Di-Container
51
+ Creates mock instance of DI-container
29
52
 
30
53
  ```ts
31
54
  import { createMockDi } from '@tramvai/test-mocks';
@@ -37,7 +60,7 @@ const dep = di.get(SOME_TOKEN);
37
60
 
38
61
  ### Context
39
62
 
40
- Позволяет создать инстанс CONTEXT_TOKEN
63
+ Creates mock instance for CONTEXT_TOKEN
41
64
 
42
65
  ```ts
43
66
  import { createMockContext } from '@tramvai/test-mocks';
@@ -54,9 +77,18 @@ it('test', async () => {
54
77
  });
55
78
  ```
56
79
 
80
+ <p>
81
+ <details>
82
+ <summary>More examples</summary>
83
+
84
+ @inline src/context.spec.ts
85
+
86
+ </details>
87
+ </p>
88
+
57
89
  ### Router
58
90
 
59
- Предоставляет мок для инстанса `@tinkoff/router`
91
+ Creates mock instance for `@tinkoff/router`
60
92
 
61
93
  ```ts
62
94
  import { createMockRouter } from '@tramvai/test-mocks';
package/lib/index.es.js CHANGED
@@ -2,26 +2,35 @@ import { STORE_TOKEN, ACTION_EXECUTION_TOKEN, ACTION_CONDITIONALS, CONTEXT_TOKEN
2
2
  import { ActionExecution, createConsumerContext } from '@tramvai/module-common';
3
3
  import { PubSub } from '@tinkoff/pubsub';
4
4
  import { createContainer, DI_TOKEN } from '@tinkoff/dippy';
5
- import { createDispatcher } from '@tramvai/state';
5
+ import keys from '@tinkoff/utils/object/keys';
6
+ import difference from '@tinkoff/utils/array/difference';
7
+ import { createReducer, createDispatcher } from '@tramvai/state';
6
8
  import { AbstractRouter } from '@tinkoff/router';
7
9
  import { parse } from '@tinkoff/url';
8
10
  import noop from '@tinkoff/utils/function/noop';
9
11
 
10
12
  /**
11
- * Создаёт мок для глобального стора в приложении
13
+ * Creates mock for a global app store
12
14
  *
13
- * @param stores - список сторов
14
- * @param initialState - начальное состояние сторов (!важно: чтобы начальное состояние работало ключи в нём должны совпадать с именами сторов)
15
+ * @param stores - stores list
16
+ * @param initialState - initialState for store (!warning: in order to work, keys in initial state should match to names of the stores)
15
17
  */
16
18
  const createMockStore = ({ stores = [], initialState = {}, } = {}) => {
17
- const dispatcherContext = createDispatcher({ stores }).createContext(null, {
19
+ const mockStores = [];
20
+ const diffKeys = difference(keys(initialState), stores.map((store) => store.storeName));
21
+ for (const key of diffKeys) {
22
+ mockStores.push(createReducer(key, initialState[key]));
23
+ }
24
+ const dispatcherContext = createDispatcher({
25
+ stores: [...stores, ...mockStores],
26
+ }).createContext(null, {
18
27
  stores: initialState,
19
28
  });
20
29
  return {
21
30
  getState: dispatcherContext.getState.bind(dispatcherContext),
22
31
  dispatch: dispatcherContext.dispatch.bind(dispatcherContext),
23
32
  subscribe: dispatcherContext.subscribe.bind(dispatcherContext),
24
- // @deprecated не используйте это свойство в своём коде
33
+ // @deprecated do not use this property
25
34
  // @ts-ignore
26
35
  __dispatcherContext__: dispatcherContext,
27
36
  };
package/lib/index.js CHANGED
@@ -6,6 +6,8 @@ var tokensCommon = require('@tramvai/tokens-common');
6
6
  var moduleCommon = require('@tramvai/module-common');
7
7
  var pubsub = require('@tinkoff/pubsub');
8
8
  var dippy = require('@tinkoff/dippy');
9
+ var keys = require('@tinkoff/utils/object/keys');
10
+ var difference = require('@tinkoff/utils/array/difference');
9
11
  var state = require('@tramvai/state');
10
12
  var router = require('@tinkoff/router');
11
13
  var url = require('@tinkoff/url');
@@ -13,23 +15,32 @@ var noop = require('@tinkoff/utils/function/noop');
13
15
 
14
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
17
 
18
+ var keys__default = /*#__PURE__*/_interopDefaultLegacy(keys);
19
+ var difference__default = /*#__PURE__*/_interopDefaultLegacy(difference);
16
20
  var noop__default = /*#__PURE__*/_interopDefaultLegacy(noop);
17
21
 
18
22
  /**
19
- * Создаёт мок для глобального стора в приложении
23
+ * Creates mock for a global app store
20
24
  *
21
- * @param stores - список сторов
22
- * @param initialState - начальное состояние сторов (!важно: чтобы начальное состояние работало ключи в нём должны совпадать с именами сторов)
25
+ * @param stores - stores list
26
+ * @param initialState - initialState for store (!warning: in order to work, keys in initial state should match to names of the stores)
23
27
  */
24
28
  const createMockStore = ({ stores = [], initialState = {}, } = {}) => {
25
- const dispatcherContext = state.createDispatcher({ stores }).createContext(null, {
29
+ const mockStores = [];
30
+ const diffKeys = difference__default["default"](keys__default["default"](initialState), stores.map((store) => store.storeName));
31
+ for (const key of diffKeys) {
32
+ mockStores.push(state.createReducer(key, initialState[key]));
33
+ }
34
+ const dispatcherContext = state.createDispatcher({
35
+ stores: [...stores, ...mockStores],
36
+ }).createContext(null, {
26
37
  stores: initialState,
27
38
  });
28
39
  return {
29
40
  getState: dispatcherContext.getState.bind(dispatcherContext),
30
41
  dispatch: dispatcherContext.dispatch.bind(dispatcherContext),
31
42
  subscribe: dispatcherContext.subscribe.bind(dispatcherContext),
32
- // @deprecated не используйте это свойство в своём коде
43
+ // @deprecated do not use this property
33
44
  // @ts-ignore
34
45
  __dispatcherContext__: dispatcherContext,
35
46
  };
package/lib/store.d.ts CHANGED
@@ -5,10 +5,10 @@ interface Options {
5
5
  initialState?: Record<string, any>;
6
6
  }
7
7
  /**
8
- * Создаёт мок для глобального стора в приложении
8
+ * Creates mock for a global app store
9
9
  *
10
- * @param stores - список сторов
11
- * @param initialState - начальное состояние сторов (!важно: чтобы начальное состояние работало ключи в нём должны совпадать с именами сторов)
10
+ * @param stores - stores list
11
+ * @param initialState - initialState for store (!warning: in order to work, keys in initial state should match to names of the stores)
12
12
  */
13
13
  export declare const createMockStore: ({ stores, initialState, }?: Options) => typeof STORE_TOKEN;
14
14
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/test-mocks",
3
- "version": "1.15.1",
3
+ "version": "1.25.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -18,14 +18,14 @@
18
18
  "build-for-publish": "true"
19
19
  },
20
20
  "dependencies": {
21
- "@tramvai/core": "1.15.1",
21
+ "@tramvai/core": "1.25.0",
22
22
  "@tinkoff/pubsub": "0.4.22",
23
- "@tinkoff/router": "0.1.58",
23
+ "@tinkoff/router": "0.1.59",
24
24
  "@tinkoff/url": "0.7.36",
25
- "@tramvai/module-cookie": "1.15.1",
26
- "@tramvai/module-common": "1.15.1",
27
- "@tramvai/state": "1.15.1",
28
- "@tramvai/tokens-common": "1.15.1"
25
+ "@tramvai/module-cookie": "1.25.0",
26
+ "@tramvai/module-common": "1.25.0",
27
+ "@tramvai/state": "1.25.0",
28
+ "@tramvai/tokens-common": "1.25.0"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@tinkoff/dippy": "0.7.35",