diff --git a/src/__snapshots__/layout.test.js.snap b/src/__snapshots__/layout.test.js.snap new file mode 100644 index 0000000..d728295 --- /dev/null +++ b/src/__snapshots__/layout.test.js.snap @@ -0,0 +1,87 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`layout running against a node with a basic bounding box 1`] = ` +Object { + "box": Object { + "axisX1": 0, + "axisX2": 20, + "axisY": 5, + "height": 10, + "width": 20, + }, + "props": Object {}, + "type": "Example", +} +`; + +exports[`layout running against a node with a complete bounding box 1`] = ` +Object { + "box": Object { + "axisX1": 5, + "axisX2": 15, + "axisY": 2, + "height": 10, + "width": 20, + }, + "props": Object {}, + "type": "Example", +} +`; + +exports[`layout running against a node with props 1`] = ` +Object { + "box": Object { + "axisX1": 0, + "axisX2": 0, + "axisY": 0, + "height": 0, + "width": 0, + }, + "props": Object { + "property": "example", + }, + "type": "Example", +} +`; + +exports[`layout running against a simple node 1`] = ` +Object { + "box": Object { + "axisX1": 0, + "axisX2": 0, + "axisY": 0, + "height": 0, + "width": 0, + }, + "props": Object {}, + "type": "Example", +} +`; + +exports[`layout running layout on children 1`] = ` +Object { + "box": Object { + "axisX1": 0, + "axisX2": 0, + "axisY": 0, + "height": 0, + "width": 0, + }, + "children": Array [ + Object { + "box": Object { + "axisX1": 0, + "axisX2": 20, + "axisY": 5, + "height": 10, + "width": 20, + }, + "props": Object {}, + "type": "Other", + }, + "string example", + ], + "props": Object {}, + "type": "Example", +} +`; diff --git a/src/layout.js b/src/layout.js index aac4be1..bfda14e 100644 --- a/src/layout.js +++ b/src/layout.js @@ -20,10 +20,12 @@ const layout = data => { data.children = data.children.map(layout); } - const result = nodeTypes[type].layout({ + const normalizedData = { props: {}, + box: {}, ...data - }); + }; + const result = nodeTypes[type].layout(normalizedData) || normalizedData; return { ...result, diff --git a/src/layout.test.js b/src/layout.test.js new file mode 100644 index 0000000..d10fce2 --- /dev/null +++ b/src/layout.test.js @@ -0,0 +1,79 @@ +import layout from 'layout'; + +jest.mock('rendering/types', () => ({ + Example: { + layout: jest.fn() + }, + Other: { + layout: jest.fn() + } +})); + +import nodeTypes from 'rendering/types'; + +describe('layout', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + test('running against a string', () => { + expect(layout('test')).toEqual('test'); + }); + + test('running against a simple node', () => { + expect(layout({ + type: 'Example' + })).toMatchSnapshot(); + }); + + test('running against a node with a basic bounding box', () => { + nodeTypes.Example.layout.mockImplementation(data => { + data.box = { width: 20, height: 10 }; + return data; + }); + expect(layout({ + type: 'Example' + })).toMatchSnapshot(); + }); + + test('running against a node with a complete bounding box', () => { + nodeTypes.Example.layout.mockImplementation(data => { + data.box = { + width: 20, + height: 10, + axisY: 2, + axisX1: 5, + axisX2: 15 + }; + return data; + }); + expect(layout({ + type: 'Example' + })).toMatchSnapshot(); + }); + + test('running against a node with props', () => { + expect(layout({ + type: 'Example', + props: { + property: 'example' + } + })).toMatchSnapshot(); + }); + + test('running layout on children', () => { + nodeTypes.Other.layout.mockImplementation(data => { + data.box = { width: 20, height: 10 }; + return data; + }); + expect(layout({ + type: 'Example', + children: [ + { + type: 'Other' + }, + 'string example' + ] + })).toMatchSnapshot(); + }); +});