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
|
import { describe, it, beforeEach, afterEach } from "node:test";
import assert from "node:assert";
import "../test-setup.ts";
import { createRoot, type Root } from "react-dom/client";
import { act } from "@testing-library/react";
import { ModelControls } from "./ModelControls.tsx";
import { useStore } from "../store/store.ts";
describe("ModelControls", () => {
let container: HTMLDivElement;
let root: Root;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
useStore.setState({
models: [],
selectedModelId: null,
cameraView: "perspective",
backdropUrl: null,
availableModels: [
{ id: "servbot", name: "Servbot", url: "/models/servbot.gltf" },
],
});
});
afterEach(() => {
act(() => {
root.unmount();
});
container.remove();
});
it("should not render when no model is selected", async () => {
await act(async () => {
root.render(<ModelControls />);
});
const controls = container.querySelector(".model-controls");
assert.ok(
!controls,
"Should not render controls when no model is selected",
);
});
it("should render when a model is selected", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const controls = container.querySelector(".model-controls");
assert.ok(controls, "Should render controls when model is selected");
});
it("should have position controls", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const positionXInput = container.querySelector('input[name="position-x"]');
const positionYInput = container.querySelector('input[name="position-y"]');
const positionZInput = container.querySelector('input[name="position-z"]');
assert.ok(positionXInput, "Should have position X input");
assert.ok(positionYInput, "Should have position Y input");
assert.ok(positionZInput, "Should have position Z input");
});
it("should have rotation controls", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const rotationXInput = container.querySelector('input[name="rotation-x"]');
const rotationYInput = container.querySelector('input[name="rotation-y"]');
const rotationZInput = container.querySelector('input[name="rotation-z"]');
assert.ok(rotationXInput, "Should have rotation X input");
assert.ok(rotationYInput, "Should have rotation Y input");
assert.ok(rotationZInput, "Should have rotation Z input");
});
it("should update position when input changes", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const positionXInput = container.querySelector(
'input[name="position-x"]',
) as HTMLInputElement;
await act(async () => {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value",
)!.set!;
nativeInputValueSetter.call(positionXInput, "5");
positionXInput.dispatchEvent(new Event("input", { bubbles: true }));
});
const model = useStore.getState().models[0];
assert.strictEqual(model.position.x, 5);
});
it("should have hue shift controls", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const hueRedInput = container.querySelector('input[name="hue-red"]');
const hueGreenInput = container.querySelector('input[name="hue-green"]');
const hueBlueInput = container.querySelector('input[name="hue-blue"]');
assert.ok(hueRedInput, "Should have red hue shift input");
assert.ok(hueGreenInput, "Should have green hue shift input");
assert.ok(hueBlueInput, "Should have blue hue shift input");
});
it("should update hue shift when slider changes", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const hueRedInput = container.querySelector(
'input[name="hue-red"]',
) as HTMLInputElement;
await act(async () => {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype,
"value",
)!.set!;
nativeInputValueSetter.call(hueRedInput, "0.5");
hueRedInput.dispatchEvent(new Event("input", { bubbles: true }));
});
const model = useStore.getState().models[0];
assert.strictEqual(model.hueShift.red, 0.5);
});
it("should have a delete button", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const deleteButton = container.querySelector("button.delete-model");
assert.ok(deleteButton, "Should have delete button");
});
it("should remove model when delete button is clicked", async () => {
useStore.getState().addModel("servbot", { x: 0, y: 0, z: 0 });
const modelId = useStore.getState().models[0].id;
useStore.getState().selectModel(modelId);
await act(async () => {
root.render(<ModelControls />);
});
const deleteButton = container.querySelector(
"button.delete-model",
) as HTMLButtonElement;
await act(async () => {
deleteButton.click();
});
const models = useStore.getState().models;
assert.strictEqual(models.length, 0, "Model should be removed");
assert.strictEqual(
useStore.getState().selectedModelId,
null,
"Selection should be cleared",
);
});
});
|