-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdraggable-window.test.tsx
More file actions
156 lines (130 loc) · 4.44 KB
/
draggable-window.test.tsx
File metadata and controls
156 lines (130 loc) · 4.44 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
import { fireEvent, render, waitFor } from "@testing-library/react";
import { act } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DraggableWindow } from "@/components/draggable-window";
import { useWindowsStore } from "@/store/windows.store";
const resizeObserverCallbacks: Array<() => void> = [];
class ResizeObserverMock {
constructor(private readonly callback: () => void) {
resizeObserverCallbacks.push(callback);
}
observe() {}
disconnect() {}
}
vi.stubGlobal("ResizeObserver", ResizeObserverMock);
describe("DraggableWindow", () => {
beforeEach(() => {
resizeObserverCallbacks.length = 0;
useWindowsStore.setState((state) => ({
...state,
notifications: {
...state.notifications,
position: { x: 0, y: 0 },
size: { width: 360, height: 300 },
},
windowFocusHistory: [],
}));
});
it("caps auto height using the provided content limit", async () => {
const { container } = render(
<DraggableWindow
id="notifications"
title="Powiadomienia"
resizable={false}
minWidth={242}
minHeight={88}
heightMode="auto-up-to-max"
maxContentHeight={80}
>
<div>Treść</div>
</DraggableWindow>,
);
const windowElement = container.querySelector("#ll-notifications");
if (!(windowElement instanceof HTMLDivElement)) {
throw new Error("Expected draggable window root");
}
const windowBody = windowElement.firstElementChild;
const contentElement = windowBody?.querySelector(".ll\\:flex-1");
if (!(windowBody instanceof HTMLDivElement)) {
throw new Error("Expected draggable window body");
}
if (!(contentElement instanceof HTMLDivElement)) {
throw new Error("Expected draggable window content");
}
Object.defineProperty(windowBody, "offsetHeight", {
configurable: true,
value: 150,
});
Object.defineProperty(contentElement, "clientHeight", {
configurable: true,
value: 100,
});
Object.defineProperty(contentElement, "scrollHeight", {
configurable: true,
value: 160,
});
await act(async () => {
resizeObserverCallbacks.forEach((callback) => callback());
});
await waitFor(() => {
expect(windowElement.style.height).toBe("130px");
});
});
it("uses the resize handle to update max content height when armed", async () => {
const handleMaxContentHeightChange = vi.fn();
const handleArmedChange = vi.fn();
const { container } = render(
<DraggableWindow
id="notifications"
title="Powiadomienia"
resizable={false}
minWidth={242}
minHeight={88}
heightMode="auto-up-to-max"
maxContentHeight={80}
isMaxHeightAdjustmentArmed
onMaxContentHeightChange={handleMaxContentHeightChange}
onMaxHeightAdjustmentArmedChange={handleArmedChange}
>
<div>Treść</div>
</DraggableWindow>,
);
const windowElement = container.querySelector("#ll-notifications");
if (!(windowElement instanceof HTMLDivElement)) {
throw new Error("Expected draggable window root");
}
const windowBody = windowElement.firstElementChild;
const contentElement = windowBody?.querySelector(".ll\\:flex-1");
const resizeHandle = container.querySelector(".ll\\:cursor-se-resize");
if (!(windowBody instanceof HTMLDivElement)) {
throw new Error("Expected draggable window body");
}
if (!(contentElement instanceof HTMLDivElement)) {
throw new Error("Expected draggable window content");
}
if (!(resizeHandle instanceof HTMLDivElement)) {
throw new Error("Expected resize handle");
}
Object.defineProperty(windowElement, "offsetWidth", {
configurable: true,
value: 360,
});
Object.defineProperty(windowElement, "offsetHeight", {
configurable: true,
value: 130,
});
Object.defineProperty(windowBody, "offsetHeight", {
configurable: true,
value: 130,
});
Object.defineProperty(contentElement, "clientHeight", {
configurable: true,
value: 100,
});
fireEvent.mouseDown(resizeHandle, { clientX: 100, clientY: 100 });
fireEvent.mouseMove(document, { buttons: 1, clientX: 100, clientY: 140 });
fireEvent.mouseUp(document);
expect(handleMaxContentHeightChange).toHaveBeenCalledWith(140);
expect(handleArmedChange).toHaveBeenCalledWith(false);
});
});