-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTruncatingTitle.test.tsx
More file actions
179 lines (151 loc) · 6.62 KB
/
Copy pathTruncatingTitle.test.tsx
File metadata and controls
179 lines (151 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
import { render, screen } from "@testing-library/react"
import { TruncatingTitle } from "./TruncatingTitle.js"
// ── ResizeObserver mock ───────────────────────────────────────────────────────
// Arrow functions cannot be constructors — use a class so `new ResizeObserver()` works.
const mockObserve = vi.fn()
const mockDisconnect = vi.fn()
beforeEach(() => {
vi.stubGlobal("ResizeObserver", class {
observe = mockObserve
disconnect = mockDisconnect
})
})
afterEach(() => {
vi.unstubAllGlobals()
vi.clearAllMocks()
})
// ── Overflow predicate helpers ────────────────────────────────────────────────
/** Overflow predicate: overflows while the candidate text contains `needle`. */
const overflowsWhile = (needle: string) => (el: HTMLSpanElement) =>
Boolean(el.textContent?.includes(needle))
// ── Tests ─────────────────────────────────────────────────────────────────────
describe("TruncatingTitle", () => {
// ── No-overflow cases ───────────────────────────────────────────────────────
it("renders all items when none overflow", () => {
render(<TruncatingTitle prefix="Health" items={["publish", "scan", "deploy"]} />)
expect(screen.getByText("Health: publish, scan, deploy")).toBeInTheDocument()
})
it("renders a single item without a +N suffix", () => {
render(<TruncatingTitle prefix="Health" items={["publish"]} />)
expect(screen.getByText("Health: publish")).toBeInTheDocument()
expect(screen.queryByText(/more/)).not.toBeInTheDocument()
})
it("applies the className to the span", () => {
const { container } = render(
<TruncatingTitle prefix="H" items={["a"]} className="card-title" />
)
expect(container.querySelector(".card-title")).toBeInTheDocument()
})
it("does not show +N suffix when the explicit predicate returns false", () => {
render(
<TruncatingTitle
prefix="Health"
items={["publish", "scan"]}
_isOverflowing={() => false}
/>
)
expect(screen.queryByText(/more/)).not.toBeInTheDocument()
expect(screen.getByText("Health: publish, scan")).toBeInTheDocument()
})
// ── Overflow reduction (single-pass, synchronous) ───────────────────────────
it("reduces to 1 item + +N more when always overflowing", () => {
render(
<TruncatingTitle
prefix="Health"
items={["publish", "scan", "deploy"]}
_isOverflowing={() => true}
/>
)
expect(screen.getByText("Health: publish +2 more")).toBeInTheDocument()
})
it("stops reducing when overflow is resolved", () => {
// Overflowing while the full third-item string ", deploy" is present
render(
<TruncatingTitle
prefix="Health"
items={["publish", "scan", "deploy"]}
_isOverflowing={overflowsWhile(", deploy")}
/>
)
// "Health: publish, scan +1 more" — no longer contains ", deploy" → stops
expect(screen.getByText("Health: publish, scan +1 more")).toBeInTheDocument()
})
it("shows the correct +N count", () => {
render(
<TruncatingTitle
prefix="X"
items={["a", "b", "c", "d", "e"]}
_isOverflowing={() => true}
/>
)
expect(screen.getByText("X: a +4 more")).toBeInTheDocument()
})
it("stops at 1 item minimum and does not show +0 suffix", () => {
// With a single item, there is nothing to truncate further;
// the while loop never executes and we end up showing the 1 item without suffix.
render(
<TruncatingTitle
prefix="X"
items={["only"]}
_isOverflowing={() => true}
/>
)
expect(screen.getByText("X: only")).toBeInTheDocument()
expect(screen.queryByText(/more/)).not.toBeInTheDocument()
})
// ── Items change ────────────────────────────────────────────────────────────
it("expands back to full when items change and new set fits", () => {
const { rerender } = render(
<TruncatingTitle
prefix="Health"
items={["publish", "scan"]}
_isOverflowing={() => true}
/>
)
expect(screen.getByText("Health: publish +1 more")).toBeInTheDocument()
rerender(
<TruncatingTitle
prefix="Health"
items={["alpha", "beta"]}
_isOverflowing={() => false}
/>
)
expect(screen.getByText("Health: alpha, beta")).toBeInTheDocument()
})
it("re-applies reduction after items change when still overflowing", () => {
const { rerender } = render(
<TruncatingTitle prefix="X" items={["a"]} _isOverflowing={() => false} />
)
expect(screen.getByText("X: a")).toBeInTheDocument()
rerender(
<TruncatingTitle prefix="X" items={["a", "b", "c"]} _isOverflowing={() => true} />
)
expect(screen.getByText("X: a +2 more")).toBeInTheDocument()
})
// ── Prefix-less mode ───────────────────────────────────────────────────────
it("renders items without a prefix or colon when prefix is omitted", () => {
render(<TruncatingTitle items={["publish", "scan"]} />)
expect(screen.getByText("publish, scan")).toBeInTheDocument()
expect(screen.queryByText(/:/)).not.toBeInTheDocument()
})
it("truncates correctly with no prefix", () => {
render(
<TruncatingTitle
items={["publish", "scan", "deploy"]}
_isOverflowing={() => true}
/>
)
expect(screen.getByText("publish +2 more")).toBeInTheDocument()
})
// ── ResizeObserver lifecycle ────────────────────────────────────────────────
it("sets up a ResizeObserver on mount", () => {
render(<TruncatingTitle prefix="H" items={["a"]} />)
expect(mockObserve).toHaveBeenCalled()
})
it("disconnects the ResizeObserver on unmount", () => {
const { unmount } = render(<TruncatingTitle prefix="H" items={["a"]} />)
unmount()
expect(mockDisconnect).toHaveBeenCalled()
})
})