Browse Source

notifying commands

Daniel Zlotin 6 years ago
parent
commit
f0aeb62d69
2 changed files with 34 additions and 11 deletions
  1. 30
    9
      lib/src/commands/Commands.test.ts
  2. 4
    2
      lib/src/commands/Commands.ts

+ 30
- 9
lib/src/commands/Commands.test.ts View File

@@ -303,28 +303,43 @@ describe('Commands', () => {
303 303
       uut = new Commands(mockCommandsSender, mockParser, mockCrawler, commandsObserver);
304 304
     });
305 305
 
306
-    it('always call last', () => {
306
+    function getAllMethodsOfUut() {
307
+      const uutFns = Object.getOwnPropertyNames(Commands.prototype);
308
+      const methods = _.filter(uutFns, (fn) => fn !== 'constructor');
309
+      expect(methods.length).toBeGreaterThan(1);
310
+      return methods;
311
+    }
312
+
313
+    function getAllMethodsOfNativeCommandsSender() {
307 314
       const nativeCommandsSenderFns = _.functions(mockCommandsSender);
308 315
       expect(nativeCommandsSenderFns.length).toBeGreaterThan(1);
316
+      return nativeCommandsSenderFns;
317
+    }
309 318
 
319
+    it('always call last, when nativeCommand fails, dont notify listeners', () => {
310 320
       // throw when calling any native commands sender
311
-      _.forEach(nativeCommandsSenderFns, (fn) => {
321
+      _.forEach(getAllMethodsOfNativeCommandsSender(), (fn) => {
312 322
         mockCommandsSender[fn].mockImplementation(() => {
313 323
           throw new Error(`throwing from mockNativeCommandsSender`);
314 324
         });
315 325
       });
316 326
 
317
-      // call all commands on uut, all should throw, no commandObservers called
318
-      const uutFns = Object.getOwnPropertyNames(Commands.prototype);
319
-      const methods = _.filter(uutFns, (fn) => fn !== 'constructor');
320
-      expect(methods.sort()).toEqual(nativeCommandsSenderFns.sort());
327
+      expect(getAllMethodsOfUut().sort()).toEqual(getAllMethodsOfNativeCommandsSender().sort());
321 328
 
322
-      _.forEach(methods, (m) => {
329
+      // call all commands on uut, all should throw, no commandObservers called
330
+      _.forEach(getAllMethodsOfUut(), (m) => {
323 331
         expect(() => uut[m]()).toThrow();
324 332
         expect(cb).not.toHaveBeenCalled();
325 333
       });
326 334
     });
327 335
 
336
+    xit('notify on all commands', () => {
337
+      _.forEach(getAllMethodsOfUut(), (m) => {
338
+        uut[m]();
339
+      });
340
+      expect(cb).toHaveBeenCalledTimes(getAllMethodsOfUut().length);
341
+    });
342
+
328 343
     it('setRoot', () => {
329 344
       uut.setRoot({});
330 345
       expect(cb).toHaveBeenCalledTimes(1);
@@ -338,11 +353,17 @@ describe('Commands', () => {
338 353
       expect(cb).toHaveBeenCalledWith('setDefaultOptions', { options });
339 354
     });
340 355
 
341
-    xit('setOptions', () => {
356
+    it('setOptions', () => {
342 357
       const options = { x: 1 };
343 358
       uut.setOptions('compId', options);
344 359
       expect(cb).toHaveBeenCalledTimes(1);
345
-      expect(cb).toHaveBeenCalledWith('setOptions', { componentId: 'compId', options: {} });
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' });
346 367
     });
347 368
   });
348 369
 });

+ 4
- 2
lib/src/commands/Commands.ts View File

@@ -20,7 +20,6 @@ export class Commands {
20 20
 
21 21
     const result = this.nativeCommandsSender.setRoot(layout);
22 22
     this.commandsObserver.notify('setRoot', { layout });
23
-
24 23
     return result;
25 24
   }
26 25
 
@@ -44,7 +43,10 @@ export class Commands {
44 43
     const input = _.cloneDeep(simpleApi);
45 44
     const layout = this.layoutTreeParser.parse(input);
46 45
     this.layoutTreeCrawler.crawl(layout);
47
-    return this.nativeCommandsSender.showModal(layout);
46
+
47
+    const result = this.nativeCommandsSender.showModal(layout);
48
+    this.commandsObserver.notify('showModal', { layout });
49
+    return result;
48 50
   }
49 51
 
50 52
   public dismissModal(id) {