esp-js-react 8.0.1-next.0 → 8.0.3-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.
Files changed (42) hide show
  1. package/.dist/esp-react.js +68 -104
  2. package/.dist/esp-react.js.map +1 -1
  3. package/.dist/esp-react.min.js +1 -1
  4. package/.dist/esp-react.min.js.map +1 -1
  5. package/.dist/typings/connect.d.ts +2 -6
  6. package/.dist/typings/connectableComponent.d.ts +0 -16
  7. package/.dist/typings/espModelContextProvider.d.ts +24 -7
  8. package/.dist/typings/espRouterContextProvider.d.ts +5 -5
  9. package/.dist/typings/index.d.ts +2 -3
  10. package/.dist/typings/polimer/getEspPolimerImmutableModel.d.ts +21 -0
  11. package/.dist/typings/useModelSelector.d.ts +25 -1
  12. package/.dist/typings/viewBinder.d.ts +8 -1
  13. package/.dist/typings/viewBindingDecorator.d.ts +1 -1
  14. package/package.json +4 -4
  15. package/src/connect.tsx +2 -6
  16. package/src/connectableComponent.tsx +2 -19
  17. package/src/espModelContextProvider.tsx +26 -9
  18. package/src/espRouterContextProvider.tsx +5 -5
  19. package/src/index.ts +5 -9
  20. package/src/polimer/getEspPolimerImmutableModel.ts +50 -0
  21. package/src/useModelSelector.ts +31 -7
  22. package/src/viewBinder.tsx +13 -2
  23. package/src/viewBindingDecorator.ts +2 -2
  24. package/tests/connectableComponentTests.tsx +496 -0
  25. package/tests/espModelContextProviderTests.tsx +142 -0
  26. package/tests/espRouterContextProviderTests.tsx +54 -0
  27. package/tests/indexTests.ts +24 -91
  28. package/tests/testApi/asserts.ts +90 -0
  29. package/tests/testApi/testApi.tsx +100 -0
  30. package/tests/testApi/testModel.ts +44 -0
  31. package/tests/testApi/useStoreReceivedProps.ts +14 -0
  32. package/tests/testApi/viewFactory.tsx +31 -0
  33. package/tests/useModelSelectorTests.tsx +47 -153
  34. package/tests/viewBinderTests.tsx +61 -0
  35. package/.dist/typings/polimer/getEspReactRenderModel.d.ts +0 -21
  36. package/.dist/typings/viewBinderConnectable.d.ts +0 -15
  37. package/src/polimer/getEspReactRenderModel.ts +0 -50
  38. package/src/viewBinderConnectable.tsx +0 -54
  39. package/tests/connectableComponentLegacyTests.tsx +0 -405
  40. /package/.dist/typings/polimer/{polimerExtentsions.d.ts → polimerModelBuilderExtentsions.d.ts} +0 -0
  41. /package/src/polimer/{polimerExtentsions.ts → polimerModelBuilderExtentsions.ts} +0 -0
  42. /package/tests/{routerSpy.ts → testApi/routerSpy.ts} +0 -0
@@ -1,405 +0,0 @@
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
- });
File without changes