esp-js-react 8.0.4-next.0 → 8.0.6-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 +111 -102
- 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/index.d.ts +0 -1
- package/.dist/typings/polimer/getEspPolimerImmutableModel.d.ts +3 -15
- package/.dist/typings/useSyncModelWithSelector.d.ts +2 -0
- package/.yarnignore +0 -1
- package/package.json +4 -4
- package/src/connectableComponent.tsx +7 -6
- package/src/espModelContextProvider.tsx +4 -3
- package/src/espRouterContextProvider.tsx +4 -5
- package/src/index.ts +0 -5
- package/src/polimer/getEspPolimerImmutableModel.ts +13 -38
- package/src/useSyncModelWithSelector.ts +96 -63
- package/tests/connectableComponentTests.tsx +37 -56
- package/tests/indexTests.ts +0 -2
- package/tests/testApi/asserts.ts +13 -1
- package/tests/testApi/testModel.ts +2 -2
- package/tests/testApi/viewFactory.tsx +1 -1
- package/tests/useSyncModelWithSelectorTests.tsx +47 -4
- package/tests/viewBinderTests.tsx +1 -1
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import {useMemo} from 'react';
|
|
2
2
|
import {useSyncExternalStoreWithSelector} from 'use-sync-external-store/with-selector';
|
|
3
|
-
import {
|
|
3
|
+
import {Logger, Router, utils} from 'esp-js';
|
|
4
4
|
import {useRouter} from './espRouterContextProvider';
|
|
5
5
|
import {useGetModelId} from './espModelContextProvider';
|
|
6
|
-
import {
|
|
6
|
+
import {getPolimerImmutableModel, hasPolimerImmutableModel} from './polimer/getEspPolimerImmutableModel';
|
|
7
7
|
|
|
8
8
|
export type SyncModelWithSelectorEqualityFn<T> = (last: T, next: T) => boolean;
|
|
9
9
|
|
|
10
|
+
export const logger = Logger.create('useSyncModelWithSelector');
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
13
|
* Options affecting the subscription of the model to the Router.
|
|
12
14
|
*
|
|
@@ -105,72 +107,22 @@ export const useSyncModelWithSelector = <TModel, TSelected>(
|
|
|
105
107
|
): TSelected => {
|
|
106
108
|
checkArguments(selector, options);
|
|
107
109
|
const router = useRouter();
|
|
108
|
-
const
|
|
110
|
+
const modelIdFromContext = useGetModelId();
|
|
111
|
+
const modelId = options?.modelId || modelIdFromContext;
|
|
109
112
|
const tryPreSelectPolimerImmutableModel = options?.tryPreSelectPolimerImmutableModel;
|
|
110
113
|
const equalityFn = options?.equalityFn;
|
|
111
114
|
const dependencies = useMemo(
|
|
112
115
|
() => {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
let model: TModel = null;
|
|
119
|
-
return {
|
|
120
|
-
subscribe: (stateChanged: () => void) => {
|
|
121
|
-
const modelSubscriptionDisposable = router
|
|
122
|
-
.getModelObservable(modelId)
|
|
123
|
-
.subscribe(
|
|
124
|
-
(m: any) => {
|
|
125
|
-
// try and get a submodel to render
|
|
126
|
-
const nextModel = tryPreSelectPolimerImmutableModel
|
|
127
|
-
? tryGetPolimerImmutableModel(m)
|
|
128
|
-
: m;
|
|
129
|
-
// if the above didn't manage to get a submodel,
|
|
130
|
-
// we need to mutate to force useSyncExternalStoreWithSelector to pick up the change
|
|
131
|
-
model = nextModel === m
|
|
132
|
-
? Object.create(nextModel)
|
|
133
|
-
: nextModel;
|
|
134
|
-
stateChanged();
|
|
135
|
-
}
|
|
136
|
-
);
|
|
137
|
-
return () => {
|
|
138
|
-
model = null;
|
|
139
|
-
modelSubscriptionDisposable.dispose();
|
|
140
|
-
};
|
|
141
|
-
},
|
|
142
|
-
// React expects getSnapshot to be immutable, it'll only re-render if the instance changes
|
|
143
|
-
getSnapshot: () => {
|
|
144
|
-
return model;
|
|
145
|
-
},
|
|
146
|
-
// While getSnapshot needs to model to be immutable,
|
|
147
|
-
// useSyncExternalStoreWithSelector builds on top of this, so the TSelected can defer to an equality check to determine if the change is propagated.
|
|
148
|
-
// The selector here just maps the TSelected, useSyncExternalStoreWithSelector internally does the equality check.
|
|
149
|
-
wrappedSelector: (snapshot: TModel) => {
|
|
150
|
-
// The first time this renders, react calls getSnapshot and the selector before the router has pushed a change.
|
|
151
|
-
// In that case, there is no state for any component to process, therefore, we return null;
|
|
152
|
-
return snapshot
|
|
153
|
-
? selector(snapshot)
|
|
154
|
-
: null;
|
|
155
|
-
}
|
|
156
|
-
};
|
|
116
|
+
const canSubscribe = router && utils.isString(modelId) && router.isModelRegistered(modelId);
|
|
117
|
+
if (canSubscribe) {
|
|
118
|
+
return createSubscriptionState(router, modelId, selector, tryPreSelectPolimerImmutableModel);
|
|
119
|
+
}
|
|
120
|
+
return createNoopSubscriptionState();
|
|
157
121
|
},
|
|
158
|
-
// Don't
|
|
122
|
+
// Don't add 'selector' to the dependency list as that will likely change unless the caller memoizes it.
|
|
159
123
|
[router, modelId]
|
|
160
124
|
);
|
|
161
125
|
|
|
162
|
-
// If we don't have a modelId, we still need to return a hook so React doesn't complain.
|
|
163
|
-
// Additionally, we need to let the above expiration (dependencies) run to unsubscribe from any previous subscription.
|
|
164
|
-
if (utils.stringIsEmpty(modelId)) {
|
|
165
|
-
return useSyncExternalStoreWithSelector(
|
|
166
|
-
noopSubscription.subscribe,
|
|
167
|
-
noopSubscription.getSnapshot,
|
|
168
|
-
undefined,
|
|
169
|
-
noopSubscription.wrappedSelector,
|
|
170
|
-
equalityFn
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
126
|
// Docs on useSyncExternalStore https://github.com/reactwg/react-18/discussions/86
|
|
175
127
|
// Redux's usage of this: https://github.com/reduxjs/react-redux/blob/master/src/hooks/useSelector.ts
|
|
176
128
|
// Code: https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js
|
|
@@ -183,8 +135,89 @@ export const useSyncModelWithSelector = <TModel, TSelected>(
|
|
|
183
135
|
);
|
|
184
136
|
};
|
|
185
137
|
|
|
186
|
-
const
|
|
187
|
-
|
|
138
|
+
const createSubscriptionState = <TModel, TSelected>(
|
|
139
|
+
router: Router,
|
|
140
|
+
modelId: string,
|
|
141
|
+
selector: (model: TModel) => TSelected,
|
|
142
|
+
tryPreSelectPolimerImmutableModel: boolean
|
|
143
|
+
) => {
|
|
144
|
+
// Because of how useSyncExternalStore works, there is an implicit dependency between the subscribe and getSnapshot functions.
|
|
145
|
+
// When the subscription receives the new state from the Router, it can't pass this directly onto getSnapshot,
|
|
146
|
+
// React calls that when it deems it needs to.
|
|
147
|
+
// Given that, we need to cache the state which getSnapshot will return.
|
|
148
|
+
//
|
|
149
|
+
// Another edge case with useSyncExternalStore:
|
|
150
|
+
// It will call getSnapshot before it calls subscribe, to account for this we need to fetch the model first.
|
|
151
|
+
let currentModel: TModel;
|
|
152
|
+
let onStateChanged: () => void = null;
|
|
153
|
+
const modelSubscriptionDisposable = router
|
|
154
|
+
.getModelObservable<TModel>(modelId)
|
|
155
|
+
.subscribe(
|
|
156
|
+
(m: any) => {
|
|
157
|
+
// try and get the esp-js-polimer immutable model if possible, this should mutate when any state changes
|
|
158
|
+
const nextModel = tryPreSelectPolimerImmutableModel && hasPolimerImmutableModel(m)
|
|
159
|
+
? getPolimerImmutableModel<TModel>(m)
|
|
160
|
+
// If the above didn't manage to get a polimer model,
|
|
161
|
+
// we need to mutate to force useSyncExternalStoreWithSelector to pick up the change.
|
|
162
|
+
// This should only affect older style OO models.
|
|
163
|
+
: Object.create(m);
|
|
164
|
+
warnIfModelInstanceHasNotChanged(modelId, currentModel, nextModel);
|
|
165
|
+
currentModel = nextModel;
|
|
166
|
+
if (onStateChanged) {
|
|
167
|
+
onStateChanged();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
return {
|
|
172
|
+
subscribe: (stateChanged: () => void) => {
|
|
173
|
+
onStateChanged = stateChanged;
|
|
174
|
+
return () => {
|
|
175
|
+
onStateChanged = null;
|
|
176
|
+
currentModel = null;
|
|
177
|
+
modelSubscriptionDisposable.dispose();
|
|
178
|
+
};
|
|
179
|
+
},
|
|
180
|
+
// React expects getSnapshot to be immutable, it'll only re-render if the instance changes
|
|
181
|
+
getSnapshot: () => {
|
|
182
|
+
return currentModel;
|
|
183
|
+
},
|
|
184
|
+
// While getSnapshot needs to model to be immutable,
|
|
185
|
+
// useSyncExternalStoreWithSelector builds on top of this, so the TSelected can defer to an equality check to determine if the change is propagated.
|
|
186
|
+
// The selector here just maps the TSelected, useSyncExternalStoreWithSelector internally does the equality check.
|
|
187
|
+
wrappedSelector: (snapshot: TModel) => {
|
|
188
|
+
return snapshot
|
|
189
|
+
? selector(snapshot)
|
|
190
|
+
: null;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const createNoopSubscriptionState = () => ({
|
|
196
|
+
subscribe: () => {
|
|
197
|
+
return () => {};
|
|
198
|
+
},
|
|
188
199
|
getSnapshot: () => null,
|
|
189
|
-
wrappedSelector: () =>
|
|
200
|
+
wrappedSelector: () => null
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const warnIfModelInstanceHasNotChanged = (modelId: string, lastModel: any, nextModel: string) => {
|
|
204
|
+
if (process.env.NODE_ENV === 'production') {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (utils.stringIsEmpty(modelId)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (lastModel === nextModel) {
|
|
211
|
+
let stack: string | undefined = undefined;
|
|
212
|
+
try {
|
|
213
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
214
|
+
throw new Error();
|
|
215
|
+
} catch (e) {
|
|
216
|
+
stack = (e as Error).stack;
|
|
217
|
+
}
|
|
218
|
+
logger.warn(
|
|
219
|
+
`useSyncModelWithSelector had detected the latest model update hasn't mutated. This will cause odd render bugs. modelId: ${modelId}`,
|
|
220
|
+
stack
|
|
221
|
+
);
|
|
222
|
+
}
|
|
190
223
|
};
|
|
@@ -5,55 +5,66 @@ import {
|
|
|
5
5
|
connect,
|
|
6
6
|
ConnectableComponent,
|
|
7
7
|
ConnectableComponentProps,
|
|
8
|
-
getEspPolimerImmutableModel,
|
|
9
8
|
PublishModelEventDelegate,
|
|
10
9
|
viewBinding,
|
|
11
10
|
MapModelToProps,
|
|
12
11
|
CreatePublishEventProps,
|
|
13
|
-
GetEspPolimerImmutableModelConsts,
|
|
14
12
|
} from '../src';
|
|
15
13
|
import {testApi, TestApi} from './testApi/testApi';
|
|
16
14
|
import {observeEvent} from 'esp-js';
|
|
17
15
|
import {viewFactory} from './testApi/viewFactory';
|
|
16
|
+
import {GetEspPolimerImmutableModelFunctionName} from '../src/polimer/getEspPolimerImmutableModel';
|
|
18
17
|
|
|
19
|
-
interface
|
|
18
|
+
interface TestModelState {
|
|
20
19
|
value: string;
|
|
21
20
|
value2: string;
|
|
22
21
|
value3: string;
|
|
22
|
+
foo: string;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
@viewBinding(viewFactory('View1'))
|
|
26
26
|
@viewBinding(viewFactory('View3'), 'alternative-view-context')
|
|
27
27
|
class TestModel {
|
|
28
|
-
|
|
28
|
+
protected _state: TestModelState = {
|
|
29
|
+
value: 'initial-value',
|
|
30
|
+
value2: undefined,
|
|
31
|
+
value3: undefined,
|
|
32
|
+
foo: undefined
|
|
33
|
+
};
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
this.value
|
|
35
|
+
public get value(): string {
|
|
36
|
+
return this._state.value;
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
@observeEvent('test-event')
|
|
35
40
|
_onTestEvent(ev: string) {
|
|
36
|
-
this.
|
|
41
|
+
this._updateState({
|
|
42
|
+
value: ev
|
|
43
|
+
});
|
|
37
44
|
}
|
|
38
|
-
}
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
value2: this.value,
|
|
46
|
-
value3: 'value-3'
|
|
47
|
-
} as MappedModel;
|
|
46
|
+
protected _updateState = (partialState: Partial<TestModelState>) => {
|
|
47
|
+
this._state = {
|
|
48
|
+
...this._state,
|
|
49
|
+
...partialState
|
|
50
|
+
};
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
class
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
class TestModel2 extends TestModel {
|
|
55
|
+
|
|
56
|
+
constructor() {
|
|
57
|
+
super();
|
|
58
|
+
this._updateState({
|
|
59
|
+
value: this._state.value,
|
|
60
|
+
value2: this._state.value,
|
|
61
|
+
value3: 'value-3',
|
|
55
62
|
foo: 'the-foo'
|
|
56
|
-
};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
[GetEspPolimerImmutableModelFunctionName] = () => {
|
|
67
|
+
return this._state;
|
|
57
68
|
};
|
|
58
69
|
}
|
|
59
70
|
|
|
@@ -71,17 +82,14 @@ interface ConnectedComponentElementCreationProperties {
|
|
|
71
82
|
describe('ConnectableComponentTests', () => {
|
|
72
83
|
let api: TestApi,
|
|
73
84
|
testModel: TestModel,
|
|
74
|
-
testModel2: TestModel2
|
|
75
|
-
testModel3: TestModel3;
|
|
85
|
+
testModel2: TestModel2;
|
|
76
86
|
|
|
77
87
|
beforeEach(() => {
|
|
78
88
|
api = testApi();
|
|
79
89
|
testModel = new TestModel();
|
|
80
90
|
testModel2 = new TestModel2();
|
|
81
|
-
testModel3 = new TestModel3();
|
|
82
91
|
api.setupModel('model-id1', testModel);
|
|
83
92
|
api.setupModel('model-id2', testModel2);
|
|
84
|
-
api.setupModel('model-id3', testModel3);
|
|
85
93
|
});
|
|
86
94
|
|
|
87
95
|
const createConnectedComponentElement = (options: ConnectedComponentElementCreationProperties): React.JSX.Element => {
|
|
@@ -101,7 +109,7 @@ describe('ConnectableComponentTests', () => {
|
|
|
101
109
|
let publishEventProps: { publishEvent1: () => void };
|
|
102
110
|
let userAlternativeViewContext: string;
|
|
103
111
|
if (options.useMapModelToProps) {
|
|
104
|
-
mapModelToProps = (model: TestModel |
|
|
112
|
+
mapModelToProps = (model: TestModel | TestModelState, publishEventProps2: any) => {
|
|
105
113
|
return {
|
|
106
114
|
foo: model.value,
|
|
107
115
|
publishEventPropsPassedMapModelToProps: publishEventProps2 && typeof publishEventProps2.publishEvent1 === 'function',
|
|
@@ -378,7 +386,6 @@ describe('ConnectableComponentTests', () => {
|
|
|
378
386
|
2,
|
|
379
387
|
props => {
|
|
380
388
|
expect(props.model.value).toBe('the-event-value2');
|
|
381
|
-
expect(props.foo).toBe('the-event-value2');
|
|
382
389
|
}
|
|
383
390
|
);
|
|
384
391
|
});
|
|
@@ -430,7 +437,7 @@ describe('ConnectableComponentTests', () => {
|
|
|
430
437
|
});
|
|
431
438
|
|
|
432
439
|
it('can pass view via prop', () => {
|
|
433
|
-
const View = viewFactory('MyPropView')
|
|
440
|
+
const View = viewFactory('MyPropView');
|
|
434
441
|
api.doRender(
|
|
435
442
|
(
|
|
436
443
|
<ConnectableComponent view={View} />
|
|
@@ -442,38 +449,12 @@ describe('ConnectableComponentTests', () => {
|
|
|
442
449
|
});
|
|
443
450
|
|
|
444
451
|
describe('Overriding model/state selector with @getEspPolimerImmutableModel', () => {
|
|
445
|
-
it('The model returned by the function decorated with a @getEspPolimerImmutableModel decorator is passed to useMapModelToProps', () => {
|
|
446
|
-
let elementCreationProperties: ConnectedComponentElementCreationProperties = {
|
|
447
|
-
modelId: 'model-id2', // this model has a @getEspPolimerImmutableModel decorator
|
|
448
|
-
useConnectFunction: false,
|
|
449
|
-
useAlternativeViewContext: true
|
|
450
|
-
};
|
|
451
|
-
api.doRender(
|
|
452
|
-
createConnectedComponentElement(elementCreationProperties),
|
|
453
|
-
elementCreationProperties.modelId
|
|
454
|
-
);
|
|
455
|
-
api.asserts.props
|
|
456
|
-
.receivedPropCountIs(1)
|
|
457
|
-
.propAtIndex(0, props => {
|
|
458
|
-
const mappedModel = props.model as any as MappedModel;
|
|
459
|
-
expect(mappedModel.value2).toBe('initial-value');
|
|
460
|
-
expect(mappedModel.value3).toBe('value-3');
|
|
461
|
-
});
|
|
462
|
-
publishTestEvent(elementCreationProperties.modelId, 'the-event-value');
|
|
463
|
-
api.asserts.props
|
|
464
|
-
.receivedPropCountIs(2)
|
|
465
|
-
.propAtIndex(1, props => {
|
|
466
|
-
const mappedModel = props.model as any as MappedModel;
|
|
467
|
-
expect(mappedModel.value2).toBe('the-event-value');
|
|
468
|
-
});
|
|
469
|
-
});
|
|
470
|
-
|
|
471
452
|
it('will select esp-js-polimer model based on const', () => {
|
|
472
453
|
api.doRender(
|
|
473
454
|
(
|
|
474
455
|
<ConnectableComponent />
|
|
475
456
|
),
|
|
476
|
-
'model-
|
|
457
|
+
'model-id2'
|
|
477
458
|
);
|
|
478
459
|
api.asserts.props
|
|
479
460
|
.receivedPropCountIs(1)
|
|
@@ -482,7 +463,7 @@ describe('ConnectableComponentTests', () => {
|
|
|
482
463
|
expect(props.model.foo).toBe('the-foo');
|
|
483
464
|
});
|
|
484
465
|
act(() => {
|
|
485
|
-
api.router.publishEvent('model-
|
|
466
|
+
api.router.publishEvent('model-id2', 'test-event', 'changed-value');
|
|
486
467
|
});
|
|
487
468
|
api.asserts.props
|
|
488
469
|
.receivedPropCountIs(2)
|
package/tests/indexTests.ts
CHANGED
|
@@ -23,8 +23,6 @@ describe('index exports', () => {
|
|
|
23
23
|
expect(espReact.viewBinding).toBeDefined();
|
|
24
24
|
expect(espReact.ViewBinder).toBeDefined();
|
|
25
25
|
expect(espReact.DEFAULT_VIEW_KEY).toBeDefined();
|
|
26
|
-
expect(espReact.GetEspPolimerImmutableModelConsts).toBeDefined();
|
|
27
|
-
expect(espReact.getEspPolimerImmutableModel).toBeDefined();
|
|
28
26
|
expect(espReact.createViewForModel).toBeDefined();
|
|
29
27
|
expect(espReact.connect).toBeDefined();
|
|
30
28
|
expect(espReact.ConnectableComponent).toBeDefined();
|
package/tests/testApi/asserts.ts
CHANGED
|
@@ -5,8 +5,10 @@ import {RouterSpy} from './routerSpy';
|
|
|
5
5
|
import {TestPropStore} from './useStoreReceivedProps';
|
|
6
6
|
|
|
7
7
|
export type ViewAsserts = {
|
|
8
|
-
valueIs(expected: string): ViewAsserts;
|
|
9
8
|
modelIdIs(expected: string): ViewAsserts
|
|
9
|
+
modelIdIsNotInDom(): ViewAsserts
|
|
10
|
+
valueIs(expected: string): ViewAsserts;
|
|
11
|
+
valueIsNotInDom(): ViewAsserts;
|
|
10
12
|
propIs(propName: string, expected: string): ViewAsserts
|
|
11
13
|
viewNameElementTextIs(viewName: string);
|
|
12
14
|
htmlElementInnerTextIs(elementsTestId: string, expectedText: string): ViewAsserts
|
|
@@ -19,11 +21,21 @@ export const viewAsserts = (renderResult: RenderResult) => {
|
|
|
19
21
|
expect(idElement).toHaveTextContent(expected);
|
|
20
22
|
return this;
|
|
21
23
|
},
|
|
24
|
+
modelIdIsNotInDom() {
|
|
25
|
+
let idElement = renderResult.queryByTestId('modelIdDisplay');
|
|
26
|
+
expect(idElement).not.toBeInTheDocument();
|
|
27
|
+
return this;
|
|
28
|
+
},
|
|
22
29
|
valueIs(expected: string) {
|
|
23
30
|
let valueElement = renderResult.getByTestId('valueDisplay');
|
|
24
31
|
expect(valueElement).toHaveTextContent(expected);
|
|
25
32
|
return this;
|
|
26
33
|
},
|
|
34
|
+
valueIsNotInDom() {
|
|
35
|
+
let idElement = renderResult.queryByTestId('valueDisplay');
|
|
36
|
+
expect(idElement).not.toBeInTheDocument();
|
|
37
|
+
return this;
|
|
38
|
+
},
|
|
27
39
|
propIs(propName: string, expected: string) {
|
|
28
40
|
let span = renderResult.getByTestId('span-with-props-as-attributes');
|
|
29
41
|
let attributeName = `data-${propName}`;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {EventContext, observeEvent} from 'esp-js';
|
|
2
|
-
import {
|
|
2
|
+
import {GetEspPolimerImmutableModelFunctionName} from '../../src/polimer/getEspPolimerImmutableModel';
|
|
3
3
|
|
|
4
4
|
export type TestModelImmutableState = { value: string, modelId: string, entityKey: string };
|
|
5
5
|
|
|
@@ -38,7 +38,7 @@ export class TestModel {
|
|
|
38
38
|
};
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
[
|
|
41
|
+
[GetEspPolimerImmutableModelFunctionName]: () => TestModelImmutableState = () => {
|
|
42
42
|
return this._internalState;
|
|
43
43
|
};
|
|
44
44
|
}
|
|
@@ -2,7 +2,7 @@ import {useTestPropStore} from './useStoreReceivedProps';
|
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
|
|
4
4
|
export const viewFactory = (name: string) => {
|
|
5
|
-
return (props: { modelId
|
|
5
|
+
return (props: { modelId?: string; value?: string, [key: string]: any }) => {
|
|
6
6
|
let testPropStore = useTestPropStore();
|
|
7
7
|
testPropStore.pushProps(props);
|
|
8
8
|
return (
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import 'jest';
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import {act} from 'react';
|
|
4
|
-
import {syncModelWithSelectorOptions, useSyncModelWithSelector} from '../src';
|
|
4
|
+
import {EspModelContextProvider, EspRouterContextProvider, syncModelWithSelectorOptions, useSyncModelWithSelector} from '../src';
|
|
5
5
|
import {TestModel, TestModelImmutableState} from './testApi/testModel';
|
|
6
|
-
import {ViewMetadata} from './testApi/viewFactory';
|
|
6
|
+
import {viewFactory, ViewMetadata} from './testApi/viewFactory';
|
|
7
7
|
import {routerAsserts} from './testApi/asserts';
|
|
8
8
|
import {testApi, TestApi} from './testApi/testApi';
|
|
9
|
+
import {TestPropStoreContext} from './testApi/useStoreReceivedProps';
|
|
9
10
|
|
|
10
11
|
type SelectedObject = { id: string, value: string };
|
|
11
12
|
|
|
@@ -171,8 +172,8 @@ describe('useSyncModelWithSelector', () => {
|
|
|
171
172
|
.valueIs('initial-value');
|
|
172
173
|
api.doReRender(View, undefined);
|
|
173
174
|
api.asserts.view
|
|
174
|
-
.
|
|
175
|
-
.
|
|
175
|
+
.modelIdIsNotInDom()
|
|
176
|
+
.valueIsNotInDom();
|
|
176
177
|
api.setupTestModel(modelId2);
|
|
177
178
|
api.doReRender(View, modelId2);
|
|
178
179
|
api.asserts.view
|
|
@@ -253,6 +254,48 @@ describe('useSyncModelWithSelector', () => {
|
|
|
253
254
|
});
|
|
254
255
|
});
|
|
255
256
|
|
|
257
|
+
describe('noop argument edge cases', () => {
|
|
258
|
+
const View = viewFactory('View1');
|
|
259
|
+
|
|
260
|
+
it('falsey router', () => {
|
|
261
|
+
api.doRender((
|
|
262
|
+
<TestPropStoreContext.Provider value={api.propStore}>
|
|
263
|
+
<EspRouterContextProvider router={undefined}>
|
|
264
|
+
<EspModelContextProvider modelId={'some-modelId'}>
|
|
265
|
+
<View />,
|
|
266
|
+
</EspModelContextProvider>
|
|
267
|
+
</EspRouterContextProvider>
|
|
268
|
+
</TestPropStoreContext.Provider>
|
|
269
|
+
));
|
|
270
|
+
api.asserts.props
|
|
271
|
+
.receivedPropCountIs(1)
|
|
272
|
+
.propAtIndexHasModelId(0, undefined)
|
|
273
|
+
.propAtIndex(0, props => {
|
|
274
|
+
expect(props.modelId).not.toBeDefined();
|
|
275
|
+
expect(props.value).not.toBeDefined();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('falsey modelId', () => {
|
|
280
|
+
api.doRender((
|
|
281
|
+
<TestPropStoreContext.Provider value={api.propStore}>
|
|
282
|
+
<EspRouterContextProvider router={api.router}>
|
|
283
|
+
<EspModelContextProvider modelId={undefined}>
|
|
284
|
+
<View />,
|
|
285
|
+
</EspModelContextProvider>
|
|
286
|
+
</EspRouterContextProvider>
|
|
287
|
+
</TestPropStoreContext.Provider>
|
|
288
|
+
));
|
|
289
|
+
api.asserts.props
|
|
290
|
+
.receivedPropCountIs(1)
|
|
291
|
+
.propAtIndexHasModelId(0, undefined)
|
|
292
|
+
.propAtIndex(0, props => {
|
|
293
|
+
expect(props.modelId).not.toBeDefined();
|
|
294
|
+
expect(props.value).not.toBeDefined();
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
256
299
|
describe('argument checking', () => {
|
|
257
300
|
it('throws if selector incorrect', () => {
|
|
258
301
|
expect(() => {
|