esp-js-react 8.0.0-next.0 → 8.0.2-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 +132 -90
- 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/{espModelContext.d.ts → espModelContextProvider.d.ts} +2 -2
- package/.dist/typings/{espRouterContext.d.ts → espRouterContextProvider.d.ts} +8 -8
- package/.dist/typings/index.d.ts +5 -5
- package/.dist/typings/polimer/getEspPolimerImmutableModel.d.ts +21 -0
- package/.dist/typings/useModelSelector.d.ts +20 -6
- package/package.json +4 -4
- package/src/connectableComponent.tsx +10 -9
- package/src/{espModelContext.tsx → espModelContextProvider.tsx} +5 -5
- package/src/{espRouterContext.tsx → espRouterContextProvider.tsx} +8 -8
- package/src/index.ts +19 -13
- package/src/polimer/getEspPolimerImmutableModel.ts +50 -0
- package/src/useModelSelector.ts +85 -20
- package/src/viewBinder.tsx +1 -1
- package/src/viewBinderConnectable.tsx +11 -10
- package/tests/connectableComponentLegacyTests.tsx +26 -26
- package/tests/connectableComponentTests.tsx +419 -0
- package/tests/indexTests.ts +25 -91
- package/tests/testApi/asserts.ts +75 -0
- package/tests/testApi/routerSpy.ts +24 -0
- package/tests/testApi/testApi.tsx +84 -0
- package/tests/testApi/testModel.ts +37 -0
- package/tests/testApi/useStoreReceivedProps.ts +11 -0
- package/tests/testApi/viewMetadata.tsx +46 -0
- package/tests/useModelSelectorTests.tsx +257 -0
- package/.dist/typings/polimer/getEspReactRenderModel.d.ts +0 -21
- package/src/polimer/getEspReactRenderModel.ts +0 -50
- package/tests/connectWithSelectorTests.tsx +0 -67
- /package/.dist/typings/polimer/{polimerExtentsions.d.ts → polimerModelBuilderExtentsions.d.ts} +0 -0
- /package/src/polimer/{polimerExtentsions.ts → polimerModelBuilderExtentsions.ts} +0 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {RenderResult} from '@testing-library/react';
|
|
2
|
+
// import for extra asserts
|
|
3
|
+
import '@testing-library/jest-dom';
|
|
4
|
+
import {RouterSpy} from './routerSpy';
|
|
5
|
+
import {TestPropStore} from './useStoreReceivedProps';
|
|
6
|
+
|
|
7
|
+
export type ViewAsserts = {
|
|
8
|
+
valueIs(expected: string): ViewAsserts;
|
|
9
|
+
modelIdIs(expected: string): ViewAsserts
|
|
10
|
+
propIs(propName: string, expected: string): ViewAsserts
|
|
11
|
+
htmlElementInnerTextIs(elementsTestId: string, expectedText: string): ViewAsserts
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const viewAsserts = (renderResult: RenderResult) => {
|
|
15
|
+
return {
|
|
16
|
+
modelIdIs(expected: string) {
|
|
17
|
+
let idElement = renderResult.getByTestId('modelIdDisplay');
|
|
18
|
+
expect(idElement).toHaveTextContent(expected);
|
|
19
|
+
return this;
|
|
20
|
+
},
|
|
21
|
+
valueIs(expected: string) {
|
|
22
|
+
let valueElement = renderResult.getByTestId('valueDisplay');
|
|
23
|
+
expect(valueElement).toHaveTextContent(expected);
|
|
24
|
+
return this;
|
|
25
|
+
},
|
|
26
|
+
propIs(propName: string, expected: string) {
|
|
27
|
+
let span = renderResult.getByTestId('span-with-props-as-attributes');
|
|
28
|
+
let attributeName = `data-${propName}`;
|
|
29
|
+
if (!span.hasAttribute(attributeName)) {
|
|
30
|
+
let error = `Span doesn't have attribute '${attributeName}'. Has ${JSON.stringify(span.attributes)}`;
|
|
31
|
+
fail(error);
|
|
32
|
+
}
|
|
33
|
+
expect(span.getAttribute(attributeName)).toBe(expected);
|
|
34
|
+
return this;
|
|
35
|
+
},
|
|
36
|
+
htmlElementInnerTextIs(elementsTestId: string, expectedText: string) {
|
|
37
|
+
let span = renderResult.getByTestId(elementsTestId);
|
|
38
|
+
expect(span).toHaveTextContent(expectedText);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
} as ViewAsserts;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type RouterAsserts = {
|
|
46
|
+
subscriberCountIs(modelId: string, expectedCount: number): RouterAsserts
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const routerAsserts = (routerSpy: RouterSpy) => {
|
|
50
|
+
return {
|
|
51
|
+
subscriberCountIs(modelId: string, expectedCount: number) {
|
|
52
|
+
expect(routerSpy.getSubscriberCount(modelId)).toEqual(expectedCount);
|
|
53
|
+
return this;
|
|
54
|
+
},
|
|
55
|
+
} as RouterAsserts;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type PropAsserts = {
|
|
59
|
+
receivedPropCountIs(expectedCount: number): PropAsserts;
|
|
60
|
+
propAtIndex(index: number, asserter: (props) => void): PropAsserts
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const propAsserts = (testPropStore: TestPropStore) => {
|
|
64
|
+
return {
|
|
65
|
+
receivedPropCountIs(expectedCount: number) {
|
|
66
|
+
expect(testPropStore.receivedProps.length).toEqual(expectedCount);
|
|
67
|
+
return this;
|
|
68
|
+
},
|
|
69
|
+
propAtIndex(index: number, asserter: (props) => void): PropAsserts {
|
|
70
|
+
asserter(testPropStore.receivedProps[index]);
|
|
71
|
+
return this;
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
} as PropAsserts;
|
|
75
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {Router, Observable} from 'esp-js';
|
|
2
|
+
|
|
3
|
+
export class RouterSpy extends Router {
|
|
4
|
+
private _modelSubscriptionCountByModelId = new Map<string, number>();
|
|
5
|
+
|
|
6
|
+
public getSubscriberCount(modelId: string) {
|
|
7
|
+
return this._modelSubscriptionCountByModelId.get(modelId);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public getModelObservable<TModel>(modelId: string): Observable<TModel> {
|
|
11
|
+
return Observable.create(o => {
|
|
12
|
+
let c1 = this._modelSubscriptionCountByModelId.get(modelId) || 0;
|
|
13
|
+
c1++;
|
|
14
|
+
this._modelSubscriptionCountByModelId.set(modelId, c1);
|
|
15
|
+
let subscription = super.getModelObservable(modelId).subscribe(o);
|
|
16
|
+
return () => {
|
|
17
|
+
let c2 = this._modelSubscriptionCountByModelId.get(modelId);
|
|
18
|
+
c2--;
|
|
19
|
+
this._modelSubscriptionCountByModelId.set(modelId, c2);
|
|
20
|
+
subscription.dispose();
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {TestModel} from './testModel';
|
|
2
|
+
import {RouterSpy} from './routerSpy';
|
|
3
|
+
import {render, RenderResult} from '@testing-library/react';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import {EspModelContextProvider, EspRouterContextProvider} from '../../src';
|
|
6
|
+
import {PropAsserts, propAsserts, RouterAsserts, routerAsserts, ViewAsserts, viewAsserts} from './asserts';
|
|
7
|
+
import {isValidElement} from 'react';
|
|
8
|
+
import {TestPropStore, TestPropStoreContext} from './useStoreReceivedProps';
|
|
9
|
+
|
|
10
|
+
export type TestApi = {
|
|
11
|
+
router: RouterSpy;
|
|
12
|
+
asserts: {
|
|
13
|
+
router: RouterAsserts,
|
|
14
|
+
view: ViewAsserts,
|
|
15
|
+
props: PropAsserts,
|
|
16
|
+
};
|
|
17
|
+
setupModel(modelId: string, alternativeModel?: object): any;
|
|
18
|
+
setupModelAndRender(modelId: string, Component: React.ComponentType): any;
|
|
19
|
+
doRender(Component: React.ComponentType, modelIdForContext: string): any
|
|
20
|
+
doRender(Component: React.JSX.Element, modelIdForContext: string): any
|
|
21
|
+
doReRender(Component: React.ComponentType, modelIdForContext: string): any;
|
|
22
|
+
doReRender(Component: React.JSX.Element, modelIdForContext: string): any;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const testApi = ()=> {
|
|
26
|
+
let router: RouterSpy = new RouterSpy();
|
|
27
|
+
let renderResult: RenderResult;
|
|
28
|
+
let propStore = new TestPropStore();
|
|
29
|
+
return {
|
|
30
|
+
router,
|
|
31
|
+
asserts: {
|
|
32
|
+
get router() {
|
|
33
|
+
return routerAsserts(router);
|
|
34
|
+
},
|
|
35
|
+
get view() {
|
|
36
|
+
return viewAsserts(renderResult);
|
|
37
|
+
},
|
|
38
|
+
get props() {
|
|
39
|
+
return propAsserts(propStore);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
setupModelAndRender(modelId: string, view: React.ComponentType) {
|
|
43
|
+
this.setupModel(modelId);
|
|
44
|
+
this.doRender(view, modelId);
|
|
45
|
+
return this;
|
|
46
|
+
},
|
|
47
|
+
setupModel(modelId: string, alternativeModel?: object) {
|
|
48
|
+
const testModel = alternativeModel || new TestModel(modelId);
|
|
49
|
+
router.addModel(modelId, testModel);
|
|
50
|
+
router.observeEventsOn(modelId, testModel);
|
|
51
|
+
return this;
|
|
52
|
+
},
|
|
53
|
+
doRender(ComponentOrElement: any, modelIdForContext: string) {
|
|
54
|
+
let e: any = isValidElement(ComponentOrElement)
|
|
55
|
+
? ComponentOrElement
|
|
56
|
+
: (<ComponentOrElement />);
|
|
57
|
+
renderResult = render((
|
|
58
|
+
<TestPropStoreContext.Provider value={propStore}>
|
|
59
|
+
<EspRouterContextProvider router={router}>
|
|
60
|
+
<EspModelContextProvider modelId={modelIdForContext}>
|
|
61
|
+
{e},
|
|
62
|
+
</EspModelContextProvider>
|
|
63
|
+
</EspRouterContextProvider>
|
|
64
|
+
</TestPropStoreContext.Provider>
|
|
65
|
+
));
|
|
66
|
+
return this;
|
|
67
|
+
},
|
|
68
|
+
doReRender(ComponentOrElement: any, modelIdForContext: string) {
|
|
69
|
+
let componentOrElement: any = isValidElement(ComponentOrElement)
|
|
70
|
+
? ComponentOrElement
|
|
71
|
+
: (<ComponentOrElement />);
|
|
72
|
+
renderResult.rerender((
|
|
73
|
+
<TestPropStoreContext.Provider value={propStore}>
|
|
74
|
+
<EspRouterContextProvider router={router}>
|
|
75
|
+
<EspModelContextProvider modelId={modelIdForContext}>
|
|
76
|
+
{componentOrElement},
|
|
77
|
+
</EspModelContextProvider>
|
|
78
|
+
</EspRouterContextProvider>
|
|
79
|
+
</TestPropStoreContext.Provider>
|
|
80
|
+
));
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
} as TestApi;
|
|
84
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {observeEvent} from 'esp-js';
|
|
2
|
+
import {GetEspPolimerImmutableModelConsts} from '../../src';
|
|
3
|
+
|
|
4
|
+
export type TestModelImmutableState = { value: string, modelId: string };
|
|
5
|
+
|
|
6
|
+
export class TestModel {
|
|
7
|
+
|
|
8
|
+
private _internalState: TestModelImmutableState;
|
|
9
|
+
|
|
10
|
+
constructor(modelId: string) {
|
|
11
|
+
this._internalState = {
|
|
12
|
+
value: 'initial-value',
|
|
13
|
+
modelId: modelId,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public get valueViaModelGetter() {
|
|
18
|
+
return this._internalState.value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public get modelIdViaModelGetter() {
|
|
22
|
+
return this._internalState.modelId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@observeEvent('test-event')
|
|
26
|
+
_onTestEvent(ev: string) {
|
|
27
|
+
// mimic some immutable logic that would be provided by a lib such as immer in a real app.
|
|
28
|
+
this._internalState = {
|
|
29
|
+
...this._internalState,
|
|
30
|
+
value: ev
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
[GetEspPolimerImmutableModelConsts.HandlerFunctionName]: () => TestModelImmutableState = () => {
|
|
35
|
+
return this._internalState;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {createContext, useContext} from 'react';
|
|
2
|
+
|
|
3
|
+
export const TestPropStoreContext = createContext<TestPropStore>(null);
|
|
4
|
+
export const useTestPropStore = () => useContext(TestPropStoreContext);
|
|
5
|
+
|
|
6
|
+
export class TestPropStore {
|
|
7
|
+
receivedProps: any[] = [];
|
|
8
|
+
pushProps(props: any) {
|
|
9
|
+
this.receivedProps.push(props);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import {utils} from 'esp-js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This is a bit of a hack to get the types and values of props passed into our HOC.
|
|
6
|
+
*
|
|
7
|
+
* React testing library doesn't have a lot of hooks for this, and generally you don't need to write HOC in modern React.
|
|
8
|
+
* However we need to support legacy code that uses the prop passing via HOC patterns.
|
|
9
|
+
* This function helps parse the props into attributes we can put on the DOM to asserts they are flowing through.
|
|
10
|
+
*/
|
|
11
|
+
const getPropsAsDataAttributesWithMetadata = (props: any) => {
|
|
12
|
+
const propsAsDataAttributes: {[key: string]: string} = Object.keys(props).reduce((result: any, key: string) => {
|
|
13
|
+
let prop = props[key];
|
|
14
|
+
let value: string;
|
|
15
|
+
if (utils.isString(prop)) {
|
|
16
|
+
value = prop;
|
|
17
|
+
} else if (utils.isFunction(props)) {
|
|
18
|
+
value = 'function';
|
|
19
|
+
} else if (utils.isObject(props)) {
|
|
20
|
+
value = 'object';
|
|
21
|
+
} else {
|
|
22
|
+
value = String(props);
|
|
23
|
+
}
|
|
24
|
+
// need to lower case data-* attributes else React throws a warning
|
|
25
|
+
result[`data-${key.toLowerCase()}`] = value;
|
|
26
|
+
return result;
|
|
27
|
+
}, {});
|
|
28
|
+
return propsAsDataAttributes;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type ViewMetadataProps = {
|
|
32
|
+
modelId: string,
|
|
33
|
+
modelValue: string
|
|
34
|
+
[key: string]: string; // ...rest props
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const ViewMetadata = (props: ViewMetadataProps) => {
|
|
38
|
+
const { modelId, modelValue, ...rest} = props;
|
|
39
|
+
return (
|
|
40
|
+
<div>
|
|
41
|
+
<span data-testid='modelIdDisplay'>{modelId}</span>
|
|
42
|
+
<span data-testid='valueDisplay'>{modelValue}</span>
|
|
43
|
+
<span data-testid='span-with-props-as-attributes' {...getPropsAsDataAttributesWithMetadata(rest)} />
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import 'jest';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import {act} from 'react';
|
|
4
|
+
import {modelSelectorOptions, useModelSelector} from '../src';
|
|
5
|
+
import {TestModel, TestModelImmutableState} from './testApi/testModel';
|
|
6
|
+
import {ViewMetadata} from './testApi/viewMetadata';
|
|
7
|
+
import {routerAsserts} from './testApi/asserts';
|
|
8
|
+
import {testApi, TestApi} from './testApi/testApi';
|
|
9
|
+
|
|
10
|
+
type SelectedObject = { id: string, value: string };
|
|
11
|
+
|
|
12
|
+
describe('useModelSelector', () => {
|
|
13
|
+
let api: TestApi;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
api = testApi();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('Default API case', () => {
|
|
20
|
+
const modelId = 'modelId';
|
|
21
|
+
|
|
22
|
+
const View = () => {
|
|
23
|
+
const valuesObj = useModelSelector<TestModelImmutableState, SelectedObject>(
|
|
24
|
+
model => {
|
|
25
|
+
return {id: model.modelId, value: model.value};
|
|
26
|
+
},
|
|
27
|
+
);
|
|
28
|
+
if (!valuesObj) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
return (
|
|
32
|
+
<ViewMetadata modelId={valuesObj.id} modelValue={valuesObj.value} />
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
api.setupModelAndRender(modelId, View);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('renders immutable model based on modelId found on context', () => {
|
|
41
|
+
api.asserts.view
|
|
42
|
+
.modelIdIs(modelId)
|
|
43
|
+
.valueIs('initial-value');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('updates are rendered', () => {
|
|
47
|
+
act(() => {
|
|
48
|
+
api.router.publishEvent(modelId, 'test-event', 'new-value');
|
|
49
|
+
});
|
|
50
|
+
api.asserts.view
|
|
51
|
+
.modelIdIs(modelId)
|
|
52
|
+
.valueIs('new-value');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('modelId can be passed via selector', () => {
|
|
57
|
+
const modelId = 'modelId';
|
|
58
|
+
|
|
59
|
+
const View = () => {
|
|
60
|
+
const values: string[] = useModelSelector<TestModelImmutableState, string[]>(
|
|
61
|
+
model => [model.modelId, model.value],
|
|
62
|
+
modelSelectorOptions().setModelId(modelId)
|
|
63
|
+
);
|
|
64
|
+
if (!values) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
return (
|
|
68
|
+
<ViewMetadata modelId={values[0]} modelValue={values[1]} />
|
|
69
|
+
);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
api.setupModel(modelId);
|
|
74
|
+
api.doRender(View, undefined);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('renders correct model data', () => {
|
|
78
|
+
api.asserts.view
|
|
79
|
+
.modelIdIs(modelId)
|
|
80
|
+
.valueIs('initial-value');
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('equalityFn is invoked to determine if changed should be propagated', () => {
|
|
85
|
+
const modelId = 'modelId';
|
|
86
|
+
|
|
87
|
+
const View = () => {
|
|
88
|
+
const values = useModelSelector<TestModelImmutableState, SelectedObject>(
|
|
89
|
+
model => ({id: model.modelId, value: model.value}),
|
|
90
|
+
modelSelectorOptions<SelectedObject>()
|
|
91
|
+
.setEqualityFn((a, b) => {
|
|
92
|
+
if (!a) {
|
|
93
|
+
return false; // first time
|
|
94
|
+
}
|
|
95
|
+
// delegate to model, set via the test, to see if we're 'equal'
|
|
96
|
+
// If this returns true, useModelSelector internally assume the last snapshot is the same thus doesn't propagate the last snapshot.
|
|
97
|
+
return b.value === 'assume-no-change';
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
if (!values) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
return (
|
|
104
|
+
<ViewMetadata modelId={values.id} modelValue={values.value} />
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
beforeEach(() => {
|
|
109
|
+
api.setupModelAndRender(modelId, View);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('renders initial model data', () => {
|
|
113
|
+
api.asserts.view
|
|
114
|
+
.modelIdIs(modelId)
|
|
115
|
+
.valueIs('initial-value');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('updates are ignored due to equalityFn', () => {
|
|
119
|
+
api.asserts.view
|
|
120
|
+
.modelIdIs(modelId)
|
|
121
|
+
.valueIs('initial-value');
|
|
122
|
+
act(() => {
|
|
123
|
+
api.router.publishEvent(modelId, 'test-event', 'assume-no-change');
|
|
124
|
+
});
|
|
125
|
+
api.asserts.view
|
|
126
|
+
.modelIdIs(modelId)
|
|
127
|
+
.valueIs('initial-value'); // event data WAS NOT propagated
|
|
128
|
+
act(() => {
|
|
129
|
+
api.router.publishEvent(modelId, 'test-event', 'do-change');
|
|
130
|
+
});
|
|
131
|
+
api.asserts.view
|
|
132
|
+
.modelIdIs(modelId)
|
|
133
|
+
.valueIs('do-change'); // event data WAS propagated
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe('selector internal caching is based on modelId', () => {
|
|
138
|
+
const modelId1 = 'modelId1';
|
|
139
|
+
const modelId2 = 'modelId2';
|
|
140
|
+
|
|
141
|
+
const View = (props: {modelId: string}) => {
|
|
142
|
+
const values = useModelSelector<TestModelImmutableState, SelectedObject>(
|
|
143
|
+
model => ({id: model.modelId, value: model.value}),
|
|
144
|
+
modelSelectorOptions().setModelId(props.modelId)
|
|
145
|
+
);
|
|
146
|
+
if (!values) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
return (
|
|
150
|
+
<ViewMetadata modelId={values.id} modelValue={values.value} />
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
beforeEach(() => {
|
|
155
|
+
api.setupModel(modelId1);
|
|
156
|
+
api.doRender(View, modelId1);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('selector subscribed to Router', () => {
|
|
160
|
+
routerAsserts(api.router).subscriberCountIs(modelId1, 1);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('unsubscribes when modelId changes', () => {
|
|
164
|
+
api.doReRender(View, undefined);
|
|
165
|
+
api.asserts.router.subscriberCountIs(modelId1, 0);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('resubscribes to new model when modelId changes', () => {
|
|
169
|
+
api.asserts.view
|
|
170
|
+
.modelIdIs(modelId1)
|
|
171
|
+
.valueIs('initial-value');
|
|
172
|
+
api.doReRender(View, undefined);
|
|
173
|
+
api.asserts.view
|
|
174
|
+
.modelIdIs('')
|
|
175
|
+
.valueIs('');
|
|
176
|
+
api.setupModel(modelId2);
|
|
177
|
+
api.doReRender(View, modelId2);
|
|
178
|
+
api.asserts.view
|
|
179
|
+
.modelIdIs(modelId2)
|
|
180
|
+
.valueIs('initial-value');
|
|
181
|
+
act(() => {
|
|
182
|
+
api.router.publishEvent(modelId2, 'test-event', 'updated');
|
|
183
|
+
});
|
|
184
|
+
api.asserts.view
|
|
185
|
+
.modelIdIs(modelId2)
|
|
186
|
+
.valueIs('updated');
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe('non immutable model cases #1 - selector receives then maps model subset', () => {
|
|
191
|
+
const modelId = 'modelId';
|
|
192
|
+
|
|
193
|
+
const View = () => {
|
|
194
|
+
const values: string[] = useModelSelector<TestModel, string[]>(
|
|
195
|
+
model => {
|
|
196
|
+
// here we're expecting to receive a TestModel, not testModelInstance[GetEspPolimerImmutableModelConsts.HandlerFunctionName]()
|
|
197
|
+
return [model.modelIdViaModelGetter, model.valueViaModelGetter];
|
|
198
|
+
},
|
|
199
|
+
modelSelectorOptions().setTryPreSelectPolimerImmutableModel(false)
|
|
200
|
+
);
|
|
201
|
+
if (!values) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
return (
|
|
205
|
+
<ViewMetadata modelId={values[0]} modelValue={values[1]} />
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
beforeEach(() => {
|
|
210
|
+
api.setupModelAndRender(modelId, View);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('selector passes router root model to selector', () => {
|
|
214
|
+
// Not much to test here, it's really in the View logic for this case.
|
|
215
|
+
// If the below passes, it means the selector was given the root ESP model.
|
|
216
|
+
api.asserts.view
|
|
217
|
+
.modelIdIs(modelId)
|
|
218
|
+
.valueIs('initial-value');
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe('non immutable model cases - selector maps root model', () => {
|
|
223
|
+
const modelId = 'modelId';
|
|
224
|
+
|
|
225
|
+
const View = () => {
|
|
226
|
+
const model: TestModel = useModelSelector<TestModel, TestModel>(
|
|
227
|
+
m => m,
|
|
228
|
+
modelSelectorOptions().setTryPreSelectPolimerImmutableModel(false)
|
|
229
|
+
);
|
|
230
|
+
if (!model) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
return (
|
|
234
|
+
<ViewMetadata modelId={model.modelIdViaModelGetter} modelValue={model.valueViaModelGetter} />
|
|
235
|
+
);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
beforeEach(() => {
|
|
239
|
+
api.setupModelAndRender(modelId, View);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('view renders correct data', () => {
|
|
243
|
+
api.asserts.view
|
|
244
|
+
.modelIdIs(modelId)
|
|
245
|
+
.valueIs('initial-value');
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('Updates propagated', () => {
|
|
249
|
+
act(() => {
|
|
250
|
+
api.router.publishEvent(modelId, 'test-event', 'updated');
|
|
251
|
+
});
|
|
252
|
+
api.asserts.view
|
|
253
|
+
.modelIdIs(modelId)
|
|
254
|
+
.valueIs('updated');
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export interface GetEspReactRenderModelMetadata {
|
|
2
|
-
functionName: string;
|
|
3
|
-
}
|
|
4
|
-
export declare namespace GetEspReactRenderModelConsts {
|
|
5
|
-
const CustomDataKey = "GetEspReactRenderModelCustomDataKey";
|
|
6
|
-
/**
|
|
7
|
-
* If there is no @getEspReactRenderModel decorator and a function by this name exists on a model, then it will be invoked to identify the sub model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
8
|
-
*/
|
|
9
|
-
const HandlerFunctionName = "getEspReactRenderModel";
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* A decorator which can be used to identify the render model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
13
|
-
*/
|
|
14
|
-
export declare function getEspReactRenderModel(): (target: any, name: any, descriptor: any) => any;
|
|
15
|
-
/**
|
|
16
|
-
* Returns the model which will be used to pluck state/props for the current view.
|
|
17
|
-
*
|
|
18
|
-
* This is basically a hook (not a React 'hook') to call a function on `model` and get a new sub graph to use as state.
|
|
19
|
-
*
|
|
20
|
-
*/
|
|
21
|
-
export declare const tryGetRenderModel: <TRenderModel>(model: any) => TRenderModel;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import {EspMetadata, EspDecoratorUtil, utils} from 'esp-js';
|
|
2
|
-
|
|
3
|
-
export interface GetEspReactRenderModelMetadata {
|
|
4
|
-
functionName: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export namespace GetEspReactRenderModelConsts {
|
|
8
|
-
export const CustomDataKey = 'GetEspReactRenderModelCustomDataKey';
|
|
9
|
-
/**
|
|
10
|
-
* If there is no @getEspReactRenderModel decorator and a function by this name exists on a model, then it will be invoked to identify the sub model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
11
|
-
*/
|
|
12
|
-
export const HandlerFunctionName = 'getEspReactRenderModel';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A decorator which can be used to identify the render model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
17
|
-
*/
|
|
18
|
-
export function getEspReactRenderModel() {
|
|
19
|
-
return function (target, name, descriptor) {
|
|
20
|
-
let metadata: EspMetadata = EspDecoratorUtil.getOrCreateMetaData(target.constructor);
|
|
21
|
-
metadata.addCustomData(
|
|
22
|
-
GetEspReactRenderModelConsts.CustomDataKey,
|
|
23
|
-
<GetEspReactRenderModelMetadata>{ functionName: name }
|
|
24
|
-
);
|
|
25
|
-
return descriptor;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Returns the model which will be used to pluck state/props for the current view.
|
|
31
|
-
*
|
|
32
|
-
* This is basically a hook (not a React 'hook') to call a function on `model` and get a new sub graph to use as state.
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
export const tryGetRenderModel = <TRenderModel>(model: any): TRenderModel => {
|
|
36
|
-
// does the given model have a decorated function we can invoke to get a different model to render?
|
|
37
|
-
if (EspDecoratorUtil.hasMetadata(model)) {
|
|
38
|
-
let metadata: GetEspReactRenderModelMetadata = EspDecoratorUtil.getCustomData(model, GetEspReactRenderModelConsts.CustomDataKey);
|
|
39
|
-
if (metadata) {
|
|
40
|
-
return model[metadata.functionName]();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
// else see if there is a function with name GetEspReactRenderModelConsts.HandlerFunctionName we can invoke to get a different model to render?
|
|
44
|
-
let renderModelGetter = model[GetEspReactRenderModelConsts.HandlerFunctionName];
|
|
45
|
-
if (renderModelGetter && utils.isFunction(renderModelGetter)) {
|
|
46
|
-
return renderModelGetter.call(model);
|
|
47
|
-
}
|
|
48
|
-
// else return the initial model passed in
|
|
49
|
-
return model;
|
|
50
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import 'jest';
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
import {act} from 'react';
|
|
4
|
-
import {render, RenderResult} from '@testing-library/react';
|
|
5
|
-
import {useModelSelector, EspModelContext, EspRouterContext} from '../src';
|
|
6
|
-
import {observeEvent, Router} from 'esp-js';
|
|
7
|
-
// import for extra asserts
|
|
8
|
-
import '@testing-library/jest-dom';
|
|
9
|
-
|
|
10
|
-
class TestModel {
|
|
11
|
-
public value: string;
|
|
12
|
-
|
|
13
|
-
constructor() {
|
|
14
|
-
this.value = 'initial-value';
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
@observeEvent('test-event')
|
|
18
|
-
_onTestEvent(ev: string) {
|
|
19
|
-
this.value = ev;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const MyComponent = () => {
|
|
24
|
-
const modelValue = useModelSelector<TestModel, string>(
|
|
25
|
-
model => model.value
|
|
26
|
-
);
|
|
27
|
-
return (
|
|
28
|
-
<div>
|
|
29
|
-
<span data-testid='valueDisplay'>{modelValue}</span>
|
|
30
|
-
</div>
|
|
31
|
-
);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
describe('connectWithSelector', () => {
|
|
35
|
-
let router: Router,
|
|
36
|
-
testModel: TestModel,
|
|
37
|
-
renderResult: RenderResult,
|
|
38
|
-
testModelId = 'this-model-id';
|
|
39
|
-
|
|
40
|
-
beforeEach(() => {
|
|
41
|
-
router = new Router();
|
|
42
|
-
testModel = new TestModel();
|
|
43
|
-
router.addModel(testModelId, testModel);
|
|
44
|
-
router.observeEventsOn(testModelId, testModel);
|
|
45
|
-
renderResult = render((
|
|
46
|
-
<EspRouterContext router={router}>
|
|
47
|
-
<EspModelContext modelId={testModelId}>
|
|
48
|
-
<MyComponent />,
|
|
49
|
-
</EspModelContext>
|
|
50
|
-
</EspRouterContext>
|
|
51
|
-
));
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('component is rendered', () => {
|
|
55
|
-
let testElement = renderResult.getByTestId('valueDisplay');
|
|
56
|
-
expect(testElement).toHaveTextContent('initial-value');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('model updates are rendered', () => {
|
|
60
|
-
let testElement = renderResult.getByTestId('valueDisplay');
|
|
61
|
-
expect(testElement).toHaveTextContent('initial-value');
|
|
62
|
-
act(() => {
|
|
63
|
-
router.publishEvent(testModelId, 'test-event', 'new-value');
|
|
64
|
-
});
|
|
65
|
-
expect(testElement).toHaveTextContent('new-value');
|
|
66
|
-
});
|
|
67
|
-
});
|
/package/.dist/typings/polimer/{polimerExtentsions.d.ts → polimerModelBuilderExtentsions.d.ts}
RENAMED
|
File without changes
|
|
File without changes
|