Procházet zdrojové kódy

commands notify listeners

Daniel Zlotin před 6 roky
rodič
revize
5848b9c8b5
2 změnil soubory, kde provedl 69 přidání a 38 odebrání
  1. 39
    25
      lib/src/commands/Commands.test.ts
  2. 30
    13
      lib/src/commands/Commands.ts

+ 39
- 25
lib/src/commands/Commands.test.ts Zobrazit soubor

@@ -333,37 +333,51 @@ describe('Commands', () => {
333 333
       });
334 334
     });
335 335
 
336
-    xit('notify on all commands', () => {
336
+    it('notify on all commands', () => {
337 337
       _.forEach(getAllMethodsOfUut(), (m) => {
338 338
         uut[m]();
339 339
       });
340 340
       expect(cb).toHaveBeenCalledTimes(getAllMethodsOfUut().length);
341 341
     });
342 342
 
343
-    it('setRoot', () => {
344
-      uut.setRoot({});
345
-      expect(cb).toHaveBeenCalledTimes(1);
346
-      expect(cb).toHaveBeenCalledWith('setRoot', { layout: 'parsed' });
347
-    });
348
-
349
-    it('setDefaultOptions', () => {
350
-      const options = { x: 1 };
351
-      uut.setDefaultOptions(options);
352
-      expect(cb).toHaveBeenCalledTimes(1);
353
-      expect(cb).toHaveBeenCalledWith('setDefaultOptions', { options });
354
-    });
355
-
356
-    it('setOptions', () => {
357
-      const options = { x: 1 };
358
-      uut.setOptions('compId', options);
359
-      expect(cb).toHaveBeenCalledTimes(1);
360
-      expect(cb).toHaveBeenCalledWith('setOptions', { componentId: 'compId', options });
361
-    });
362
-
363
-    it('showModal', () => {
364
-      uut.showModal({});
365
-      expect(cb).toHaveBeenCalledTimes(1);
366
-      expect(cb).toHaveBeenCalledWith('showModal', { layout: 'parsed' });
343
+    describe('passes correct params', () => {
344
+      const argsForMethodName = {
345
+        setRoot: [{}],
346
+        setDefaultOptions: [{}],
347
+        setOptions: ['id', {}],
348
+        showModal: [{}],
349
+        dismissModal: ['id'],
350
+        dismissAllModals: [],
351
+        push: ['id', {}],
352
+        pop: ['id', {}],
353
+        popTo: ['id'],
354
+        popToRoot: ['id'],
355
+        showOverlay: [{}],
356
+        dismissOverlay: ['id'],
357
+      };
358
+      const paramsForMethodName = {
359
+        setRoot: { layout: 'parsed' },
360
+        setDefaultOptions: { options: {} },
361
+        setOptions: { componentId: 'id', options: {} },
362
+        showModal: { layout: 'parsed' },
363
+        dismissModal: { componentId: 'id' },
364
+        dismissAllModals: {},
365
+        push: { componentId: 'id', layout: 'parsed' },
366
+        pop: { componentId: 'id', options: {} },
367
+        popTo: { componentId: 'id' },
368
+        popToRoot: { componentId: 'id' },
369
+        showOverlay: { layout: 'parsed' },
370
+        dismissOverlay: { componentId: 'id' },
371
+      };
372
+      _.forEach(getAllMethodsOfUut(), (m) => {
373
+        it(`for ${m}`, () => {
374
+          expect(argsForMethodName).toHaveProperty(m);
375
+          expect(paramsForMethodName).toHaveProperty(m);
376
+          _.invoke(uut, m, ...argsForMethodName[m]);
377
+          expect(cb).toHaveBeenCalledTimes(1);
378
+          expect(cb).toHaveBeenCalledWith(m, paramsForMethodName[m]);
379
+        });
380
+      });
367 381
     });
368 382
   });
369 383
 });

+ 30
- 13
lib/src/commands/Commands.ts Zobrazit soubor

@@ -49,45 +49,62 @@ export class Commands {
49 49
     return result;
50 50
   }
51 51
 
52
-  public dismissModal(id) {
53
-    return this.nativeCommandsSender.dismissModal(id);
52
+  public dismissModal(componentId) {
53
+    const result = this.nativeCommandsSender.dismissModal(componentId);
54
+    this.commandsObserver.notify('dismissModal', { componentId });
55
+    return result;
54 56
   }
55 57
 
56 58
   public dismissAllModals() {
57
-    return this.nativeCommandsSender.dismissAllModals();
59
+    const result = this.nativeCommandsSender.dismissAllModals();
60
+    this.commandsObserver.notify('dismissAllModals', {});
61
+    return result;
58 62
   }
59 63
 
60
-  public push(onComponentId, componentData) {
61
-    const input = _.cloneDeep(componentData);
64
+  public push(componentId, simpleApi) {
65
+    const input = _.cloneDeep(simpleApi);
62 66
     this.optionsProcessor.processOptions(input);
63 67
     const layout = this.layoutTreeParser.parse(input);
64 68
     this.layoutTreeCrawler.crawl(layout);
65
-    return this.nativeCommandsSender.push(onComponentId, layout);
69
+
70
+    const result = this.nativeCommandsSender.push(componentId, layout);
71
+    this.commandsObserver.notify('push', { componentId, layout });
72
+    return result;
66 73
   }
67 74
 
68 75
   public pop(componentId, options) {
69
-    return this.nativeCommandsSender.pop(componentId, options);
76
+    const result = this.nativeCommandsSender.pop(componentId, options);
77
+    this.commandsObserver.notify('pop', { componentId, options });
78
+    return result;
70 79
   }
71 80
 
72 81
   public popTo(componentId) {
73
-    return this.nativeCommandsSender.popTo(componentId);
82
+    const result = this.nativeCommandsSender.popTo(componentId);
83
+    this.commandsObserver.notify('popTo', { componentId });
84
+    return result;
74 85
   }
75 86
 
76 87
   public popToRoot(componentId) {
77
-    return this.nativeCommandsSender.popToRoot(componentId);
88
+    const result = this.nativeCommandsSender.popToRoot(componentId);
89
+    this.commandsObserver.notify('popToRoot', { componentId });
90
+    return result;
78 91
   }
79 92
 
80
-  public showOverlay(componentData) {
81
-    const input = _.cloneDeep(componentData);
93
+  public showOverlay(simpleApi) {
94
+    const input = _.cloneDeep(simpleApi);
82 95
     this.optionsProcessor.processOptions(input);
83 96
 
84 97
     const layout = this.layoutTreeParser.parse(input);
85 98
     this.layoutTreeCrawler.crawl(layout);
86 99
 
87
-    return this.nativeCommandsSender.showOverlay(layout);
100
+    const result = this.nativeCommandsSender.showOverlay(layout);
101
+    this.commandsObserver.notify('showOverlay', { layout });
102
+    return result;
88 103
   }
89 104
 
90 105
   public dismissOverlay(componentId) {
91
-    return this.nativeCommandsSender.dismissOverlay(componentId);
106
+    const result = this.nativeCommandsSender.dismissOverlay(componentId);
107
+    this.commandsObserver.notify('dismissOverlay', { componentId });
108
+    return result;
92 109
   }
93 110
 }