Browse Source

deep clones to avoid mutation errors

Daniel Zlotin 8 years ago
parent
commit
aca3151308
2 changed files with 13 additions and 6 deletions
  1. 6
    6
      src/commands/LayoutTreeParser.js
  2. 7
    0
      src/commands/LayoutTreeParser.test.js

+ 6
- 6
src/commands/LayoutTreeParser.js View File

@@ -16,14 +16,14 @@ export default class LayoutTreeParser {
16 16
   }
17 17
 
18 18
   parseFromSimpleJSON(simpleJsonApi) {
19
-    // TOOD deepclone
20
-    if (simpleJsonApi.sideMenu) {
21
-      return this._createSideMenu(simpleJsonApi);
19
+    const input = _.cloneDeep(simpleJsonApi);
20
+    if (input.sideMenu) {
21
+      return this._createSideMenu(input);
22 22
     }
23
-    if (simpleJsonApi.tabs) {
24
-      return this._createTabs(simpleJsonApi.tabs);
23
+    if (input.tabs) {
24
+      return this._createTabs(input.tabs);
25 25
     }
26
-    return this._createContainerStackWithContainer(simpleJsonApi.container);
26
+    return this._createContainerStackWithContainer(input.container);
27 27
   }
28 28
 
29 29
   _node(node) {

+ 7
- 0
src/commands/LayoutTreeParser.test.js View File

@@ -26,6 +26,12 @@ describe('LayoutTreeParser', () => {
26 26
       });
27 27
   });
28 28
 
29
+  it('deep clones to avoid mutations', () => {
30
+    const obj = {};
31
+    const result = uut.parseFromSimpleJSON({ container: { foo: obj } });
32
+    expect(result.children[0].data.foo).not.toBe(obj);
33
+  });
34
+
29 35
   it('parses single screen', () => {
30 36
     expect(uut.parseFromSimpleJSON(SimpleLayouts.singleScreenApp))
31 37
       .toEqual({
@@ -69,6 +75,7 @@ describe('LayoutTreeParser', () => {
69 75
           }
70 76
         ]
71 77
       });
78
+    expect(uut.parseFromSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams).children[0].data.passProps.bar).toBe(SimpleLayouts.passedFunction);
72 79
     expect(uut.parseFromSimpleJSON(SimpleLayouts.singleScreenWithAditionalParams).children[0].data.passProps.bar()).toEqual('Hello from a function');
73 80
   });
74 81