|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 |
|
3 | | -import { truncate } from "./text"; |
| 3 | +import { truncate, truncateOnWord } from "./text"; |
4 | 4 |
|
5 | 5 | describe("Text util tests", () => { |
6 | 6 | describe("fn: truncate", () => { |
@@ -69,4 +69,84 @@ describe("Text util tests", () => { |
69 | 69 | } |
70 | 70 | }); |
71 | 71 | }); |
| 72 | + describe("fn: truncateOnWord", () => { |
| 73 | + it("should return the original text when it is shorter than the max length", () => { |
| 74 | + const cases = [ |
| 75 | + { |
| 76 | + input: "Hello world", |
| 77 | + maxLength: 100, |
| 78 | + expected: "Hello world", |
| 79 | + }, |
| 80 | + { |
| 81 | + input: "Hello world", |
| 82 | + maxLength: 11, |
| 83 | + expected: "Hello world", |
| 84 | + }, |
| 85 | + ]; |
| 86 | + |
| 87 | + for (const { input, maxLength, expected } of cases) { |
| 88 | + const result = truncateOnWord(input, maxLength); |
| 89 | + |
| 90 | + expect(result).toEqual(expected); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it("should return the truncated text on the last word when it is longer than the max length", () => { |
| 95 | + const cases = [ |
| 96 | + { |
| 97 | + input: "The quick brown fox jumps over the lazy dog", |
| 98 | + maxLength: 12, |
| 99 | + expected: "The quick...", |
| 100 | + }, |
| 101 | + { |
| 102 | + input: "Cal.com is the scheduling infrastructure for everyone", |
| 103 | + maxLength: 14, |
| 104 | + expected: "Cal.com is...", |
| 105 | + }, |
| 106 | + ]; |
| 107 | + |
| 108 | + for (const { input, maxLength, expected } of cases) { |
| 109 | + const result = truncateOnWord(input, maxLength); |
| 110 | + |
| 111 | + expect(result).toEqual(expected); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it("should return the truncated text without ellipsis when it is longer than the max length and ellipsis is false", () => { |
| 116 | + const cases = [ |
| 117 | + { |
| 118 | + input: "The quick brown fox jumps over the lazy dog", |
| 119 | + maxLength: 12, |
| 120 | + ellipsis: false, |
| 121 | + expected: "The quick", |
| 122 | + }, |
| 123 | + ]; |
| 124 | + |
| 125 | + for (const { input, maxLength, ellipsis, expected } of cases) { |
| 126 | + const result = truncateOnWord(input, maxLength, ellipsis); |
| 127 | + |
| 128 | + expect(result).toEqual(expected); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + it("should fallback to character truncation when no spaces are present in the truncated segment", () => { |
| 133 | + const cases = [ |
| 134 | + { |
| 135 | + input: "supercalifragilisticexpialidocious", |
| 136 | + maxLength: 10, |
| 137 | + expected: "supercalif...", |
| 138 | + }, |
| 139 | + { |
| 140 | + input: "https://cal.com/pro/30min/extremely-long-url-without-any-spaces", |
| 141 | + maxLength: 20, |
| 142 | + expected: "https://cal.com/pro/...", |
| 143 | + }, |
| 144 | + ]; |
| 145 | + |
| 146 | + for (const { input, maxLength, expected } of cases) { |
| 147 | + const result = truncateOnWord(input, maxLength); |
| 148 | + expect(result).toEqual(expected); |
| 149 | + } |
| 150 | + }); |
| 151 | + }); |
72 | 152 | }); |
0 commit comments