Browse Source

merge static and passed navigationOptions (#2336)

Daniel Zlotin 6 years ago
parent
commit
993d2faadf
No account linked to committer's email address
2 changed files with 55 additions and 11 deletions
  1. 13
    3
      lib/src/commands/LayoutTreeCrawler.js
  2. 42
    8
      lib/src/commands/LayoutTreeCrawler.test.js

+ 13
- 3
lib/src/commands/LayoutTreeCrawler.js View File

@@ -22,12 +22,22 @@ class LayoutTreeCrawler {
22 22
 
23 23
   _handleContainer(node) {
24 24
     this._assertContainerDataName(node);
25
-    this.store.setPropsForContainerId(node.id, node.data.passProps);
26
-    const clazz = this.store.getOriginalContainerClassForName(node.data.name);
27
-    node.data.navigationOptions = _.cloneDeep(_.get(clazz, 'navigationOptions', {}));
25
+    this._savePropsToStore(node);
26
+    this._applyStaticNavigationOptions(node);
28 27
     OptionsProcessor.processOptions(node.data.navigationOptions);
29 28
   }
30 29
 
30
+  _savePropsToStore(node) {
31
+    this.store.setPropsForContainerId(node.id, node.data.passProps);
32
+  }
33
+
34
+  _applyStaticNavigationOptions(node) {
35
+    const clazz = this.store.getOriginalContainerClassForName(node.data.name) || {};
36
+    const staticOptions = _.cloneDeep(clazz.navigationOptions) || {};
37
+    const passedOptions = _.cloneDeep(node.data.navigationOptions) || {};
38
+    node.data.navigationOptions = _.merge({}, staticOptions, passedOptions);
39
+  }
40
+
31 41
   _assertKnownLayoutType(type) {
32 42
     if (!_.includes(LayoutTypes, type)) {
33 43
       throw new Error(`Unknown layout type ${type}`);

+ 42
- 8
lib/src/commands/LayoutTreeCrawler.test.js View File

@@ -61,6 +61,43 @@ describe('LayoutTreeCrawler', () => {
61 61
     expect(node.data.navigationOptions).toEqual(theStyle);
62 62
   });
63 63
 
64
+  it('Containers: merges navigationOptions from container class static property with passed options, favoring passed options', () => {
65
+    const theStyle = {
66
+      bazz: 123,
67
+      inner: {
68
+        foo: 'bar'
69
+      },
70
+      opt: 'exists only in static'
71
+    };
72
+    const MyContainer = class {
73
+      static get navigationOptions() {
74
+        return theStyle;
75
+      }
76
+    };
77
+
78
+    const passedOptions = {
79
+      aaa: 'exists only in passed',
80
+      bazz: 789,
81
+      inner: {
82
+        foo: 'this is overriden'
83
+      }
84
+    };
85
+
86
+    const node = { type: LayoutTypes.Container, data: { name: 'theContainerName', navigationOptions: passedOptions } };
87
+    store.setOriginalContainerClassForName('theContainerName', MyContainer);
88
+
89
+    uut.crawl(node);
90
+
91
+    expect(node.data.navigationOptions).toEqual({
92
+      aaa: 'exists only in passed',
93
+      bazz: 789,
94
+      inner: {
95
+        foo: 'this is overriden'
96
+      },
97
+      opt: 'exists only in static'
98
+    });
99
+  });
100
+
64 101
   it('Containers: deepClones navigationOptions', () => {
65 102
     const theStyle = {};
66 103
     const MyContainer = class {
@@ -91,25 +128,20 @@ describe('LayoutTreeCrawler', () => {
91 128
 
92 129
   describe('navigation options', () => {
93 130
     let navigationOptions;
94
-    let MyContainer;
95 131
     let node;
96 132
 
97 133
     beforeEach(() => {
98 134
       navigationOptions = {};
99
-      MyContainer = class {
100
-        static get navigationOptions() {
101
-          return navigationOptions;
102
-        }
103
-      };
104
-      node = { type: LayoutTypes.Container, data: { name: 'theContainerName' } };
105
-      store.setOriginalContainerClassForName('theContainerName', MyContainer);
135
+      node = { type: LayoutTypes.Container, data: { name: 'theContainerName', navigationOptions } };
106 136
     });
107 137
 
108 138
     it('processes colors into numeric AARRGGBB', () => {
109 139
       navigationOptions.someKeyColor = 'red';
110 140
       uut.crawl(node);
111 141
       expect(node.data.navigationOptions.someKeyColor).toEqual(0xffff0000);
142
+    });
112 143
 
144
+    it('processes colors into numeric AARRGGBB', () => {
113 145
       navigationOptions.someKeyColor = 'yellow';
114 146
       uut.crawl(node);
115 147
       expect(node.data.navigationOptions.someKeyColor).toEqual(0xffffff00);
@@ -119,7 +151,9 @@ describe('LayoutTreeCrawler', () => {
119 151
       navigationOptions.someKeyColor = '#123456';
120 152
       uut.crawl(node);
121 153
       expect(node.data.navigationOptions.someKeyColor).toEqual(0xff123456);
154
+    });
122 155
 
156
+    it('processes numeric colors with rrggbbAA', () => {
123 157
       navigationOptions.someKeyColor = 0x123456ff; // wut
124 158
       uut.crawl(node);
125 159
       expect(node.data.navigationOptions.someKeyColor).toEqual(0xff123456);