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
package/src/shallowEqual.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
// taken from fbjs:
|
|
2
|
-
// https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/shallowEqual.js
|
|
3
|
-
// Licence (MIT) https://github.com/facebook/fbjs/blob/master/packages/fbjs/LICENSE
|
|
4
|
-
/**
|
|
5
|
-
* Copyright (c) 2013-present, Facebook, Inc.
|
|
6
|
-
*
|
|
7
|
-
* This source code is licensed under the MIT license found in the
|
|
8
|
-
* LICENSE file in the root directory of this source tree.
|
|
9
|
-
*
|
|
10
|
-
* @providesModule shallowEqual
|
|
11
|
-
* @typechecks
|
|
12
|
-
* @flow
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/*eslint-disable no-self-compare */
|
|
16
|
-
|
|
17
|
-
'use strict';
|
|
18
|
-
|
|
19
|
-
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* inlined Object.is polyfill to avoid requiring consumers ship their own
|
|
23
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
|
24
|
-
*/
|
|
25
|
-
export function is(x: any, y: any): boolean {
|
|
26
|
-
// SameValue algorithm
|
|
27
|
-
if (x === y) { // Steps 1-5, 7-10
|
|
28
|
-
// Steps 6.b-6.e: +0 != -0
|
|
29
|
-
// Added the nonzero y check to make Flow happy, but it is redundant
|
|
30
|
-
return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
|
31
|
-
} else {
|
|
32
|
-
// Step 6.a: NaN == NaN
|
|
33
|
-
return x !== x && y !== y;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Performs equality by iterating through keys on an object and returning false
|
|
39
|
-
* when any key has values which are not strictly equal between the arguments.
|
|
40
|
-
* Returns true when the values of all keys are strictly equal.
|
|
41
|
-
*/
|
|
42
|
-
export function shallowEqual(objA, objB) {
|
|
43
|
-
if (is(objA, objB)) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (typeof objA !== 'object' || objA === null ||
|
|
48
|
-
typeof objB !== 'object' || objB === null) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const keysA = Object.keys(objA);
|
|
53
|
-
const keysB = Object.keys(objB);
|
|
54
|
-
|
|
55
|
-
if (keysA.length !== keysB.length) {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Test for A's keys different from B.
|
|
60
|
-
for (let i = 0; i < keysA.length; i++) {
|
|
61
|
-
if (
|
|
62
|
-
!hasOwnProperty.call(objB, keysA[i]) ||
|
|
63
|
-
!is(objA[keysA[i]], objB[keysA[i]])
|
|
64
|
-
) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return true;
|
|
70
|
-
}
|
package/src/shouldUpdateMixin.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// notice_start
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2018 Keith Woods
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
// notice_end
|
|
18
|
-
|
|
19
|
-
import * as React from 'react';
|
|
20
|
-
import {shallowEqual} from './shallowEqual';
|
|
21
|
-
|
|
22
|
-
// WORKS (compiles at least):
|
|
23
|
-
//
|
|
24
|
-
// export function shouldUpdateMixin<TProps, TState, TPropBindings>(itemsThatEffectUpdateSelector: (nextProps: TProps) => TPropBindings) {
|
|
25
|
-
// return function <T extends {new(...args:any[]):{}}>(constructor:T) {
|
|
26
|
-
// return class extends constructor {
|
|
27
|
-
// _propBindings : {};
|
|
28
|
-
// shouldComponentUpdate(nextProps, nextState) {
|
|
29
|
-
// let newBindings = itemsThatEffectUpdateSelector(nextProps);
|
|
30
|
-
// let bindingsAreEqual = shallowEqual(this._propBindings, newBindings);
|
|
31
|
-
// this._propBindings = newBindings;
|
|
32
|
-
// return !bindingsAreEqual;
|
|
33
|
-
// }
|
|
34
|
-
// render() {
|
|
35
|
-
// return React.createElement(<any>constructor, (<any>this).props);
|
|
36
|
-
// }
|
|
37
|
-
// };
|
|
38
|
-
// };
|
|
39
|
-
// }
|
|
40
|
-
|
|
41
|
-
export function shouldUpdateMixin<TProps, TState, TPropBindings>(itemsThatEffectUpdateSelector: (nextProps: TProps) => TPropBindings) {
|
|
42
|
-
return function <TConstructor extends { new(...args: any[]): {} }>(Constructor: TConstructor) {
|
|
43
|
-
return class extends Constructor {
|
|
44
|
-
_propBindings: {};
|
|
45
|
-
shouldComponentUpdate(nextProps, nextState) {
|
|
46
|
-
let newBindings = itemsThatEffectUpdateSelector(nextProps);
|
|
47
|
-
let bindingsAreEqual = shallowEqual(this._propBindings, newBindings);
|
|
48
|
-
this._propBindings = newBindings;
|
|
49
|
-
return !bindingsAreEqual;
|
|
50
|
-
}
|
|
51
|
-
render() {
|
|
52
|
-
return React.createElement(<any>Constructor, (<any>this).props);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// export function shouldUpdateMixin<TProps, TState, TPropBindings>(itemsThatEffectUpdateSelector: (nextProps: TProps) => TPropBindings) {
|
|
59
|
-
// return function(Comp: React.ComponentClass<TProps, TState>) {
|
|
60
|
-
// return class extends React.Component<TProps, TState> {
|
|
61
|
-
// _propBindings : {};
|
|
62
|
-
// shouldComponentUpdate(nextProps, nextState) {
|
|
63
|
-
// let newBindings = itemsThatEffectUpdateSelector(nextProps);
|
|
64
|
-
// let bindingsAreEqual = shallowEqual(this._propBindings, newBindings);
|
|
65
|
-
// this._propBindings = newBindings;
|
|
66
|
-
// return !bindingsAreEqual;
|
|
67
|
-
// }
|
|
68
|
-
// render() {
|
|
69
|
-
// return React.createElement(Comp, this.props);
|
|
70
|
-
// }
|
|
71
|
-
// };
|
|
72
|
-
// };
|
|
73
|
-
// }
|
package/src/smartComponent.tsx
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
// notice_start
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2015 Dev Shop Limited
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
// notice_end
|
|
18
|
-
|
|
19
|
-
import * as React from 'react';
|
|
20
|
-
import * as PropTypes from 'prop-types';
|
|
21
|
-
import { createViewForModel } from './viewBindingDecorator';
|
|
22
|
-
import { Router } from 'esp-js';
|
|
23
|
-
|
|
24
|
-
export interface SmartComponentProps {
|
|
25
|
-
modelId: string;
|
|
26
|
-
view?: any;
|
|
27
|
-
viewContext?: string;
|
|
28
|
-
modelSelector?: (model: any) => any;
|
|
29
|
-
[key: string]: any; // other props which will be passed through to the SmartComponent's view
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface SmartComponentState {
|
|
33
|
-
model: any;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class SmartComponent extends React.Component<SmartComponentProps, SmartComponentState> {
|
|
37
|
-
private _currentObservingModelId = false;
|
|
38
|
-
private _observationSubscription = null;
|
|
39
|
-
|
|
40
|
-
static contextTypes = {
|
|
41
|
-
router: PropTypes.instanceOf(Router).isRequired
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
componentWillReceiveProps(nextProps: SmartComponentProps) {
|
|
45
|
-
this._tryObserveModel(nextProps.modelId);
|
|
46
|
-
}
|
|
47
|
-
componentWillMount() {
|
|
48
|
-
this._tryObserveModel(this.props.modelId);
|
|
49
|
-
}
|
|
50
|
-
componentWillUnmount() {
|
|
51
|
-
this._tryDisposeObservationSubscription();
|
|
52
|
-
}
|
|
53
|
-
_tryObserveModel(modelId) {
|
|
54
|
-
if(!modelId) {
|
|
55
|
-
this._tryDisposeObservationSubscription();
|
|
56
|
-
this.setState({model: null});
|
|
57
|
-
} else if (modelId !== this._currentObservingModelId) {
|
|
58
|
-
this._tryDisposeObservationSubscription();
|
|
59
|
-
this._currentObservingModelId = modelId;
|
|
60
|
-
this._observationSubscription = this.context.router
|
|
61
|
-
.getModelObservable(modelId)
|
|
62
|
-
.map(model => {
|
|
63
|
-
return this.props.modelSelector
|
|
64
|
-
? this.props.modelSelector(model)
|
|
65
|
-
: model;
|
|
66
|
-
})
|
|
67
|
-
.subscribe(model => {
|
|
68
|
-
this.setState({model: model});
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
_tryDisposeObservationSubscription() {
|
|
73
|
-
this._currentObservingModelId = null;
|
|
74
|
-
if(this._observationSubscription) {
|
|
75
|
-
this._observationSubscription.dispose();
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
render() {
|
|
79
|
-
if(this.state.model) {
|
|
80
|
-
return createViewForModel(this.state.model, this._getChildProps(), this.props.viewContext, this.props.view);
|
|
81
|
-
}
|
|
82
|
-
return null; // this (in react 15) will render a 'comment node' rather than any actual html
|
|
83
|
-
}
|
|
84
|
-
_getChildProps() {
|
|
85
|
-
// 'consume' the props we own, pass the rest down
|
|
86
|
-
let {modelId, viewContext, view, modelSelector, ...other} = this.props;
|
|
87
|
-
let newProps = {
|
|
88
|
-
model: this.state.model,
|
|
89
|
-
router: this.context.router
|
|
90
|
-
};
|
|
91
|
-
return Object.assign({}, newProps, other);
|
|
92
|
-
}
|
|
93
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
// notice_start
|
|
2
|
-
/*
|
|
3
|
-
* Copyright 2015 Dev Shop Limited
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
// notice_end
|
|
19
|
-
|
|
20
|
-
export function removeAll(arr, item) {
|
|
21
|
-
for (let i = arr.length; i--;) {
|
|
22
|
-
if (arr[i] === item) {
|
|
23
|
-
arr.splice(i, 1);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function isString(value) {
|
|
29
|
-
return typeof value === 'string' || value instanceof String;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function format(formatString) {
|
|
33
|
-
//Guard.isString(format, "First argument to a log function should be a string, but got [" + format + "]");
|
|
34
|
-
let args = Array.prototype.slice.call(arguments, 1);
|
|
35
|
-
let message = formatString.replace(/{(\d+)}/g, function (match, number) {
|
|
36
|
-
return typeof args[number] !== 'undefined'
|
|
37
|
-
? args[number]
|
|
38
|
-
: match;
|
|
39
|
-
});
|
|
40
|
-
return message;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function startsWith(string, searchString, position) {
|
|
44
|
-
position = position || 0;
|
|
45
|
-
return string.indexOf(searchString, position) === position;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function getPropertyNames(object) {
|
|
49
|
-
let props = [];
|
|
50
|
-
Object.getOwnPropertyNames(object).forEach(function (key) {
|
|
51
|
-
props.push(key);
|
|
52
|
-
});
|
|
53
|
-
let proto = Object.getPrototypeOf(object);
|
|
54
|
-
if (proto !== null && typeof proto === 'object') {
|
|
55
|
-
let childProps = getPropertyNames(proto);
|
|
56
|
-
for (let i = 0; i < childProps.length; i++) {
|
|
57
|
-
props.push(childProps[i]);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return props;
|
|
61
|
-
}
|
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
import 'jest';
|
|
2
|
-
import * as React from 'react';
|
|
3
|
-
import * as PropTypes from 'prop-types';
|
|
4
|
-
import {configure, mount, ReactWrapper} from 'enzyme';
|
|
5
|
-
import ReactSixteenAdapter = require('enzyme-adapter-react-16');
|
|
6
|
-
import {ConnectableComponent, ConnectableComponentProps, viewBinding, connect, getEspReactRenderModel} from '../src';
|
|
7
|
-
import {Router} from 'esp-js';
|
|
8
|
-
import {observeEvent} from 'esp-js/src/decorators/observeEvent';
|
|
9
|
-
import {ConnectableComponentChildProps, PublishEvent} from '../src/connectableComponent';
|
|
10
|
-
|
|
11
|
-
configure({adapter: new ReactSixteenAdapter()});
|
|
12
|
-
|
|
13
|
-
class TestModelView1 extends React.Component {
|
|
14
|
-
constructor(props) {
|
|
15
|
-
super(props);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
render() {
|
|
19
|
-
return <span>View1</span>;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class TestModelView2 extends React.Component {
|
|
24
|
-
constructor(props) {
|
|
25
|
-
super(props);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
render() {
|
|
29
|
-
return <span>View2</span>;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
class TestModelView3 extends React.Component {
|
|
34
|
-
constructor(props) {
|
|
35
|
-
super(props);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
render() {
|
|
39
|
-
return <span>View3</span>;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
@viewBinding(TestModelView1)
|
|
44
|
-
@viewBinding(TestModelView3, 'alternative-view-context')
|
|
45
|
-
class TestModel {
|
|
46
|
-
public value: string;
|
|
47
|
-
|
|
48
|
-
constructor() {
|
|
49
|
-
this.value = 'initial-value';
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
@observeEvent('test-event')
|
|
53
|
-
_onTestEvent(ev) {
|
|
54
|
-
this.value = ev;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class TestModel2 extends TestModel {
|
|
59
|
-
@getEspReactRenderModel()
|
|
60
|
-
_getOtherState() {
|
|
61
|
-
return {
|
|
62
|
-
value: this.value,
|
|
63
|
-
value2: this.value,
|
|
64
|
-
value3: 'value-3'
|
|
65
|
-
} as MappedModel;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class TestModel3 extends TestModel {
|
|
70
|
-
getEspReactRenderModel() {
|
|
71
|
-
return {
|
|
72
|
-
value: this.value,
|
|
73
|
-
value2: this.value,
|
|
74
|
-
value3: 'value-3.1'
|
|
75
|
-
} as MappedModel;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
interface MappedModel {
|
|
80
|
-
value: string;
|
|
81
|
-
value2: string;
|
|
82
|
-
value3: string;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
interface TestOptions {
|
|
86
|
-
useConnectFunction?: boolean;
|
|
87
|
-
useMapModelToProps?: boolean;
|
|
88
|
-
useCreatePublishEventProps?: boolean;
|
|
89
|
-
useAlternativeViewContext?: boolean;
|
|
90
|
-
passOtherProps?: boolean;
|
|
91
|
-
useRenderModelSelectorDecorator?: boolean;
|
|
92
|
-
useRenderModelSelectorFunction?: boolean;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
describe('ConnectableComponent', () => {
|
|
96
|
-
let router,
|
|
97
|
-
testModel: TestModel,
|
|
98
|
-
testModel2: TestModel,
|
|
99
|
-
connectableComponentWrapper: ReactWrapper<ConnectableComponentProps<TestModel>, {}, ConnectableComponent<TestModel>>,
|
|
100
|
-
connectableComponentProps: ConnectableComponentProps<TestModel>,
|
|
101
|
-
mapModelToProps,
|
|
102
|
-
createPublishEventProps,
|
|
103
|
-
publishEventProps,
|
|
104
|
-
userAlternativeViewContext,
|
|
105
|
-
otherProps = { other1: 'other-value' };
|
|
106
|
-
|
|
107
|
-
function createModel(options: TestOptions) {
|
|
108
|
-
if (options.useRenderModelSelectorDecorator) {
|
|
109
|
-
return new TestModel2();
|
|
110
|
-
}
|
|
111
|
-
if (options.useRenderModelSelectorFunction) {
|
|
112
|
-
return new TestModel3();
|
|
113
|
-
}
|
|
114
|
-
return new TestModel();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function setup(options: TestOptions) {
|
|
118
|
-
router = new Router();
|
|
119
|
-
testModel = createModel(options);
|
|
120
|
-
router.addModel('model-id', testModel);
|
|
121
|
-
router.observeEventsOn('model-id', testModel);
|
|
122
|
-
testModel2 = new TestModel();
|
|
123
|
-
router.addModel('model-id2', testModel2);
|
|
124
|
-
router.observeEventsOn('model-id2', testModel2);
|
|
125
|
-
connectableComponentProps = {
|
|
126
|
-
modelId: 'model-id'
|
|
127
|
-
};
|
|
128
|
-
if (options.useMapModelToProps) {
|
|
129
|
-
mapModelToProps = (model: TestModel | MappedModel, publishEventProps1: any) => {
|
|
130
|
-
return {
|
|
131
|
-
foo: model.value,
|
|
132
|
-
publishEventProps: publishEventProps1,
|
|
133
|
-
...model
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
if (options.useCreatePublishEventProps) {
|
|
138
|
-
publishEventProps = {
|
|
139
|
-
publishEvent1: () => {
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
createPublishEventProps = (publishEvent: PublishEvent) => publishEventProps;
|
|
143
|
-
}
|
|
144
|
-
if (options.useAlternativeViewContext) {
|
|
145
|
-
userAlternativeViewContext = 'alternative-view-context';
|
|
146
|
-
}
|
|
147
|
-
if (options.useConnectFunction) {
|
|
148
|
-
const HotView2 = connect(
|
|
149
|
-
mapModelToProps,
|
|
150
|
-
createPublishEventProps
|
|
151
|
-
)(TestModelView2);
|
|
152
|
-
connectableComponentWrapper = mount(
|
|
153
|
-
<HotView2 {...otherProps}/>,
|
|
154
|
-
{context: {router}, childContextTypes: {router: PropTypes.instanceOf(Router)}}
|
|
155
|
-
);
|
|
156
|
-
connectableComponentWrapper.setProps(connectableComponentProps);
|
|
157
|
-
} else {
|
|
158
|
-
connectableComponentWrapper = mount(
|
|
159
|
-
<ConnectableComponent
|
|
160
|
-
{...connectableComponentProps}
|
|
161
|
-
mapModelToProps={mapModelToProps}
|
|
162
|
-
viewContext={userAlternativeViewContext}
|
|
163
|
-
{...otherProps}
|
|
164
|
-
/>,
|
|
165
|
-
{context: {router}, childContextTypes: {router: PropTypes.instanceOf(Router)}}
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function publishAnEventToModel1() {
|
|
171
|
-
router.publishEvent('model-id', 'test-event', 'the-event-value');
|
|
172
|
-
expect(testModel.value).toEqual('the-event-value');
|
|
173
|
-
connectableComponentWrapper.update();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function publishAnEventToModel2() {
|
|
177
|
-
router.publishEvent('model-id2', 'test-event','the-event-value2');
|
|
178
|
-
expect(testModel2.value).toEqual('the-event-value2');
|
|
179
|
-
connectableComponentWrapper.update();
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
afterEach(() => {
|
|
183
|
-
// console.log(connectableComponentWrapper.debug());
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
describe('ConnectableComponent via connect()', () => {
|
|
187
|
-
beforeEach(() => {
|
|
188
|
-
setup({useConnectFunction: true, useMapModelToProps: true, useCreatePublishEventProps: true});
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const getChidViewProps = () => {
|
|
192
|
-
return connectableComponentWrapper.childAt(0).childAt(0).props() as ConnectableComponentChildProps<TestModel>;
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
describe('Child view props', () => {
|
|
196
|
-
it('passes mapped props', () => {
|
|
197
|
-
let props = getChidViewProps();
|
|
198
|
-
expect(props.foo).toEqual('initial-value');
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('passes publishEventProps to props mapper', () => {
|
|
202
|
-
let props = getChidViewProps();
|
|
203
|
-
expect(props.publishEventProps).toBe(publishEventProps);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('passes model', () => {
|
|
207
|
-
let props = getChidViewProps();
|
|
208
|
-
expect(props.model).toBe(testModel);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('passes publishEventProps', () => {
|
|
212
|
-
let props = getChidViewProps();
|
|
213
|
-
expect(props.publishEvent1).toBeDefined();
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('passes other props', () => {
|
|
217
|
-
let props = getChidViewProps();
|
|
218
|
-
expect(props.other1).toEqual('other-value');
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
describe('View rendering', () => {
|
|
223
|
-
it('Renders the view provided to connect', () => {
|
|
224
|
-
expect(connectableComponentWrapper.containsMatchingElement(<TestModelView2/>)).toBeTruthy();
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
describe('ConnectableComponent directly', () => {
|
|
230
|
-
|
|
231
|
-
const getChidViewProps = () => {
|
|
232
|
-
return connectableComponentWrapper.childAt(0).props() as ConnectableComponentChildProps<TestModel>;
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
describe('Child view props', () => {
|
|
236
|
-
beforeEach(() => {
|
|
237
|
-
setup({useConnectFunction: false});
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
it('passes modelId', () => {
|
|
241
|
-
let props = getChidViewProps();
|
|
242
|
-
expect(props.modelId).toEqual('model-id');
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('passes router', () => {
|
|
246
|
-
let props = getChidViewProps();
|
|
247
|
-
expect(props.router).toBe(router);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
it('passes model', () => {
|
|
251
|
-
let props = getChidViewProps();
|
|
252
|
-
expect(props.model).toBe(testModel);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
it('passes other props', () => {
|
|
256
|
-
let props = getChidViewProps();
|
|
257
|
-
expect(props.other1).toEqual('other-value');
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
describe('Re-renders', () => {
|
|
262
|
-
beforeEach(() => {
|
|
263
|
-
setup({useConnectFunction: false});
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('Re-renders when model updates', () => {
|
|
267
|
-
publishAnEventToModel1();
|
|
268
|
-
let props = getChidViewProps();
|
|
269
|
-
expect(props.model.value).toBe('the-event-value');
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
describe('Model Subscription', () => {
|
|
274
|
-
beforeEach(() => {
|
|
275
|
-
setup({useConnectFunction: false});
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
it('Re-subscribes to new model when modelId changes', () => {
|
|
279
|
-
const newProps = {
|
|
280
|
-
...connectableComponentWrapper.props(),
|
|
281
|
-
modelId: 'model-id2',
|
|
282
|
-
};
|
|
283
|
-
connectableComponentWrapper.setProps(newProps);
|
|
284
|
-
connectableComponentWrapper.update();
|
|
285
|
-
publishAnEventToModel2();
|
|
286
|
-
let props = getChidViewProps();
|
|
287
|
-
expect(props.foo).toBe('the-event-value2');
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
describe('Publish Event Context', () => {
|
|
292
|
-
beforeEach(() => {
|
|
293
|
-
setup({useConnectFunction: false});
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
// // Cant test hooks yet, will test using state below
|
|
297
|
-
// // https://github.com/enzymejs/enzyme/issues/2011
|
|
298
|
-
// xit('Can publish event via context', () => {
|
|
299
|
-
// const childViewContext = connectableComponentWrapper.context();
|
|
300
|
-
// let publishEvent: PublishModelEventDelegate = React.useContext(PublishModelEventContext);
|
|
301
|
-
// publishEvent('test-event', 'the-event-value');
|
|
302
|
-
// connectableComponentWrapper.update();
|
|
303
|
-
// let props = getChidViewProps();
|
|
304
|
-
// expect(props.foo).toBe('the-event-value');
|
|
305
|
-
// });
|
|
306
|
-
|
|
307
|
-
// Note this is a best effort to test the context hook created via state
|
|
308
|
-
it('publishEvent set', () => {
|
|
309
|
-
const state: any = connectableComponentWrapper.state();
|
|
310
|
-
state.publishEvent('test-event', 'the-event-value');
|
|
311
|
-
connectableComponentWrapper.update();
|
|
312
|
-
let props = getChidViewProps();
|
|
313
|
-
expect(props.model.value).toBe('the-event-value');
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it('publishEvent recreated on model change', () => {
|
|
317
|
-
const newProps = {
|
|
318
|
-
...connectableComponentWrapper.props(),
|
|
319
|
-
modelId: 'model-id2',
|
|
320
|
-
};
|
|
321
|
-
connectableComponentWrapper.setProps(newProps);
|
|
322
|
-
connectableComponentWrapper.update();
|
|
323
|
-
const state: any = connectableComponentWrapper.state();
|
|
324
|
-
state.publishEvent('test-event', 'the-event-value');
|
|
325
|
-
connectableComponentWrapper.update();
|
|
326
|
-
let props = getChidViewProps();
|
|
327
|
-
expect(props.model.value).toBe('the-event-value');
|
|
328
|
-
expect(testModel2.value).toBe('the-event-value');
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
describe('View rendering', () => {
|
|
333
|
-
it('Renders the view found via @viewBinding', () => {
|
|
334
|
-
setup({useConnectFunction: false});
|
|
335
|
-
expect(connectableComponentWrapper.containsMatchingElement(<TestModelView1/>)).toBeTruthy();
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
it('Renders alternative view found via @viewBinding', () => {
|
|
339
|
-
setup({useConnectFunction: false, useAlternativeViewContext: true});
|
|
340
|
-
expect(connectableComponentWrapper.containsMatchingElement(<TestModelView3/>)).toBeTruthy();
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
describe('Overriding model/state selector', () => {
|
|
345
|
-
function testModelHasBeenReplacedWithMappedModel(expectedValue3) {
|
|
346
|
-
let props = getChidViewProps();
|
|
347
|
-
let mappedModel = props.model as any as MappedModel;
|
|
348
|
-
expect(mappedModel.value2).toBe('initial-value');
|
|
349
|
-
expect(mappedModel.value3).toBe(expectedValue3);
|
|
350
|
-
publishAnEventToModel1();
|
|
351
|
-
props = getChidViewProps();
|
|
352
|
-
mappedModel = props.model as any as MappedModel;
|
|
353
|
-
expect(mappedModel.value2).toBe('the-event-value');
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function testSwappedModelPassedToMapModelToPropsFunction(expectedValue3) {
|
|
357
|
-
let props = getChidViewProps();
|
|
358
|
-
expect(props.value2).toBe('initial-value');
|
|
359
|
-
expect(props.value3).toBe(expectedValue3);
|
|
360
|
-
publishAnEventToModel1();
|
|
361
|
-
props = getChidViewProps();
|
|
362
|
-
expect(props.value2).toBe('the-event-value');
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
describe('using @stateToRenderSelector', () => {
|
|
366
|
-
it('Swaps the model for that returned by the function decorated with a @stateToRenderSelector decorator', () => {
|
|
367
|
-
setup({useConnectFunction: false, useRenderModelSelectorDecorator: true});
|
|
368
|
-
testModelHasBeenReplacedWithMappedModel('value-3');
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('passes the swapped model to mapModelToProps function', () => {
|
|
372
|
-
setup({useConnectFunction: false, useRenderModelSelectorDecorator: true, useMapModelToProps: true});
|
|
373
|
-
testSwappedModelPassedToMapModelToPropsFunction('value-3');
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
describe('using state to render function', () => {
|
|
378
|
-
it('Swaps the model for that returned by the function decorated with a @stateToRenderSelector decorator', () => {
|
|
379
|
-
setup({useConnectFunction: false, useRenderModelSelectorFunction: true});
|
|
380
|
-
testModelHasBeenReplacedWithMappedModel('value-3.1');
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
it('passes the swapped model to mapModelToProps function', () => {
|
|
384
|
-
setup({useConnectFunction: false, useRenderModelSelectorFunction: true, useMapModelToProps: true});
|
|
385
|
-
testSwappedModelPassedToMapModelToPropsFunction('value-3.1');
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
});
|
|
389
|
-
});
|
|
390
|
-
});
|