Adding tests for rendering layout

This commit is contained in:
Jeff Avallone 2019-02-01 08:49:55 -05:00
parent b3b7480358
commit 5a782b439e
3 changed files with 170 additions and 2 deletions

View File

@ -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",
}
`;

View File

@ -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,

79
src/layout.test.js Normal file
View File

@ -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();
});
});