Browse Source

Fix broken static options provided as objects

RNN supports static options provided as both functions and objects.
Static objects broke in a recent PR - 08f8581b3f
Guy Carmeli 5 years ago
parent
commit
4d82292950
2 changed files with 27 additions and 7 deletions
  1. 19
    0
      lib/src/commands/LayoutTreeCrawler.test.ts
  2. 8
    7
      lib/src/commands/LayoutTreeCrawler.ts

+ 19
- 0
lib/src/commands/LayoutTreeCrawler.test.ts View File

@@ -56,6 +56,25 @@ describe('LayoutTreeCrawler', () => {
56 56
     expect(node.data.options).toEqual({ popGesture: true });
57 57
   });
58 58
 
59
+  it('Components: injects options from original component class static property', () => {
60
+    when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
61
+      () =>
62
+        class extends React.Component {
63
+          static options = {
64
+            popGesture: true
65
+          };
66
+        }
67
+    );
68
+    const node = {
69
+      id: 'testId',
70
+      type: LayoutType.Component,
71
+      data: { name: 'theComponentName', options: {} },
72
+      children: []
73
+    };
74
+    uut.crawl(node);
75
+    expect(node.data.options).toEqual({ popGesture: true });
76
+  });
77
+
59 78
   it('Components: crawl does not cache options', () => {
60 79
     when(mockedStore.getComponentClassForName('theComponentName')).thenReturn(
61 80
       () =>

+ 8
- 7
lib/src/commands/LayoutTreeCrawler.ts View File

@@ -46,16 +46,17 @@ export class LayoutTreeCrawler {
46 46
     return (component as ComponentWithOptions).options !== undefined;
47 47
   }
48 48
 
49
+  private applyStaticOptions(node: LayoutNode) {
50
+    node.data.options = _.merge({}, this.staticOptionsIfPossible(node), node.data.options);
51
+  }
52
+
49 53
   private staticOptionsIfPossible(node: LayoutNode) {
50 54
     const foundReactGenerator = this.store.getComponentClassForName(node.data.name!);
51 55
     const reactComponent = foundReactGenerator ? foundReactGenerator() : undefined;
52
-    return reactComponent && this.isComponentWithOptions(reactComponent)
53
-      ? reactComponent.options(node.data.passProps || {})
54
-      : {};
55
-  }
56
-
57
-  private applyStaticOptions(node: LayoutNode) {
58
-    node.data.options = _.merge({}, this.staticOptionsIfPossible(node), node.data.options);
56
+    if (reactComponent && this.isComponentWithOptions(reactComponent)) {
57
+      return _.isFunction(reactComponent.options) ? reactComponent.options(node.data.passProps || {}) : reactComponent.options;
58
+    }
59
+    return {};
59 60
   }
60 61
 
61 62
   private assertComponentDataName(component: LayoutNode) {