esp-js-react 7.0.2 → 8.0.1-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 +468 -511
- package/.dist/esp-react.js.map +1 -1
- package/.dist/esp-react.min.js +2 -1
- package/.dist/esp-react.min.js.LICENSE.txt +9 -0
- package/.dist/esp-react.min.js.map +1 -1
- package/.dist/typings/connect.d.ts +12 -0
- package/.dist/typings/connectableComponent.d.ts +20 -46
- package/.dist/typings/{espModelContext.d.ts → espModelContextProvider.d.ts} +8 -9
- package/.dist/typings/espRouterContextProvider.d.ts +47 -0
- package/.dist/typings/index.d.ts +7 -6
- package/.dist/typings/{getEspReactRenderModel.d.ts → polimer/getEspReactRenderModel.d.ts} +7 -0
- package/.dist/typings/useModelSelector.d.ts +27 -0
- package/.dist/typings/viewBinder.d.ts +1 -8
- package/.dist/typings/viewBinderConnectable.d.ts +15 -0
- package/.dist/typings/viewBindingDecorator.d.ts +1 -2
- package/package.json +7 -6
- package/src/connect.tsx +31 -0
- package/src/connectableComponent.tsx +94 -190
- package/src/espModelContextProvider.tsx +95 -0
- package/src/espRouterContextProvider.tsx +88 -0
- package/src/index.ts +40 -18
- package/src/polimer/getEspReactRenderModel.ts +50 -0
- package/src/useModelSelector.ts +159 -0
- package/src/viewBinder.tsx +14 -22
- package/src/viewBinderConnectable.tsx +54 -0
- package/src/viewBindingDecorator.ts +2 -3
- package/tests/connectableComponentLegacyTests.tsx +405 -0
- package/tests/indexTests.ts +0 -12
- package/tests/routerSpy.ts +24 -0
- package/tests/useModelSelectorTests.tsx +361 -0
- package/.dist/typings/routerProvider.d.ts +0 -18
- package/.dist/typings/shallowEqual.d.ts +0 -21
- package/.dist/typings/shouldUpdateMixin.d.ts +0 -10
- package/.dist/typings/smartComponent.d.ts +0 -33
- package/.dist/typings/utils.d.ts +0 -5
- package/src/espModelContext.tsx +0 -69
- package/src/getEspReactRenderModel.ts +0 -27
- package/src/routerProvider.tsx +0 -47
- package/src/shallowEqual.ts +0 -70
- package/src/shouldUpdateMixin.ts +0 -73
- package/src/smartComponent.tsx +0 -93
- package/src/utils.ts +0 -61
- package/tests/connectableComponentTests.tsx +0 -390
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {useMemo} from 'react';
|
|
2
|
+
import {useSyncExternalStoreWithSelector} from 'use-sync-external-store/with-selector';
|
|
3
|
+
import {Guard, utils} from 'esp-js';
|
|
4
|
+
import {useRouter} from './espRouterContextProvider';
|
|
5
|
+
import {useGetModelId} from './espModelContextProvider';
|
|
6
|
+
import {tryGetRenderModel} from './polimer/getEspReactRenderModel';
|
|
7
|
+
|
|
8
|
+
export type ModelSelectorEqualityFn<T> = (last: T, next: T) => boolean;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param modelId - the modelId of the model to be observed via the esp Router.
|
|
12
|
+
* @param tryPreSelectPolimerImmutableModel - if true, and the esp model is an esp-js-polimer model, the immutable model will be used when calling the selector.
|
|
13
|
+
* @param equalityFn - Equality function which will be applied against TSelected, defaults to instance equality (a === b).
|
|
14
|
+
*/
|
|
15
|
+
export type ModelSelectorOptions<TSelected> = {
|
|
16
|
+
modelId?: string,
|
|
17
|
+
tryPreSelectPolimerImmutableModel: boolean,
|
|
18
|
+
equalityFn: ModelSelectorEqualityFn<TSelected>
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export interface EditableModelSelectorOptions<TSelected> extends ModelSelectorOptions<TSelected> {
|
|
22
|
+
setModelId(modelId: string): this;
|
|
23
|
+
|
|
24
|
+
setTryPreSelectPolimerImmutableModel(value: boolean): this;
|
|
25
|
+
|
|
26
|
+
setEqualityFn(fn: (a: TSelected, b: TSelected) => boolean): this;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const modelSelectorOptions = <TSelected>() => {
|
|
30
|
+
let modelId = undefined;
|
|
31
|
+
let equalityFn: ModelSelectorEqualityFn<TSelected> = (a: TSelected, b: TSelected) => a === b;
|
|
32
|
+
let tryPreSelectPolimerImmutableModel = true;
|
|
33
|
+
return {
|
|
34
|
+
get modelId() {
|
|
35
|
+
return modelId;
|
|
36
|
+
},
|
|
37
|
+
setModelId(value: string) {
|
|
38
|
+
modelId = value;
|
|
39
|
+
return this;
|
|
40
|
+
},
|
|
41
|
+
get equalityFn() {
|
|
42
|
+
return equalityFn;
|
|
43
|
+
},
|
|
44
|
+
setEqualityFn(value: ModelSelectorEqualityFn<TSelected>) {
|
|
45
|
+
equalityFn = value;
|
|
46
|
+
return this;
|
|
47
|
+
},
|
|
48
|
+
get tryPreSelectPolimerImmutableModel() {
|
|
49
|
+
return tryPreSelectPolimerImmutableModel;
|
|
50
|
+
},
|
|
51
|
+
setTryPreSelectPolimerImmutableModel(value: boolean) {
|
|
52
|
+
tryPreSelectPolimerImmutableModel = value;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
} as EditableModelSelectorOptions<TSelected>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type UseModelSelector = <TModel, TSelected>(
|
|
59
|
+
selector: (model: TModel) => TSelected,
|
|
60
|
+
options?: ModelSelectorOptions<TSelected>
|
|
61
|
+
) => TSelected;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A hook which returns model state from the esp Router.
|
|
65
|
+
*
|
|
66
|
+
* This hooks wih observe updates and re-render the component if the model changes.
|
|
67
|
+
*
|
|
68
|
+
* @param selector - a function to select a selection of the model - the selector does not need to be memoized, this selector will be cached against the modelId.
|
|
69
|
+
* @param options
|
|
70
|
+
*/
|
|
71
|
+
export const useModelSelector: UseModelSelector = <TModel, TSelected>(
|
|
72
|
+
selector: (model: TModel) => TSelected,
|
|
73
|
+
options: ModelSelectorOptions<TSelected> = modelSelectorOptions<TSelected>()
|
|
74
|
+
) => {
|
|
75
|
+
Guard.isFunction(selector, 'You must pass a selector function to connectSelector');
|
|
76
|
+
const router = useRouter();
|
|
77
|
+
const modelId = options?.modelId || useGetModelId();
|
|
78
|
+
const tryPreSelectPolimerImmutableModel = options?.tryPreSelectPolimerImmutableModel;
|
|
79
|
+
const equalityFn = options?.equalityFn;
|
|
80
|
+
const dependencies = useMemo(
|
|
81
|
+
() => {
|
|
82
|
+
// Because of how useSyncExternalStore works, there is an implicit dependency between the subscribe and getSnapshot functions.
|
|
83
|
+
// When the subscription receives the new state from the Router, it can't pass this directly onto getSnapshot, React calls that when it deems it needs to.
|
|
84
|
+
// Given that, we need to cache the state which getSnapshot will return.
|
|
85
|
+
//
|
|
86
|
+
// It's not ideal but shouldn't be a problem as here we've wrapped all these dependent functions inside a single useMemo()
|
|
87
|
+
let model: TModel = null;
|
|
88
|
+
return {
|
|
89
|
+
subscribe: (stateChanged: () => void) => {
|
|
90
|
+
const modelSubscriptionDisposable = router
|
|
91
|
+
.getModelObservable(modelId)
|
|
92
|
+
.subscribe(
|
|
93
|
+
(m: any) => {
|
|
94
|
+
// try and get a submodel to render
|
|
95
|
+
const nextModel = tryPreSelectPolimerImmutableModel
|
|
96
|
+
? tryGetRenderModel(m)
|
|
97
|
+
: m;
|
|
98
|
+
// if the above didn't manage to get a submodel,
|
|
99
|
+
// we need to mutate to force useSyncExternalStoreWithSelector to pick up the change
|
|
100
|
+
model = nextModel === m
|
|
101
|
+
? Object.create(nextModel)
|
|
102
|
+
: nextModel;
|
|
103
|
+
stateChanged();
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
return () => {
|
|
107
|
+
model = null;
|
|
108
|
+
modelSubscriptionDisposable.dispose();
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
// React expects getSnapshot to be immutable, it'll only re-render if the instance changes
|
|
112
|
+
getSnapshot: () => {
|
|
113
|
+
return model;
|
|
114
|
+
},
|
|
115
|
+
// While getSnapshot needs to model to be immutable,
|
|
116
|
+
// useSyncExternalStoreWithSelector builds on top of this, so the TSelected can defer to an equality check to determine if the change is propagated.
|
|
117
|
+
// The selector here just maps the TSelected, useSyncExternalStoreWithSelector internally does the equality check.
|
|
118
|
+
wrappedSelector: (snapshot: TModel) => {
|
|
119
|
+
// The first time this renders, react calls getSnapshot and the selector before the router has pushed a change.
|
|
120
|
+
// In that case, there is no state for any component to process, therefore, we return null;
|
|
121
|
+
return snapshot
|
|
122
|
+
? selector(snapshot)
|
|
123
|
+
: null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
// Don't cache check against 'selector' argument as that will likely change unless the caller memoizes it.
|
|
128
|
+
[router, modelId]
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
// If we don't have a modelId, we still need to return a hook so React doesn't complain.
|
|
132
|
+
// Additionally, we need to let the above expiration (dependencies) run to unsubscribe from any previous subscription.
|
|
133
|
+
if (utils.stringIsEmpty(modelId)) {
|
|
134
|
+
return useSyncExternalStoreWithSelector(
|
|
135
|
+
noopSubscription.subscribe,
|
|
136
|
+
noopSubscription.getSnapshot,
|
|
137
|
+
undefined,
|
|
138
|
+
noopSubscription.wrappedSelector,
|
|
139
|
+
equalityFn
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Docs on useSyncExternalStore https://github.com/reactwg/react-18/discussions/86
|
|
144
|
+
// Redux's usage of this: https://github.com/reduxjs/react-redux/blob/master/src/hooks/useSelector.ts
|
|
145
|
+
// Code: https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js
|
|
146
|
+
return useSyncExternalStoreWithSelector(
|
|
147
|
+
dependencies.subscribe,
|
|
148
|
+
dependencies.getSnapshot,
|
|
149
|
+
undefined,
|
|
150
|
+
dependencies.wrappedSelector,
|
|
151
|
+
equalityFn,
|
|
152
|
+
);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const noopSubscription = {
|
|
156
|
+
subscribe: () => { },
|
|
157
|
+
getSnapshot: () => null,
|
|
158
|
+
wrappedSelector: () => true
|
|
159
|
+
};
|
package/src/viewBinder.tsx
CHANGED
|
@@ -16,33 +16,25 @@
|
|
|
16
16
|
*/
|
|
17
17
|
// notice_end
|
|
18
18
|
|
|
19
|
-
import * as esp from 'esp-js';
|
|
20
19
|
import * as React from 'react';
|
|
21
|
-
import
|
|
22
|
-
import {
|
|
20
|
+
import {createViewForModel} from './viewBindingDecorator';
|
|
21
|
+
import {useRouter} from './espRouterContextProvider';
|
|
23
22
|
|
|
24
23
|
export interface ViewBinderProps {
|
|
25
24
|
model: any;
|
|
26
25
|
viewContext: string;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return createViewForModel(
|
|
40
|
-
this.props.model,
|
|
41
|
-
newProps,
|
|
42
|
-
this.props.viewContext,
|
|
43
|
-
null
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
28
|
+
export const ViewBinder: React.FC<ViewBinderProps> = ({model, viewContext, ...rest}) => {
|
|
29
|
+
if (model) {
|
|
30
|
+
const router = useRouter();
|
|
31
|
+
const newProps = Object.assign({}, {model, router}, rest);
|
|
32
|
+
return createViewForModel(
|
|
33
|
+
model,
|
|
34
|
+
newProps,
|
|
35
|
+
viewContext,
|
|
36
|
+
null
|
|
37
|
+
);
|
|
47
38
|
}
|
|
48
|
-
|
|
39
|
+
return null;
|
|
40
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {useRouter} from './espRouterContextProvider';
|
|
2
|
+
import {EspModelContextProvider} from './espModelContextProvider';
|
|
3
|
+
import {Logger, Router} from 'esp-js';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import {modelSelectorOptions, useModelSelector} from './useModelSelector';
|
|
6
|
+
import {createViewForModel} from './viewBindingDecorator';
|
|
7
|
+
import {tryGetRenderModel} from './polimer/getEspReactRenderModel';
|
|
8
|
+
|
|
9
|
+
const _log = Logger.create('ViewBinderConnectable');
|
|
10
|
+
|
|
11
|
+
export interface ViewBinderConnectableProps {
|
|
12
|
+
modelId?: string;
|
|
13
|
+
viewContext?: string;
|
|
14
|
+
view?: React.ComponentType;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ViewBinderConnectableChildProps {
|
|
18
|
+
modelId: string;
|
|
19
|
+
model: object;
|
|
20
|
+
router: Router;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Dynamically binds a view found via @viewBinder(SomeJsXView) to a model managed by the Router.
|
|
25
|
+
* Optionally provide a view override and/or a viewContext
|
|
26
|
+
* @param modelId
|
|
27
|
+
* @param viewContext
|
|
28
|
+
* @param view
|
|
29
|
+
* @constructor
|
|
30
|
+
*/
|
|
31
|
+
export const ViewBinderConnectable = ({modelId, viewContext, view}: ViewBinderConnectableProps) => {
|
|
32
|
+
const router = useRouter();
|
|
33
|
+
const model = useModelSelector<object, object>(
|
|
34
|
+
m => m,
|
|
35
|
+
modelSelectorOptions()
|
|
36
|
+
.setModelId(modelId)
|
|
37
|
+
.setTryPreSelectPolimerImmutableModel(false)
|
|
38
|
+
);
|
|
39
|
+
if (!model) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
let renderModel = tryGetRenderModel<object>(model);
|
|
43
|
+
const childProps: ViewBinderConnectableChildProps = {
|
|
44
|
+
modelId,
|
|
45
|
+
router: router,
|
|
46
|
+
model: renderModel
|
|
47
|
+
};
|
|
48
|
+
let viewElement = createViewForModel(model, childProps, viewContext, view);
|
|
49
|
+
return (
|
|
50
|
+
<EspModelContextProvider modelId={modelId} router={router} model={renderModel} {...childProps}>
|
|
51
|
+
{viewElement}
|
|
52
|
+
</EspModelContextProvider>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
|
|
19
19
|
import * as React from 'react';
|
|
20
20
|
import {Guard} from 'esp-js';
|
|
21
|
-
import {ViewComponentTypes} from './connectableComponent';
|
|
22
21
|
|
|
23
22
|
export const DEFAULT_VIEW_KEY = 'default-view-key';
|
|
24
23
|
|
|
@@ -41,8 +40,8 @@ function getMetadata(target) {
|
|
|
41
40
|
* @param displayContext
|
|
42
41
|
* @param view
|
|
43
42
|
*/
|
|
44
|
-
export function createViewForModel(model: any, props, displayContext: string, view:
|
|
45
|
-
let finalView:
|
|
43
|
+
export function createViewForModel(model: any, props, displayContext: string, view: React.ComponentType) {
|
|
44
|
+
let finalView: React.ComponentType = view; // default to that passed in, if any.
|
|
46
45
|
|
|
47
46
|
// displayContext = displayContext || DEFAULT_VIEW_KEY;
|
|
48
47
|
// the view decorator isn't on the instance, it's on the constructor function that created that instance
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import 'jest';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import {
|
|
4
|
+
connect,
|
|
5
|
+
ConnectableComponent,
|
|
6
|
+
ConnectableComponentProps,
|
|
7
|
+
viewBinding,
|
|
8
|
+
getEspReactRenderModel,
|
|
9
|
+
PublishModelEventDelegate,
|
|
10
|
+
RouterContext,
|
|
11
|
+
EspRouterContextProvider
|
|
12
|
+
} from '../src';
|
|
13
|
+
import {Router, observeEvent, utils} from 'esp-js';
|
|
14
|
+
import {render, RenderResult} from '@testing-library/react';
|
|
15
|
+
|
|
16
|
+
const getPropsAsDataAttributesWithMetadata = (props: any) => {
|
|
17
|
+
const propsAsDataAttributes: {[key: string]: string} = Object.keys(props).reduce((result: any, key: string) => {
|
|
18
|
+
let prop = props[key];
|
|
19
|
+
// need to lower case data-* attributes else React throws a warning
|
|
20
|
+
result[`data-${key.toLowerCase()}`] = utils.isString(prop)
|
|
21
|
+
? prop
|
|
22
|
+
: typeof prop;
|
|
23
|
+
return result;
|
|
24
|
+
}, {});
|
|
25
|
+
return propsAsDataAttributes;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class TestModelView1 extends React.Component {
|
|
29
|
+
constructor(props) {
|
|
30
|
+
super(props);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
render() {
|
|
34
|
+
return <div>
|
|
35
|
+
<span data-testid='span-with-props-as-attributes' {...getPropsAsDataAttributesWithMetadata(this.props)} />
|
|
36
|
+
<span data-testid='view-text'>View1</span>
|
|
37
|
+
</div>;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class TestModelView2 extends React.Component {
|
|
42
|
+
constructor(props) {
|
|
43
|
+
super(props);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
render() {
|
|
47
|
+
return <div>
|
|
48
|
+
<span data-testid='span-with-props-as-attributes' {...getPropsAsDataAttributesWithMetadata(this.props)} />
|
|
49
|
+
<span data-testid='view-text'>View2</span>
|
|
50
|
+
</div>;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class TestModelView3 extends React.Component {
|
|
55
|
+
constructor(props) {
|
|
56
|
+
super(props);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
render() {
|
|
60
|
+
return <span>View3</span>;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@viewBinding(TestModelView1)
|
|
65
|
+
@viewBinding(TestModelView3, 'alternative-view-context')
|
|
66
|
+
class TestModel {
|
|
67
|
+
public value: string;
|
|
68
|
+
|
|
69
|
+
constructor() {
|
|
70
|
+
this.value = 'initial-value';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@observeEvent('test-event')
|
|
74
|
+
_onTestEvent(ev) {
|
|
75
|
+
this.value = ev;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class TestModel2 extends TestModel {
|
|
80
|
+
@getEspReactRenderModel()
|
|
81
|
+
_getOtherState() {
|
|
82
|
+
return {
|
|
83
|
+
value: this.value,
|
|
84
|
+
value2: this.value,
|
|
85
|
+
value3: 'value-3'
|
|
86
|
+
} as MappedModel;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class TestModel3 extends TestModel {
|
|
91
|
+
getEspReactRenderModel() {
|
|
92
|
+
return {
|
|
93
|
+
value: this.value,
|
|
94
|
+
value2: this.value,
|
|
95
|
+
value3: 'value-3.1'
|
|
96
|
+
} as MappedModel;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface MappedModel {
|
|
101
|
+
value: string;
|
|
102
|
+
value2: string;
|
|
103
|
+
value3: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface TestOptions {
|
|
107
|
+
useConnectFunction?: boolean;
|
|
108
|
+
useMapModelToProps?: boolean;
|
|
109
|
+
useCreatePublishEventProps?: boolean;
|
|
110
|
+
useAlternativeViewContext?: boolean;
|
|
111
|
+
passOtherProps?: boolean;
|
|
112
|
+
useRenderModelSelectorDecorator?: boolean;
|
|
113
|
+
useRenderModelSelectorFunction?: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
describe('ConnectableComponent', () => {
|
|
117
|
+
let router,
|
|
118
|
+
testModel: TestModel,
|
|
119
|
+
testModel2: TestModel,
|
|
120
|
+
renderResult: RenderResult,
|
|
121
|
+
connectableComponentProps: ConnectableComponentProps<TestModel>,
|
|
122
|
+
mapModelToProps,
|
|
123
|
+
createPublishEventProps,
|
|
124
|
+
publishEventProps,
|
|
125
|
+
userAlternativeViewContext,
|
|
126
|
+
otherProps = {other1: 'other-value'};
|
|
127
|
+
|
|
128
|
+
function createModel(options: TestOptions) {
|
|
129
|
+
if (options.useRenderModelSelectorDecorator) {
|
|
130
|
+
return new TestModel2();
|
|
131
|
+
}
|
|
132
|
+
if (options.useRenderModelSelectorFunction) {
|
|
133
|
+
return new TestModel3();
|
|
134
|
+
}
|
|
135
|
+
return new TestModel();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function setup(options: TestOptions) {
|
|
139
|
+
router = new Router();
|
|
140
|
+
testModel = createModel(options);
|
|
141
|
+
router.addModel('model-id', testModel);
|
|
142
|
+
router.observeEventsOn('model-id', testModel);
|
|
143
|
+
testModel2 = new TestModel();
|
|
144
|
+
router.addModel('model-id2', testModel2);
|
|
145
|
+
router.observeEventsOn('model-id2', testModel2);
|
|
146
|
+
connectableComponentProps = {
|
|
147
|
+
modelId: 'model-id'
|
|
148
|
+
};
|
|
149
|
+
if (options.useMapModelToProps) {
|
|
150
|
+
mapModelToProps = (model: TestModel | MappedModel, publishEventProps2: any) => {
|
|
151
|
+
return {
|
|
152
|
+
foo: model.value,
|
|
153
|
+
publishEventProps: publishEventProps2,
|
|
154
|
+
...model
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (options.useCreatePublishEventProps) {
|
|
159
|
+
publishEventProps = {
|
|
160
|
+
publishEvent1: () => {
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
createPublishEventProps = (publishEvent: PublishModelEventDelegate) => publishEventProps;
|
|
164
|
+
}
|
|
165
|
+
if (options.useAlternativeViewContext) {
|
|
166
|
+
userAlternativeViewContext = 'alternative-view-context';
|
|
167
|
+
}
|
|
168
|
+
if (options.useConnectFunction) {
|
|
169
|
+
const TestModelView2ConnectedComponent = connect(
|
|
170
|
+
mapModelToProps,
|
|
171
|
+
createPublishEventProps
|
|
172
|
+
)(TestModelView2);
|
|
173
|
+
renderResult = render(
|
|
174
|
+
<RouterContext.Provider value={router} >
|
|
175
|
+
<TestModelView2ConnectedComponent {...otherProps} {...connectableComponentProps}/>,
|
|
176
|
+
</RouterContext.Provider>
|
|
177
|
+
);
|
|
178
|
+
} else {
|
|
179
|
+
renderResult = render(
|
|
180
|
+
<EspRouterContextProvider router={router} >
|
|
181
|
+
<ConnectableComponent
|
|
182
|
+
{...connectableComponentProps}
|
|
183
|
+
mapModelToProps={mapModelToProps}
|
|
184
|
+
viewContext={userAlternativeViewContext}
|
|
185
|
+
{...otherProps}
|
|
186
|
+
/>
|
|
187
|
+
</EspRouterContextProvider>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function publishAnEventToModel1() {
|
|
193
|
+
router.publishEvent('model-id', 'test-event', 'the-event-value');
|
|
194
|
+
expect(testModel.value).toEqual('the-event-value');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function publishAnEventToModel2() {
|
|
198
|
+
router.publishEvent('model-id2', 'test-event', 'the-event-value2');
|
|
199
|
+
expect(testModel2.value).toEqual('the-event-value2');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
afterEach(() => {
|
|
203
|
+
// console.log(connectableComponentWrapper.debug());
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe('ConnectableComponent via connect()', () => {
|
|
207
|
+
beforeEach(() => {
|
|
208
|
+
setup({useConnectFunction: true, useMapModelToProps: true, useCreatePublishEventProps: true});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const getSpanWithPropsAsAttributes = () => {
|
|
212
|
+
return renderResult.getByTestId('span-with-props-as-attributes');
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
describe('Child view props', () => {
|
|
216
|
+
it('passes mapped props', () => {
|
|
217
|
+
let span = getSpanWithPropsAsAttributes();
|
|
218
|
+
expect(span.getAttribute('data-foo')).toBe('initial-value');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// it('passes publishEventProps to props mapper', () => {
|
|
222
|
+
// let span = getSpanWithPropsAsAttributes();
|
|
223
|
+
// expect(span.getAttribute('publishEventProps')).toBe('function');
|
|
224
|
+
// });
|
|
225
|
+
//
|
|
226
|
+
// it('passes model', () => {
|
|
227
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
228
|
+
// expect(props.model).toBe(testModel);
|
|
229
|
+
// });
|
|
230
|
+
//
|
|
231
|
+
// it('passes publishEventProps', () => {
|
|
232
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
233
|
+
// expect(props.publishEvent1).toBeDefined();
|
|
234
|
+
// });
|
|
235
|
+
//
|
|
236
|
+
// it('passes other props', () => {
|
|
237
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
238
|
+
// expect(props.other1).toEqual('other-value');
|
|
239
|
+
// });
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
// describe('View rendering', () => {
|
|
243
|
+
// it('Renders the view provided to connect', () => {
|
|
244
|
+
// expect(connectableComponentWrapper.containsMatchingElement(<TestModelView2/>)).toBeTruthy();
|
|
245
|
+
// });
|
|
246
|
+
// });
|
|
247
|
+
});
|
|
248
|
+
//
|
|
249
|
+
// describe('ConnectableComponent directly', () => {
|
|
250
|
+
//
|
|
251
|
+
// const getSpanWithPropsAsAttributes = () => {
|
|
252
|
+
// return connectableComponentWrapper.childAt(0).props() as ConnectableComponentChildProps<TestModel>;
|
|
253
|
+
// };
|
|
254
|
+
//
|
|
255
|
+
// describe('Child view props', () => {
|
|
256
|
+
// beforeEach(() => {
|
|
257
|
+
// setup({useConnectFunction: false});
|
|
258
|
+
// });
|
|
259
|
+
//
|
|
260
|
+
// it('passes modelId', () => {
|
|
261
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
262
|
+
// expect(props.modelId).toEqual('model-id');
|
|
263
|
+
// });
|
|
264
|
+
//
|
|
265
|
+
// it('passes router', () => {
|
|
266
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
267
|
+
// expect(props.router).toBe(router);
|
|
268
|
+
// });
|
|
269
|
+
//
|
|
270
|
+
// it('passes model', () => {
|
|
271
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
272
|
+
// expect(props.model).toBe(testModel);
|
|
273
|
+
// });
|
|
274
|
+
//
|
|
275
|
+
// it('passes other props', () => {
|
|
276
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
277
|
+
// expect(props.other1).toEqual('other-value');
|
|
278
|
+
// });
|
|
279
|
+
// });
|
|
280
|
+
//
|
|
281
|
+
// describe('Re-renders', () => {
|
|
282
|
+
// beforeEach(() => {
|
|
283
|
+
// setup({useConnectFunction: false});
|
|
284
|
+
// });
|
|
285
|
+
//
|
|
286
|
+
// it('Re-renders when model updates', () => {
|
|
287
|
+
// publishAnEventToModel1();
|
|
288
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
289
|
+
// expect(props.model.value).toBe('the-event-value');
|
|
290
|
+
// });
|
|
291
|
+
// });
|
|
292
|
+
//
|
|
293
|
+
// describe('Model Subscription', () => {
|
|
294
|
+
// beforeEach(() => {
|
|
295
|
+
// setup({useConnectFunction: false});
|
|
296
|
+
// });
|
|
297
|
+
//
|
|
298
|
+
// it('Re-subscribes to new model when modelId changes', () => {
|
|
299
|
+
// const newProps = {
|
|
300
|
+
// ...connectableComponentWrapper.props(),
|
|
301
|
+
// modelId: 'model-id2',
|
|
302
|
+
// };
|
|
303
|
+
// connectableComponentWrapper.setProps(newProps);
|
|
304
|
+
// publishAnEventToModel2();
|
|
305
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
306
|
+
// expect(props.foo).toBe('the-event-value2');
|
|
307
|
+
// });
|
|
308
|
+
// });
|
|
309
|
+
//
|
|
310
|
+
// describe('Publish Event Context', () => {
|
|
311
|
+
// beforeEach(() => {
|
|
312
|
+
// setup({useConnectFunction: false});
|
|
313
|
+
// });
|
|
314
|
+
//
|
|
315
|
+
// // Cant test hooks yet, will test using state below
|
|
316
|
+
// // https://github.com/enzymejs/enzyme/issues/2011
|
|
317
|
+
// xit('Can publish event via context', () => {
|
|
318
|
+
// const childViewContext = connectableComponentWrapper.context();
|
|
319
|
+
// let publishEvent: PublishModelEventDelegate = React.useContext(PublishModelEventContext);
|
|
320
|
+
// publishEvent('test-event', 'the-event-value');
|
|
321
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
322
|
+
// expect(props.foo).toBe('the-event-value');
|
|
323
|
+
// });
|
|
324
|
+
//
|
|
325
|
+
// // Note this is a a best effort to test the context hook created via state
|
|
326
|
+
// it('publishEvent set', () => {
|
|
327
|
+
// const state = connectableComponentWrapper.state();
|
|
328
|
+
// state.publishEvent('test-event', 'the-event-value');
|
|
329
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
330
|
+
// expect(props.model.value).toBe('the-event-value');
|
|
331
|
+
// });
|
|
332
|
+
//
|
|
333
|
+
// it('publishEvent recreated on modle change', () => {
|
|
334
|
+
// const newProps = {
|
|
335
|
+
// ...connectableComponentWrapper.props(),
|
|
336
|
+
// modelId: 'model-id2',
|
|
337
|
+
// };
|
|
338
|
+
// connectableComponentWrapper.setProps(newProps);
|
|
339
|
+
// const state = connectableComponentWrapper.state();
|
|
340
|
+
// state.publishEvent('test-event', 'the-event-value');
|
|
341
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
342
|
+
// expect(props.model.value).toBe('the-event-value');
|
|
343
|
+
// expect(testModel2.value).toBe('the-event-value');
|
|
344
|
+
// });
|
|
345
|
+
// });
|
|
346
|
+
//
|
|
347
|
+
// describe('View rendering', () => {
|
|
348
|
+
// it('Renders the view found via @viewBinding', () => {
|
|
349
|
+
// setup({useConnectFunction: false});
|
|
350
|
+
// expect(connectableComponentWrapper.containsMatchingElement(<TestModelView1/>)).toBeTruthy();
|
|
351
|
+
// });
|
|
352
|
+
//
|
|
353
|
+
// it('Renders alternative view found via @viewBinding', () => {
|
|
354
|
+
// setup({useConnectFunction: false, useAlternativeViewContext: true});
|
|
355
|
+
// expect(connectableComponentWrapper.containsMatchingElement(<TestModelView3/>)).toBeTruthy();
|
|
356
|
+
// });
|
|
357
|
+
// });
|
|
358
|
+
//
|
|
359
|
+
// describe('Overriding model/state selector', () => {
|
|
360
|
+
// function testModelHasBeenReplacedWithMappedModel(expectedValue3) {
|
|
361
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
362
|
+
// let mappedModel = props.model as any as MappedModel;
|
|
363
|
+
// expect(mappedModel.value2).toBe('initial-value');
|
|
364
|
+
// expect(mappedModel.value3).toBe(expectedValue3);
|
|
365
|
+
// publishAnEventToModel1();
|
|
366
|
+
// props = getSpanWithPropsAsAttributes();
|
|
367
|
+
// mappedModel = props.model as any as MappedModel;
|
|
368
|
+
// expect(mappedModel.value2).toBe('the-event-value');
|
|
369
|
+
// }
|
|
370
|
+
//
|
|
371
|
+
// function testSwappedModelPassedToMapModelToPropsFunction(expectedValue3) {
|
|
372
|
+
// let props = getSpanWithPropsAsAttributes();
|
|
373
|
+
// expect(props.value2).toBe('initial-value');
|
|
374
|
+
// expect(props.value3).toBe(expectedValue3);
|
|
375
|
+
// publishAnEventToModel1();
|
|
376
|
+
// props = getSpanWithPropsAsAttributes();
|
|
377
|
+
// expect(props.value2).toBe('the-event-value');
|
|
378
|
+
// }
|
|
379
|
+
//
|
|
380
|
+
// describe('using @stateToRenderSelector', () => {
|
|
381
|
+
// it('Swaps the model for that returned by the function decorated with a @stateToRenderSelector decorator', () => {
|
|
382
|
+
// setup({useConnectFunction: false, useRenderModelSelectorDecorator: true});
|
|
383
|
+
// testModelHasBeenReplacedWithMappedModel('value-3');
|
|
384
|
+
// });
|
|
385
|
+
//
|
|
386
|
+
// it('passes the swapped model to mapModelToProps function', () => {
|
|
387
|
+
// setup({useConnectFunction: false, useRenderModelSelectorDecorator: true, useMapModelToProps: true});
|
|
388
|
+
// testSwappedModelPassedToMapModelToPropsFunction('value-3');
|
|
389
|
+
// });
|
|
390
|
+
// });
|
|
391
|
+
//
|
|
392
|
+
// describe('using state to render function', () => {
|
|
393
|
+
// it('Swaps the model for that returned by the function decorated with a @stateToRenderSelector decorator', () => {
|
|
394
|
+
// setup({useConnectFunction: false, useRenderModelSelectorFunction: true});
|
|
395
|
+
// testModelHasBeenReplacedWithMappedModel('value-3.1');
|
|
396
|
+
// });
|
|
397
|
+
//
|
|
398
|
+
// it('passes the swapped model to mapModelToProps function', () => {
|
|
399
|
+
// setup({useConnectFunction: false, useRenderModelSelectorFunction: true, useMapModelToProps: true});
|
|
400
|
+
// testSwappedModelPassedToMapModelToPropsFunction('value-3.1');
|
|
401
|
+
// });
|
|
402
|
+
// });
|
|
403
|
+
// });
|
|
404
|
+
// });
|
|
405
|
+
});
|