From 5a782b439e523b5e3e089c76e9b13e8be4bd9d14 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 1 Feb 2019 08:49:55 -0500 Subject: [PATCH] Adding tests for rendering layout --- src/__snapshots__/layout.test.js.snap | 87 +++++++++++++++++++++++++++ src/layout.js | 6 +- src/layout.test.js | 79 ++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 src/__snapshots__/layout.test.js.snap create mode 100644 src/layout.test.js 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(); + }); +});