2022-09-22 15:40:38 +05:30
|
|
|
import { BOUND_TEXT_PADDING } from "../constants";
|
2023-02-21 12:36:43 +05:30
|
|
|
import { API } from "../tests/helpers/api";
|
|
|
|
import {
|
|
|
|
computeContainerHeightForBoundText,
|
|
|
|
getContainerCoords,
|
2023-02-22 16:28:12 +05:30
|
|
|
getMaxContainerWidth,
|
|
|
|
getMaxContainerHeight,
|
2023-02-21 12:36:43 +05:30
|
|
|
measureText,
|
|
|
|
wrapText,
|
|
|
|
} from "./textElement";
|
2021-12-28 16:52:57 +05:30
|
|
|
import { FontString } from "./types";
|
|
|
|
|
|
|
|
describe("Test wrapText", () => {
|
|
|
|
const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
|
|
|
|
|
2022-12-21 12:32:43 +05:30
|
|
|
it("shouldn't add new lines for trailing spaces", () => {
|
|
|
|
const text = "Hello whats up ";
|
|
|
|
const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
|
|
|
|
const res = wrapText(text, font, maxWidth);
|
|
|
|
expect(res).toBe("Hello whats up ");
|
|
|
|
});
|
|
|
|
|
2023-01-26 03:19:21 -03:00
|
|
|
it("should work with emojis", () => {
|
|
|
|
const text = "😀";
|
|
|
|
const maxWidth = 1;
|
|
|
|
const res = wrapText(text, font, maxWidth);
|
|
|
|
expect(res).toBe("😀");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show the text correctly when min width reached", () => {
|
|
|
|
const text = "Hello😀";
|
|
|
|
const maxWidth = 10;
|
|
|
|
const res = wrapText(text, font, maxWidth);
|
|
|
|
expect(res).toBe("H\ne\nl\nl\no\n😀");
|
|
|
|
});
|
|
|
|
|
2021-12-28 16:52:57 +05:30
|
|
|
describe("When text doesn't contain new lines", () => {
|
|
|
|
const text = "Hello whats up";
|
|
|
|
[
|
|
|
|
{
|
|
|
|
desc: "break all words when width of each word is less than container width",
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 90,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `Hello
|
|
|
|
whats
|
|
|
|
up`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "break all characters when width of each character is less than container width",
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 25,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `H
|
|
|
|
e
|
|
|
|
l
|
|
|
|
l
|
|
|
|
o
|
|
|
|
w
|
|
|
|
h
|
|
|
|
a
|
|
|
|
t
|
|
|
|
s
|
|
|
|
u
|
|
|
|
p`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "break words as per the width",
|
|
|
|
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 150,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `Hello whats
|
|
|
|
up`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fit the container",
|
|
|
|
|
|
|
|
width: 250,
|
|
|
|
res: "Hello whats up",
|
|
|
|
},
|
|
|
|
].forEach((data) => {
|
|
|
|
it(`should ${data.desc}`, () => {
|
2022-09-22 15:40:38 +05:30
|
|
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
2021-12-28 16:52:57 +05:30
|
|
|
expect(res).toEqual(data.res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("When text contain new lines", () => {
|
|
|
|
const text = `Hello
|
|
|
|
whats up`;
|
|
|
|
[
|
|
|
|
{
|
|
|
|
desc: "break all words when width of each word is less than container width",
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 90,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `Hello
|
|
|
|
whats
|
|
|
|
up`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "break all characters when width of each character is less than container width",
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 25,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `H
|
|
|
|
e
|
|
|
|
l
|
|
|
|
l
|
|
|
|
o
|
|
|
|
w
|
|
|
|
h
|
|
|
|
a
|
|
|
|
t
|
|
|
|
s
|
|
|
|
u
|
|
|
|
p`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "break words as per the width",
|
|
|
|
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 150,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `Hello
|
|
|
|
whats up`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fit the container",
|
|
|
|
|
|
|
|
width: 250,
|
|
|
|
res: `Hello
|
|
|
|
whats up`,
|
|
|
|
},
|
|
|
|
].forEach((data) => {
|
|
|
|
it(`should respect new lines and ${data.desc}`, () => {
|
2022-09-22 15:40:38 +05:30
|
|
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
2021-12-28 16:52:57 +05:30
|
|
|
expect(res).toEqual(data.res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("When text is long", () => {
|
|
|
|
const text = `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg break it now`;
|
|
|
|
[
|
|
|
|
{
|
|
|
|
desc: "fit characters of long string as per container width",
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 170,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `hellolongtextth
|
|
|
|
isiswhatsupwith
|
|
|
|
youIamtypingggg
|
|
|
|
gandtypinggg
|
|
|
|
break it now`,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: "fit characters of long string as per container width and break words as per the width",
|
|
|
|
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 130,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `hellolongte
|
|
|
|
xtthisiswha
|
|
|
|
tsupwithyou
|
|
|
|
Iamtypinggg
|
|
|
|
ggandtyping
|
2022-01-03 17:59:26 +05:30
|
|
|
gg break it
|
2021-12-28 16:52:57 +05:30
|
|
|
now`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "fit the long text when container width is greater than text length and move the rest to next line",
|
|
|
|
|
2022-01-03 17:59:26 +05:30
|
|
|
width: 600,
|
2021-12-28 16:52:57 +05:30
|
|
|
res: `hellolongtextthisiswhatsupwithyouIamtypingggggandtypinggg
|
|
|
|
break it now`,
|
|
|
|
},
|
|
|
|
].forEach((data) => {
|
|
|
|
it(`should ${data.desc}`, () => {
|
2022-09-22 15:40:38 +05:30
|
|
|
const res = wrapText(text, font, data.width - BOUND_TEXT_PADDING * 2);
|
2021-12-28 16:52:57 +05:30
|
|
|
expect(res).toEqual(data.res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-21 12:32:43 +05:30
|
|
|
|
|
|
|
describe("Test measureText", () => {
|
|
|
|
const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
|
|
|
|
const text = "Hello World";
|
|
|
|
|
|
|
|
it("should add correct attributes when maxWidth is passed", () => {
|
|
|
|
const maxWidth = 200 - BOUND_TEXT_PADDING * 2;
|
|
|
|
const res = measureText(text, font, maxWidth);
|
|
|
|
|
|
|
|
expect(res.container).toMatchInlineSnapshot(`
|
|
|
|
<div
|
2023-01-28 16:39:53 +05:30
|
|
|
style="position: absolute; white-space: pre-wrap; font: Emoji 20px 20px; min-height: 1em; max-width: 191px; overflow: hidden; word-break: break-word; line-height: 0px;"
|
2022-12-21 12:32:43 +05:30
|
|
|
>
|
|
|
|
<span
|
|
|
|
style="display: inline-block; overflow: hidden; width: 1px; height: 1px;"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should add correct attributes when maxWidth is not passed", () => {
|
|
|
|
const res = measureText(text, font);
|
|
|
|
|
|
|
|
expect(res.container).toMatchInlineSnapshot(`
|
|
|
|
<div
|
|
|
|
style="position: absolute; white-space: pre; font: Emoji 20px 20px; min-height: 1em;"
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
style="display: inline-block; overflow: hidden; width: 1px; height: 1px;"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
});
|
2023-02-21 12:36:43 +05:30
|
|
|
|
|
|
|
describe("Test getContainerCoords", () => {
|
|
|
|
const params = { width: 200, height: 100, x: 10, y: 20 };
|
2023-02-22 16:28:12 +05:30
|
|
|
|
2023-02-21 12:36:43 +05:30
|
|
|
it("should compute coords correctly when ellipse", () => {
|
2023-02-22 16:28:12 +05:30
|
|
|
const element = API.createElement({
|
2023-02-21 12:36:43 +05:30
|
|
|
type: "ellipse",
|
|
|
|
...params,
|
|
|
|
});
|
2023-02-22 16:28:12 +05:30
|
|
|
expect(getContainerCoords(element)).toEqual({
|
2023-02-21 12:36:43 +05:30
|
|
|
x: 44.2893218813452455,
|
|
|
|
y: 39.64466094067262,
|
|
|
|
});
|
|
|
|
});
|
2023-02-22 16:28:12 +05:30
|
|
|
|
2023-02-21 12:36:43 +05:30
|
|
|
it("should compute coords correctly when rectangle", () => {
|
2023-02-22 16:28:12 +05:30
|
|
|
const element = API.createElement({
|
2023-02-21 12:36:43 +05:30
|
|
|
type: "rectangle",
|
|
|
|
...params,
|
|
|
|
});
|
2023-02-22 16:28:12 +05:30
|
|
|
expect(getContainerCoords(element)).toEqual({
|
|
|
|
x: 15,
|
|
|
|
y: 25,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should compute coords correctly when diamond", () => {
|
|
|
|
const element = API.createElement({
|
|
|
|
type: "diamond",
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
expect(getContainerCoords(element)).toEqual({
|
|
|
|
x: 65,
|
|
|
|
y: 50,
|
2023-02-21 12:36:43 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test computeContainerHeightForBoundText", () => {
|
|
|
|
const params = {
|
|
|
|
width: 178,
|
|
|
|
height: 194,
|
|
|
|
};
|
2023-02-22 16:28:12 +05:30
|
|
|
|
2023-02-21 12:36:43 +05:30
|
|
|
it("should compute container height correctly for rectangle", () => {
|
|
|
|
const element = API.createElement({
|
|
|
|
type: "rectangle",
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
expect(computeContainerHeightForBoundText(element, 150)).toEqual(160);
|
|
|
|
});
|
2023-02-22 16:28:12 +05:30
|
|
|
|
2023-02-21 12:36:43 +05:30
|
|
|
it("should compute container height correctly for ellipse", () => {
|
|
|
|
const element = API.createElement({
|
|
|
|
type: "ellipse",
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
expect(computeContainerHeightForBoundText(element, 150)).toEqual(212);
|
|
|
|
});
|
2023-02-22 16:28:12 +05:30
|
|
|
|
|
|
|
it("should compute container height correctly for diamond", () => {
|
|
|
|
const element = API.createElement({
|
|
|
|
type: "diamond",
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
expect(computeContainerHeightForBoundText(element, 150)).toEqual(300);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test getMaxContainerWidth", () => {
|
|
|
|
const params = {
|
|
|
|
width: 178,
|
|
|
|
height: 194,
|
|
|
|
};
|
|
|
|
|
|
|
|
it("should return max width when container is rectangle", () => {
|
|
|
|
const container = API.createElement({ type: "rectangle", ...params });
|
|
|
|
expect(getMaxContainerWidth(container)).toBe(168);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return max width when container is ellipse", () => {
|
|
|
|
const container = API.createElement({ type: "ellipse", ...params });
|
|
|
|
expect(getMaxContainerWidth(container)).toBe(116);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return max width when container is diamond", () => {
|
|
|
|
const container = API.createElement({ type: "diamond", ...params });
|
|
|
|
expect(getMaxContainerWidth(container)).toBe(79);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test getMaxContainerHeight", () => {
|
|
|
|
const params = {
|
|
|
|
width: 178,
|
|
|
|
height: 194,
|
|
|
|
};
|
|
|
|
|
|
|
|
it("should return max height when container is rectangle", () => {
|
|
|
|
const container = API.createElement({ type: "rectangle", ...params });
|
|
|
|
expect(getMaxContainerHeight(container)).toBe(184);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return max height when container is ellipse", () => {
|
|
|
|
const container = API.createElement({ type: "ellipse", ...params });
|
|
|
|
expect(getMaxContainerHeight(container)).toBe(127);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return max height when container is diamond", () => {
|
|
|
|
const container = API.createElement({ type: "diamond", ...params });
|
|
|
|
expect(getMaxContainerHeight(container)).toBe(87);
|
|
|
|
});
|
2023-02-21 12:36:43 +05:30
|
|
|
});
|
2022-12-21 12:32:43 +05:30
|
|
|
});
|