From 97c41094343a9eae2f193d476c2b43dacc7fffd5 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 21 Apr 2014 16:52:53 -0700 Subject: [PATCH] Add reduceTest util --- src/Layout-test-utils.js | 82 +++++++++++++++++++++++++++++++++++- src/__tests__/Layout-test.js | 2 +- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/Layout-test-utils.js b/src/Layout-test-utils.js index e801d015..d5958b97 100644 --- a/src/Layout-test-utils.js +++ b/src/Layout-test-utils.js @@ -104,6 +104,85 @@ var layoutTestUtils = (function() { .toEqual(nameLayout(name, layoutB)); } + function isEqual(a, b) { + // computeLayout and computeDOMLayout output a tree with same ordered elements + return JSON.stringify(a) === JSON.stringify(b); + } + + function printNode(node) { + console.log( + JSON.stringify(node) + .replace(/"([a-zA-Z]+)":/g, '$1: ') + .replace(/,/g, ', ') + .replace(/"/g, '\'') + ); + } + function reduceTest(node) { + function isWorking() { + return isEqual( + computeDOMLayout(node), + computeLayout(node) + ); + } + if (isWorking()) { + return console.log('Bail early, already working'); + } + + var isModified = true; + + function rec(node) { + // Style + for (var key in node.style) { + var value = node.style[key]; + delete node.style[key]; + if (isWorking()) { + node.style[key] = value; + } else { + isModified = true; + } + } + // Round values + for (var key in node.style) { + var value = node.style[key]; + if (value > 100) { + node.style[key] = Math.round(value / 100) * 100; + } else if (value > 10) { + node.style[key] = Math.round(value / 10) * 10; + } else if (value > 0) { + node.style[key] = 5; + } + if (node.style[key] !== value) { + if (isWorking()) { + node.style[key] = value; + } else { + isModified = true; + } + } + } + // Children + for (var i = 0; node.children && i < node.children.length; ++i) { + var value = node.children[i]; + node.children.splice(i, 1); + if (isWorking() && node.children) { + node.children.splice(i, 0, value); + rec(node.children[i]); + } else { + i--; + isModified = true; + } + } + } + while (isModified) { + isModified = false; + rec(node); + } + + printNode(node); + printNode(computeDOMLayout(node)); + } + +reduceTest({ style : { top : 0, marginLeft : 18 }, children : [ { style : { left : -7, paddingRight : 2, alignItems : 'stretch' }, children : [ { style : { left : 9 } }, { style : { width : 377, top : 0, marginTop : 0, flexDirection : 'column' } }, { style : { paddingTop : 10 } } ] }, { style : { width : 222, paddingTop : 1, alignSelf : 'stretch' } }, { style : { width : 61, left : 3 } } ] }) + return { testLayout: function(node, expectedLayout) { var layout = computeLayout(node); @@ -115,6 +194,7 @@ var layoutTestUtils = (function() { expect({i: i, node: node, layout: computeLayout(node)}) .toEqual({i: i, node: node, layout: computeDOMLayout(node)}); }, - computeDOMLayout: computeDOMLayout + computeDOMLayout: computeDOMLayout, + reduceTest: reduceTest } })(); diff --git a/src/__tests__/Layout-test.js b/src/__tests__/Layout-test.js index 1e4d88da..d89070ba 100755 --- a/src/__tests__/Layout-test.js +++ b/src/__tests__/Layout-test.js @@ -502,7 +502,7 @@ describe('Layout', function() { {width: 12, height: 0, top: 0, left: 0} ]} ); - }) + }); it('should layout randomly', function() { function RNG(seed) {