Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Nov 5, 2024
1 parent ba92c7f commit a538980
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 3 deletions.
160 changes: 158 additions & 2 deletions src/wireframes/model/constraints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* Copyright (c) Sebastian Stehle. All rights reserved.
*/

import { Vec2 } from '@app/core/utils';
import { TextMeasurer, Vec2 } from '@app/core/utils';
import { DefaultAppearance } from '@app/wireframes/interface';
import { DiagramItem, MinSizeConstraint, SizeConstraint, TextHeightConstraint } from '@app/wireframes/model';
import { DiagramItem, MinSizeConstraint, SizeConstraint, TextHeightConstraint, TextSizeConstraint } from '@app/wireframes/model';

const shape = DiagramItem.createShape({
id: '1',
Expand Down Expand Up @@ -72,3 +72,159 @@ describe('SizeConstraint', () => {
expect(constraint.calculateSizeY()).toBeTruthy();
});
});


describe('TextSizeConstraint', () => {
let measured = 0;

const measurer: TextMeasurer = {
getTextWidth(text, fontSize) {
measured++;
return text.length * fontSize * 1.5;
},
};

beforeEach(() => {
measured = 0;
});

it('should only calculate width when width cannot be resized', () => {
const constraint1 = new TextSizeConstraint(measurer, 5, 5, 1.2, true, 10);
const constraint2 = new TextSizeConstraint(measurer, 5, 5, 1.2, false, 10);

expect(constraint1.calculateSizeX()).toBeFalsy();
expect(constraint1.calculateSizeY()).toBeTruthy();

expect(constraint2.calculateSizeX()).toBeTruthy();
expect(constraint2.calculateSizeY()).toBeTruthy();
});

it('should calculate size from measurer without padding', () => {
const constraint2 = new TextSizeConstraint(measurer, 0, 0, 1.5, true, 10);

const newShape: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const size = constraint2.updateSize(newShape, Vec2.ZERO, null!);

expect(size.x).toBe(120);
expect(size.y).toBe(24);
});

it('should calculate size from measurer with padding', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 10);

const newShape: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const size = constraint2.updateSize(newShape, Vec2.ZERO);

expect(size.x).toBe(126);
expect(size.y).toBe(32);
});

it('should calculate size from measurer min width', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 200);

const newShape: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const size = constraint2.updateSize(newShape, Vec2.ZERO);

expect(size.x).toBe(200);
expect(size.y).toBe(32);
});

it('should cache font size', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 200);

const shape1: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const shape2: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

constraint2.updateSize(shape1, Vec2.ZERO);
constraint2.updateSize(shape2, Vec2.ZERO, shape1);

expect(measured).toBe(1);
});

it('should recompute font width if text changed', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 200);

const shape1: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const shape2: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'World',
} as any;

constraint2.updateSize(shape1, Vec2.ZERO);
constraint2.updateSize(shape2, Vec2.ZERO, shape1);

expect(measured).toBe(2);
});

it('should recompute font width if font family changed', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 200);

const shape1: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const shape2: DiagramItem = {
fontSize: 16,
fontFamily: 'Aria',
text: 'Hello',
} as any;

constraint2.updateSize(shape1, Vec2.ZERO);
constraint2.updateSize(shape2, Vec2.ZERO, shape1);

expect(measured).toBe(2);
});

it('should recompute font width if font size changed', () => {
const constraint2 = new TextSizeConstraint(measurer, 3, 4, 1.5, true, 200);

const shape1: DiagramItem = {
fontSize: 16,
fontFamily: 'monospace',
text: 'Hello',
} as any;

const shape2: DiagramItem = {
fontSize: 18,
fontFamily: 'Aria',
text: 'Hello',
} as any;

constraint2.updateSize(shape1, Vec2.ZERO);
constraint2.updateSize(shape2, Vec2.ZERO, shape1);

expect(measured).toBe(2);
});
});
2 changes: 1 addition & 1 deletion src/wireframes/model/constraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class TextSizeConstraint implements Constraint {
private readonly minWidth = 0,
) { }

public updateSize(shape: Shape, size: Vec2, prev: DiagramItem): Vec2 {
public updateSize(shape: Shape, size: Vec2, prev?: DiagramItem): Vec2 {
const fontSize = shape.fontSize;
const fontFamily = shape.fontFamily;

Expand Down

0 comments on commit a538980

Please sign in to comment.