|
@@ -0,0 +1,73 @@
|
|
1
|
+import { ComponentEventsRegistry } from './ComponentEventsRegistry';
|
|
2
|
+import { Store } from '../components/Store';
|
|
3
|
+
|
|
4
|
+describe(`ComponentEventRegistry`, () => {
|
|
5
|
+ let uut: ComponentEventsRegistry;
|
|
6
|
+ let eventRegistry;
|
|
7
|
+ let store: Store;
|
|
8
|
+ let mockComponentRef;
|
|
9
|
+ const refId = 'myUniqueId';
|
|
10
|
+
|
|
11
|
+ beforeEach(() => {
|
|
12
|
+ eventRegistry = {
|
|
13
|
+ componentDidAppear: jest.fn(),
|
|
14
|
+ componentDidDisappear: jest.fn(),
|
|
15
|
+ onNavigationInteraction: jest.fn()
|
|
16
|
+ };
|
|
17
|
+
|
|
18
|
+ mockComponentRef = {
|
|
19
|
+ componentDidAppear: jest.fn(),
|
|
20
|
+ componentDidDisappear: jest.fn(),
|
|
21
|
+ onNavigationInteraction: jest.fn()
|
|
22
|
+ };
|
|
23
|
+
|
|
24
|
+ store = new Store();
|
|
25
|
+ store.setRefForId(refId, mockComponentRef);
|
|
26
|
+
|
|
27
|
+ uut = new ComponentEventsRegistry(eventRegistry, store);
|
|
28
|
+ });
|
|
29
|
+
|
|
30
|
+ it('register for lifecycle events on eventRegistry', () => {
|
|
31
|
+ expect(eventRegistry.componentDidAppear).toHaveBeenCalledTimes(0);
|
|
32
|
+ expect(eventRegistry.componentDidDisappear).toHaveBeenCalledTimes(0);
|
|
33
|
+ expect(eventRegistry.onNavigationInteraction).toHaveBeenCalledTimes(0);
|
|
34
|
+ uut.registerForAllComponents();
|
|
35
|
+ expect(eventRegistry.componentDidAppear).toHaveBeenCalledTimes(1);
|
|
36
|
+ expect(eventRegistry.componentDidDisappear).toHaveBeenCalledTimes(1);
|
|
37
|
+ expect(eventRegistry.onNavigationInteraction).toHaveBeenCalledTimes(1);
|
|
38
|
+ });
|
|
39
|
+
|
|
40
|
+ it('bubbles lifecycle to component from store', () => {
|
|
41
|
+ const params = {};
|
|
42
|
+ expect(mockComponentRef.componentDidAppear).toHaveBeenCalledTimes(0);
|
|
43
|
+ expect(mockComponentRef.componentDidDisappear).toHaveBeenCalledTimes(0);
|
|
44
|
+ expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledTimes(0);
|
|
45
|
+ uut.registerForAllComponents();
|
|
46
|
+ eventRegistry.componentDidAppear.mock.calls[0][0](refId);
|
|
47
|
+ eventRegistry.componentDidDisappear.mock.calls[0][0](refId);
|
|
48
|
+ eventRegistry.onNavigationInteraction.mock.calls[0][0](refId, params);
|
|
49
|
+ expect(mockComponentRef.componentDidAppear).toHaveBeenCalledTimes(1);
|
|
50
|
+ expect(mockComponentRef.componentDidDisappear).toHaveBeenCalledTimes(1);
|
|
51
|
+ expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledTimes(1);
|
|
52
|
+ expect(mockComponentRef.onNavigationInteraction).toHaveBeenCalledWith(params);
|
|
53
|
+ });
|
|
54
|
+
|
|
55
|
+ it('defensive unknown id', () => {
|
|
56
|
+ uut.registerForAllComponents();
|
|
57
|
+ expect(() => {
|
|
58
|
+ eventRegistry.componentDidAppear.mock.calls[0][0]('bad id');
|
|
59
|
+ eventRegistry.componentDidDisappear.mock.calls[0][0]('bad id');
|
|
60
|
+ eventRegistry.onNavigationInteraction.mock.calls[0][0]('bad id', {});
|
|
61
|
+ }).not.toThrow();
|
|
62
|
+ });
|
|
63
|
+
|
|
64
|
+ it('defensive method impl', () => {
|
|
65
|
+ store.setRefForId('myId', {});
|
|
66
|
+ uut.registerForAllComponents();
|
|
67
|
+ expect(() => {
|
|
68
|
+ eventRegistry.componentDidAppear.mock.calls[0][0]('myId');
|
|
69
|
+ eventRegistry.componentDidDisappear.mock.calls[0][0]('myId');
|
|
70
|
+ eventRegistry.onNavigationInteraction.mock.calls[0][0]('myId', {});
|
|
71
|
+ }).not.toThrow();
|
|
72
|
+ });
|
|
73
|
+});
|