Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
grantjbutler committed Feb 10, 2022
1 parent f175300 commit e6a32d7
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/renderer/tests/FlexComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, it, expect } from 'vitest';
import { ContainerLayoutNode, FlexComponent, Frame, Size, SourceComponent, SourceLayoutNode } from '../src/layout';

describe('FlexComponent', () => {
it('should layout vertically', () => {
const component = new FlexComponent();
component.direction = 'vertical';
component.spacing = 10;

const childA = new SourceComponent();
childA.source = {
name: 'A',
width: 100,
height: 100,
};
component.addChild(childA);

const childB = new SourceComponent();
childB.source = {
name: 'B',
width: 100,
height: 100,
};
component.addChild(childB);

const node = component.exerciseLayout(new Size(100, 210));
expect(node.frame).to.eql(new Frame(0, 0, 100, 210));
expect(node).to.be.an.instanceOf(ContainerLayoutNode);
expect((node as ContainerLayoutNode).children).to.have.lengthOf(2);

const aNode = (node as ContainerLayoutNode).children[0];
const bNode = (node as ContainerLayoutNode).children[1];
expect(aNode.frame).to.eql(new Frame(0, 0, 100, 100));
expect(aNode).to.be.an.instanceOf(SourceLayoutNode);

expect(bNode.frame).to.eql(new Frame(0, 110, 100, 100));
expect(bNode).to.be.an.instanceOf(SourceLayoutNode);
});
});
26 changes: 26 additions & 0 deletions packages/renderer/tests/InsetComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, it, expect } from 'vitest';
import { Frame, InsetComponent, Size, SourceComponent, Insets, ContainerLayoutNode, SourceLayoutNode } from '../src/layout';

describe('InsetComponent', () => {
it('should layout', () => {
const component = new InsetComponent();
component.insets = new Insets(10, 10, 10, 10);

const sourceComponent = new SourceComponent();
sourceComponent.source = {
name: 'source',
width: 1920,
height: 1080,
};
component.addChild(sourceComponent);

const node = component.exerciseLayout(new Size(1920, 1080));
expect(node.frame).to.eql(new Frame(0, 0, 1904.4444444444446, 1080));
expect(node).to.be.an.instanceOf(ContainerLayoutNode);
expect((node as ContainerLayoutNode).children).to.have.lengthOf(1);

const childNode = (node as ContainerLayoutNode).children[0];
expect(childNode.frame).to.eql(new Frame(10, 10, 1884.4444444444446, 1060));
expect(childNode).to.be.an.instanceOf(SourceLayoutNode);
});
});
77 changes: 77 additions & 0 deletions packages/renderer/tests/LayoutExerciser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest';
import { FlexComponent, Frame, InsetComponent, LayoutExerciser, Size, SourceComponent, Insets, ContainerLayoutNode, SourceLayoutNode } from '../src/layout';

describe('LayoutExerciser', () => {
it('should perform layout', () => {
const component = new SourceComponent();
component.source = {
name: 'source',
width: 160,
height: 90,
};

const exerciser = new LayoutExerciser();
const node = exerciser.execute(component, new Size(160, 100));

expect(node.frame).to.eql(new Frame(0, 5, 160, 90));
expect(node).to.be.an.instanceOf(SourceLayoutNode);
});

it('should perform complex layout', () => {
const rootComponent = new InsetComponent();
rootComponent.insets = new Insets(50, 50, 50, 50);

const verticalFlexComponent = new FlexComponent();
verticalFlexComponent.direction = 'vertical';
verticalFlexComponent.spacing = 50;
rootComponent.addChild(verticalFlexComponent);

const horizontalFlexComponent = new FlexComponent();
horizontalFlexComponent.direction = 'horizontal';
horizontalFlexComponent.spacing = 50;
horizontalFlexComponent.addChild(newSource());
horizontalFlexComponent.addChild(newSource());

verticalFlexComponent.addChild(horizontalFlexComponent);
verticalFlexComponent.addChild(newSource());

const exerciser = new LayoutExerciser();
const node = exerciser.execute(rootComponent, new Size(1920, 1080));

expect(node.frame).to.eql(new Frame(0, 0, 1920, 1080));
expect(node).to.be.an.instanceOf(ContainerLayoutNode);
expect((node as ContainerLayoutNode).children).to.have.lengthOf(1);

const verticalFlexNode = (node as ContainerLayoutNode).children[0];
expect(verticalFlexNode.frame).to.eql(new Frame(50, 50, 1820, 980));
expect(verticalFlexNode).to.be.an.instanceOf(ContainerLayoutNode);
expect((verticalFlexNode as ContainerLayoutNode).children).to.have.lengthOf(2);

const horizontalFlexNode = (verticalFlexNode as ContainerLayoutNode).children[0];
expect(horizontalFlexNode.frame).to.eql(new Frame(0, 0, 1820, 465));
expect(horizontalFlexNode).to.be.an.instanceOf(ContainerLayoutNode);
expect((horizontalFlexNode as ContainerLayoutNode).children).to.have.lengthOf(2);

const bottomSourceNode = (verticalFlexNode as ContainerLayoutNode).children[1];
expect(bottomSourceNode.frame).to.eql(new Frame(496.66666666666663, 515, 826.6666666666667, 465));
expect(bottomSourceNode).to.be.an.instanceOf(SourceLayoutNode);

const topLeftSourceNode = (horizontalFlexNode as ContainerLayoutNode).children[0];
expect(topLeftSourceNode.frame).to.eql(new Frame(58.33333333333326, 0, 826.6666666666667, 465));
expect(topLeftSourceNode).to.be.an.instanceOf(SourceLayoutNode);

const topRightSourceNode = (horizontalFlexNode as ContainerLayoutNode).children[1];
expect(topRightSourceNode.frame).to.eql(new Frame(935, 0, 826.6666666666667, 465));
expect(topRightSourceNode).to.be.an.instanceOf(SourceLayoutNode);
});
});

function newSource(): SourceComponent {
const component = new SourceComponent();
component.source = {
name: 'source',
width: 1920,
height: 1080,
};
return component;
}
17 changes: 17 additions & 0 deletions packages/renderer/tests/SourceComponent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it, expect } from 'vitest';
import { Frame, Size, SourceComponent, SourceLayoutNode } from '../src/layout';

describe('SourceComponent', () => {
it('should layout', () => {
const component = new SourceComponent();
component.source = {
name: 'source',
width: 1920,
height: 1080,
};

const node = component.exerciseLayout(new Size(1920, 1080));
expect(node.frame).to.eql(new Frame(0, 0, 1920, 1080));
expect(node).to.be.an.instanceOf(SourceLayoutNode);
});
});

0 comments on commit e6a32d7

Please sign in to comment.