esp-js-react 8.0.3-next.0 → 8.0.4-next.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/.dist/esp-react.js +39 -47
- package/.dist/esp-react.js.map +1 -1
- package/.dist/esp-react.min.js +1 -1
- package/.dist/esp-react.min.js.map +1 -1
- package/.dist/typings/espModelContextProvider.d.ts +3 -5
- package/.dist/typings/index.d.ts +2 -2
- package/.dist/typings/{useModelSelector.d.ts → useSyncModelWithSelector.d.ts} +9 -10
- package/package.json +4 -4
- package/src/connectableComponent.tsx +3 -3
- package/src/espModelContextProvider.tsx +7 -40
- package/src/index.ts +6 -9
- package/src/{useModelSelector.ts → useSyncModelWithSelector.ts} +27 -20
- package/tests/indexTests.ts +2 -2
- package/tests/{useModelSelectorTests.tsx → useSyncModelWithSelectorTests.tsx} +46 -14
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import 'jest';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import {act} from 'react';
|
|
4
|
-
import {
|
|
4
|
+
import {syncModelWithSelectorOptions, useSyncModelWithSelector} from '../src';
|
|
5
5
|
import {TestModel, TestModelImmutableState} from './testApi/testModel';
|
|
6
6
|
import {ViewMetadata} from './testApi/viewFactory';
|
|
7
7
|
import {routerAsserts} from './testApi/asserts';
|
|
@@ -9,7 +9,7 @@ import {testApi, TestApi} from './testApi/testApi';
|
|
|
9
9
|
|
|
10
10
|
type SelectedObject = { id: string, value: string };
|
|
11
11
|
|
|
12
|
-
describe('
|
|
12
|
+
describe('useSyncModelWithSelector', () => {
|
|
13
13
|
let api: TestApi;
|
|
14
14
|
|
|
15
15
|
beforeEach(() => {
|
|
@@ -20,7 +20,7 @@ describe('useModelSelector', () => {
|
|
|
20
20
|
const modelId = 'modelId';
|
|
21
21
|
|
|
22
22
|
const View = () => {
|
|
23
|
-
const valuesObj =
|
|
23
|
+
const valuesObj = useSyncModelWithSelector<TestModelImmutableState, SelectedObject>(
|
|
24
24
|
model => {
|
|
25
25
|
return {id: model.modelId, value: model.value};
|
|
26
26
|
},
|
|
@@ -57,9 +57,9 @@ describe('useModelSelector', () => {
|
|
|
57
57
|
const modelId = 'modelId';
|
|
58
58
|
|
|
59
59
|
const View = () => {
|
|
60
|
-
const values: string[] =
|
|
60
|
+
const values: string[] = useSyncModelWithSelector<TestModelImmutableState, string[]>(
|
|
61
61
|
model => [model.modelId, model.value],
|
|
62
|
-
|
|
62
|
+
syncModelWithSelectorOptions().setModelId(modelId)
|
|
63
63
|
);
|
|
64
64
|
if (!values) {
|
|
65
65
|
return;
|
|
@@ -85,15 +85,15 @@ describe('useModelSelector', () => {
|
|
|
85
85
|
const modelId = 'modelId';
|
|
86
86
|
|
|
87
87
|
const View = () => {
|
|
88
|
-
const values =
|
|
88
|
+
const values = useSyncModelWithSelector<TestModelImmutableState, SelectedObject>(
|
|
89
89
|
model => ({id: model.modelId, value: model.value}),
|
|
90
|
-
|
|
90
|
+
syncModelWithSelectorOptions<SelectedObject>()
|
|
91
91
|
.setEqualityFn((a, b) => {
|
|
92
92
|
if (!a) {
|
|
93
93
|
return false; // first time
|
|
94
94
|
}
|
|
95
95
|
// delegate to model, set via the test, to see if we're 'equal'
|
|
96
|
-
// If this returns true,
|
|
96
|
+
// If this returns true, useSyncModelWithSelector internally assume the last snapshot is the same thus doesn't propagate the last snapshot.
|
|
97
97
|
return b.value === 'assume-no-change';
|
|
98
98
|
})
|
|
99
99
|
);
|
|
@@ -139,9 +139,9 @@ describe('useModelSelector', () => {
|
|
|
139
139
|
const modelId2 = 'modelId2';
|
|
140
140
|
|
|
141
141
|
const View = (props: {modelId: string}) => {
|
|
142
|
-
const values =
|
|
142
|
+
const values = useSyncModelWithSelector<TestModelImmutableState, SelectedObject>(
|
|
143
143
|
model => ({id: model.modelId, value: model.value}),
|
|
144
|
-
|
|
144
|
+
syncModelWithSelectorOptions().setModelId(props.modelId)
|
|
145
145
|
);
|
|
146
146
|
if (!values) {
|
|
147
147
|
return;
|
|
@@ -191,12 +191,12 @@ describe('useModelSelector', () => {
|
|
|
191
191
|
const modelId = 'modelId';
|
|
192
192
|
|
|
193
193
|
const View = () => {
|
|
194
|
-
const values: string[] =
|
|
194
|
+
const values: string[] = useSyncModelWithSelector<TestModel, string[]>(
|
|
195
195
|
model => {
|
|
196
196
|
// here we're expecting to receive a TestModel, not testModelInstance[GetEspPolimerImmutableModelConsts.HandlerFunctionName]()
|
|
197
197
|
return [model.state.modelId, model.state.value];
|
|
198
198
|
},
|
|
199
|
-
|
|
199
|
+
syncModelWithSelectorOptions().setTryPreSelectPolimerImmutableModel(false)
|
|
200
200
|
);
|
|
201
201
|
if (!values) {
|
|
202
202
|
return;
|
|
@@ -221,9 +221,9 @@ describe('useModelSelector', () => {
|
|
|
221
221
|
const modelId = 'modelId';
|
|
222
222
|
|
|
223
223
|
const View = () => {
|
|
224
|
-
const model: TestModel =
|
|
224
|
+
const model: TestModel = useSyncModelWithSelector<TestModel, TestModel>(
|
|
225
225
|
m => m,
|
|
226
|
-
|
|
226
|
+
syncModelWithSelectorOptions().setTryPreSelectPolimerImmutableModel(false)
|
|
227
227
|
);
|
|
228
228
|
if (!model) {
|
|
229
229
|
return;
|
|
@@ -252,4 +252,36 @@ describe('useModelSelector', () => {
|
|
|
252
252
|
.valueIs('updated');
|
|
253
253
|
});
|
|
254
254
|
});
|
|
255
|
+
|
|
256
|
+
describe('argument checking', () => {
|
|
257
|
+
it('throws if selector incorrect', () => {
|
|
258
|
+
expect(() => {
|
|
259
|
+
useSyncModelWithSelector(null, syncModelWithSelectorOptions());
|
|
260
|
+
}).toThrow(new Error('You must pass a selector function to useSyncModelWithSelector'));
|
|
261
|
+
expect(() => {
|
|
262
|
+
useSyncModelWithSelector(undefined, syncModelWithSelectorOptions());
|
|
263
|
+
}).toThrow(new Error('You must pass a selector function to useSyncModelWithSelector'));
|
|
264
|
+
expect(() => {
|
|
265
|
+
const x: any = {};
|
|
266
|
+
useSyncModelWithSelector(x, syncModelWithSelectorOptions());
|
|
267
|
+
}).toThrow(new Error('You must pass a selector function to useSyncModelWithSelector'));
|
|
268
|
+
});
|
|
269
|
+
it('throws if options incorrect', () => {
|
|
270
|
+
expect(() => {
|
|
271
|
+
useSyncModelWithSelector((m) => m, null);
|
|
272
|
+
}).toThrow(new Error('You must provide options when using useSyncModelWithSelector'));
|
|
273
|
+
expect(() => {
|
|
274
|
+
useSyncModelWithSelector((m) => m, syncModelWithSelectorOptions().setEqualityFn(null));
|
|
275
|
+
}).toThrow(new Error('You must provide an equalityFn when using useSyncModelWithSelector'));
|
|
276
|
+
expect(() => {
|
|
277
|
+
useSyncModelWithSelector(
|
|
278
|
+
(m) => m,
|
|
279
|
+
{
|
|
280
|
+
modelId: '',
|
|
281
|
+
equalityFn: null,
|
|
282
|
+
tryPreSelectPolimerImmutableModel: true
|
|
283
|
+
});
|
|
284
|
+
}).toThrow(new Error('You must provide an equalityFn when using useSyncModelWithSelector'));
|
|
285
|
+
});
|
|
286
|
+
});
|
|
255
287
|
});
|