-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclearStyles.test.ts
More file actions
237 lines (185 loc) · 8.93 KB
/
clearStyles.test.ts
File metadata and controls
237 lines (185 loc) · 8.93 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { clearVisibilityStyles, restoreVisibilityStyles } from "../clearStyles";
describe("clearStyles", () => {
let element: HTMLDivElement;
beforeEach(() => {
element = document.createElement("div");
document.body.appendChild(element);
});
afterEach(() => {
document.body.removeChild(element);
});
describe("clearVisibilityStyles", () => {
it("should hide element and disable transitions/animations", () => {
// Set initial styles
element.style.visibility = "visible";
element.style.transition = "all 0.3s ease";
element.style.animation = "fadeIn 1s";
clearVisibilityStyles(element);
expect(element.style.visibility).toBe("hidden");
expect(element.style.transition).toBe("none");
expect(element.style.animation).toBe("none");
});
it("should handle element with no initial styles", () => {
clearVisibilityStyles(element);
expect(element.style.visibility).toBe("hidden");
expect(element.style.transition).toBe("none");
expect(element.style.animation).toBe("none");
});
it("should store original style values", () => {
// Set initial styles
element.style.visibility = "visible";
element.style.transition = "opacity 0.5s";
element.style.animation = "slideIn 2s";
clearVisibilityStyles(element);
// Verify styles are cleared
expect(element.style.visibility).toBe("hidden");
expect(element.style.transition).toBe("none");
expect(element.style.animation).toBe("none");
// Verify we can restore (this indirectly tests that values were stored)
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("visible");
expect(element.style.transition).toBe("opacity 0.5s");
expect(element.style.animation).toBe("slideIn 2s");
});
it("should handle multiple calls on the same element", () => {
element.style.visibility = "visible";
element.style.transition = "all 0.3s";
// First call
clearVisibilityStyles(element);
expect(element.style.visibility).toBe("hidden");
// Second call should not overwrite stored values
element.style.visibility = "hidden"; // Simulate current state
clearVisibilityStyles(element);
// Restore should still work with original values
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("visible");
expect(element.style.transition).toBe("all 0.3s");
});
it("should handle empty string style values", () => {
element.style.visibility = "";
element.style.transition = "";
element.style.animation = "";
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("");
expect(element.style.transition).toBe("");
expect(element.style.animation).toBe("");
});
});
describe("restoreVisibilityStyles", () => {
it("should restore original styles after clearing", () => {
const originalVisibility = "visible";
const originalTransition = "all 0.3s ease-in-out";
const originalAnimation = "bounce 1s infinite";
element.style.visibility = originalVisibility;
element.style.transition = originalTransition;
element.style.animation = originalAnimation;
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe(originalVisibility);
expect(element.style.transition).toBe(originalTransition);
expect(element.style.animation).toBe(originalAnimation);
});
it("should handle restore without prior clear (no stored values)", () => {
element.style.visibility = "visible";
element.style.transition = "all 0.3s";
// Call restore without clearing first
restoreVisibilityStyles(element);
// Should not modify styles when no stored values exist
expect(element.style.visibility).toBe("visible");
expect(element.style.transition).toBe("all 0.3s");
});
it("should remove stored values after restoration", () => {
element.style.visibility = "visible";
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
// Second restore call should not affect styles
element.style.visibility = "hidden";
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("hidden"); // Should remain unchanged
});
it("should restore empty string values correctly", () => {
// Start with empty styles (which is the default)
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("");
expect(element.style.transition).toBe("");
expect(element.style.animation).toBe("");
});
it("should handle partial style restoration", () => {
element.style.visibility = "visible";
element.style.transition = "opacity 0.5s";
// animation is not set (empty)
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("visible");
expect(element.style.transition).toBe("opacity 0.5s");
expect(element.style.animation).toBe("");
});
});
describe("WeakMap storage behavior", () => {
it("should handle multiple elements independently", () => {
const element1 = document.createElement("div");
const element2 = document.createElement("div");
document.body.appendChild(element1);
document.body.appendChild(element2);
element1.style.visibility = "visible";
element1.style.transition = "all 0.3s";
element2.style.visibility = "hidden";
element2.style.animation = "fadeIn 1s";
clearVisibilityStyles(element1);
clearVisibilityStyles(element2);
restoreVisibilityStyles(element1);
restoreVisibilityStyles(element2);
expect(element1.style.visibility).toBe("visible");
expect(element1.style.transition).toBe("all 0.3s");
expect(element1.style.animation).toBe(""); // Was not set originally
expect(element2.style.visibility).toBe("hidden");
expect(element2.style.transition).toBe(""); // Was not set originally
expect(element2.style.animation).toBe("fadeIn 1s");
document.body.removeChild(element1);
document.body.removeChild(element2);
});
it("should not leak memory after element removal", () => {
const tempElement = document.createElement("div");
tempElement.style.visibility = "visible";
clearVisibilityStyles(tempElement);
// Element is not in DOM and has no references
// WeakMap should allow garbage collection
// This test mainly documents the expected behavior
expect(tempElement.style.visibility).toBe("hidden");
});
});
describe("CSS property handling edge cases", () => {
it("should handle complex transition values", () => {
element.style.transition =
"opacity 0.3s ease-in-out, transform 0.5s linear";
clearVisibilityStyles(element);
expect(element.style.transition).toBe("none");
restoreVisibilityStyles(element);
expect(element.style.transition).toBe(
"opacity 0.3s ease-in-out, transform 0.5s linear"
);
});
it("should handle complex animation values", () => {
element.style.animation =
"slideIn 0.5s ease-out, fadeIn 1s linear infinite";
clearVisibilityStyles(element);
expect(element.style.animation).toBe("none");
restoreVisibilityStyles(element);
expect(element.style.animation).toBe(
"slideIn 0.5s ease-out, fadeIn 1s linear infinite"
);
});
it("should handle inherit and initial values", () => {
element.style.visibility = "inherit";
element.style.transition = "initial";
element.style.animation = "inherit";
clearVisibilityStyles(element);
restoreVisibilityStyles(element);
expect(element.style.visibility).toBe("inherit");
expect(element.style.transition).toBe("initial");
expect(element.style.animation).toBe("inherit");
});
});
});