Skip to content

Commit

Permalink
feat: first test
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianCraig committed Feb 25, 2020
1 parent dca8e38 commit 678a7a6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 30 deletions.
9 changes: 0 additions & 9 deletions src/App.test.tsx

This file was deleted.

49 changes: 49 additions & 0 deletions src/constraints/constraint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ResolveConstraint } from "./constraint";
import { ComponentInstance, Side, ConstraintInstance } from "./definition";

const ParentComponent = (): ComponentInstance => ({
name: "parent",
positions: {
[Side.top]: 0,
[Side.right]: 100,
[Side.bottom]: 100,
[Side.left]: 0
}
});

const NewComponent = (name: string, positions = {}): ComponentInstance => ({
name,
positions
});

describe("constraint resolving tests", () => {
test("a resolved constraint doesn't do anything", () => {
const testedComponent = NewComponent("nameX");
const constraint: ConstraintInstance = {
fromInstance: testedComponent,
fromSide: Side.top,
toInstance: ParentComponent(),
toSide: Side.top,
resolved: Boolean(true),
distance: 10
};
ResolveConstraint(constraint);
expect(constraint.resolved).toBe(Boolean(true));
expect(testedComponent.positions[Side.top]).toBe(undefined);
});

test("a simple constraint from parent", () => {
const testedComponent = NewComponent("nameX");
const constraint: ConstraintInstance = {
fromInstance: testedComponent,
fromSide: Side.top,
toInstance: ParentComponent(),
toSide: Side.top,
resolved: Boolean(false),
distance: 10
};
ResolveConstraint(constraint);
expect(constraint.resolved).toBe(Boolean(true));
expect(testedComponent.positions[Side.top]).toBe(10);
});
});
14 changes: 14 additions & 0 deletions src/constraints/constraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LayoutInstance } from "./generator";
import { Side, ConstraintSize, ConstraintInstance } from "./definition";

export const ResolveConstraint = (constraint: ConstraintInstance): void => {
const {
fromInstance,
fromSide,
toInstance,
toSide,
distance,
resolved
} = constraint;
if (resolved) return;
};
33 changes: 28 additions & 5 deletions src/constraints/definition.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
export type ConstraintSize = number;

export type ConstraintSide = "top" | "right" | "bottom" | "left";
export enum Side {
top = "top",
right = "right",
bottom = "bottom",
left = "left"
}

export interface ConstraintDefinition {
component: string;
side: ConstraintSide;
side: keyof typeof Side;
distance: ConstraintSize;
}

export interface ConstraintComponent {
export interface ComponentDefinition {
constraints: ConstraintDefinition[];
width?: ConstraintSize;
height?: ConstraintSize;
}

export interface ConstraintLayout {
[key: string]: ConstraintComponent;
export interface LayoutDefinition {
[key: string]: ComponentDefinition;
}

export interface ComponentInstance {
name: string;
positions: { [key in Side]?: number };
}

export type ComponentInstances = ComponentInstance[];

export interface ConstraintInstance {
fromInstance: ComponentInstance;
fromSide: Side;
toInstance: ComponentInstance;
toSide: Side;
distance: ConstraintSize;
resolved: Boolean;
}

export type ConstraintInstances = ConstraintInstance[];
8 changes: 4 additions & 4 deletions src/constraints/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConstraintLayout } from "./definition";
import { LayoutDefinition, Side } from "./definition";

export const simpleBlock: ConstraintLayout = {
export const simpleBlock: LayoutDefinition = {
Block: {
constraints: [
{
Expand All @@ -11,12 +11,12 @@ export const simpleBlock: ConstraintLayout = {
{
component: "parent",
side: "right",
distance: 32
distance: -32
},
{
component: "parent",
side: "bottom",
distance: 32
distance: -32
},
{
component: "parent",
Expand Down
17 changes: 5 additions & 12 deletions src/constraints/generator.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import React from "react";
import { ConstraintLayout, ConstraintComponent } from "./definition";
import { LayoutDefinition, ComponentDefinition, Side } from "./definition";

export interface LayoutComponent {
width: number;
height: number;
[key: string]: React.ReactNode;
}

enum Side {
top = "top",
right = "right",
bottom = "bottom",
left = "left"
}

class LayoutInstance {
export class LayoutInstance {
private layoutInstanceList: LayoutInstance[];
private name: string;
private constraint: ConstraintComponent;
private constraint: ComponentDefinition;
private resolvedPositions: { [key in Side]?: number } = {};

constructor(
layoutInstanceList: LayoutInstance[],
name: string,
constraint: ConstraintComponent
constraint: ComponentDefinition
) {
this.layoutInstanceList = layoutInstanceList;
this.name = name;
Expand Down Expand Up @@ -51,7 +44,7 @@ class LayoutInstance {
}

export const createLayoutComponent = (
options: ConstraintLayout
options: LayoutDefinition
): React.FunctionComponent<LayoutComponent> => ({
width,
height,
Expand Down

0 comments on commit 678a7a6

Please sign in to comment.