2023-05-24 23:35:44 +00:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
2023-03-10 22:10:56 +00:00
|
|
|
import { truncate } from "./text";
|
|
|
|
|
|
|
|
describe("Text util tests", () => {
|
|
|
|
describe("fn: truncate", () => {
|
|
|
|
it("should return the original text when it is shorter than the max length", () => {
|
|
|
|
const cases = [
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 100,
|
|
|
|
expected: "Hello world",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 11,
|
|
|
|
expected: "Hello world",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const { input, maxLength, expected } of cases) {
|
|
|
|
const result = truncate(input, maxLength);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the truncated text when it is longer than the max length", () => {
|
|
|
|
const cases = [
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 10,
|
|
|
|
expected: "Hello w...",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 5,
|
|
|
|
expected: "He...",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const { input, maxLength, expected } of cases) {
|
|
|
|
const result = truncate(input, maxLength);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the truncated text without ellipsis when it is longer than the max length and ellipsis is false", () => {
|
|
|
|
const cases = [
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 10,
|
|
|
|
ellipsis: false,
|
|
|
|
expected: "Hello w",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "Hello world",
|
|
|
|
maxLength: 5,
|
|
|
|
ellipsis: false,
|
|
|
|
expected: "He",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const { input, maxLength, ellipsis, expected } of cases) {
|
|
|
|
const result = truncate(input, maxLength, ellipsis);
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|