summaryrefslogtreecommitdiffhomepage
path: root/src/app/App.test.ts
blob: ce37586567b3a4aa3c975acc1427700c475c264f (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import type { SurfaceServerMessage } from "@dispatch/ui-contract";
import { render, screen } from "@testing-library/svelte";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import type { WebSocketLike } from "../adapters/ws";
import App from "./App.svelte";
import { createAppStore } from "./store.svelte";

interface FakeSocket extends WebSocketLike {
	sent: string[];
	resolveOpen(): void;
	feedMessage(data: SurfaceServerMessage): void;
}

function fakeSocket(): FakeSocket {
	let onopen: (() => void) | null = null;
	let onmessage: ((ev: { data: string }) => void) | null = null;
	const sent: string[] = [];

	const ws: FakeSocket = {
		send(data: string) {
			sent.push(data);
		},
		close() {},
		get onopen() {
			return onopen;
		},
		set onopen(fn) {
			onopen = fn;
		},
		get onmessage() {
			return onmessage;
		},
		set onmessage(fn) {
			onmessage = fn;
		},
		get onclose() {
			return null;
		},
		set onclose(_fn) {},
		resolveOpen() {
			onopen?.();
		},
		feedMessage(msg: SurfaceServerMessage) {
			onmessage?.({ data: JSON.stringify(msg) });
		},
		sent,
	};
	return ws;
}

function sentMessages(ws: FakeSocket) {
	return ws.sent.map((s) => JSON.parse(s));
}

describe("App component interaction tests", () => {
	it("renders empty state when catalog is empty", () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		render(App, { props: { store } });

		expect(screen.getByText("No surfaces available")).toBeInTheDocument();

		store.dispose();
	});

	it("renders a catalog button per entry after a catalog message", () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "catalog",
			catalog: [
				{ id: "s1", region: "sidebar", title: "Surface One" },
				{ id: "s2", region: "panel", title: "Surface Two" },
			],
		});

		render(App, { props: { store } });

		const buttons = screen.getAllByRole("button");
		expect(buttons).toHaveLength(2);
		expect(buttons[0]).toHaveTextContent("Surface One");
		expect(buttons[1]).toHaveTextContent("Surface Two");

		store.dispose();
	});

	it("clicking a catalog entry subscribes and renders its surface", async () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "catalog",
			catalog: [{ id: "s1", region: "sidebar", title: "Surface One" }],
		});

		render(App, { props: { store } });

		const user = userEvent.setup();
		const button = screen.getByRole("button", { name: /Surface One/ });
		ws.sent.length = 0;
		await user.click(button);

		const msgs = sentMessages(ws);
		const subscribe = msgs.find(
			(m: { type: string; surfaceId: string }) => m.type === "subscribe" && m.surfaceId === "s1",
		);
		expect(subscribe).toBeTruthy();

		ws.feedMessage({
			type: "surface",
			spec: {
				id: "s1",
				region: "sidebar",
				title: "Surface One",
				fields: [{ kind: "stat", label: "Tokens", value: "1,234" }],
			},
		});

		expect(await screen.findByRole("heading", { name: "Surface One" })).toBeInTheDocument();
		expect(await screen.findByText("Tokens")).toBeInTheDocument();
		expect(await screen.findByText("1,234")).toBeInTheDocument();

		store.dispose();
	});

	it("clicking a different entry unsubscribes the previous then subscribes the new", async () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "catalog",
			catalog: [
				{ id: "s1", region: "sidebar", title: "Surface One" },
				{ id: "s2", region: "panel", title: "Surface Two" },
			],
		});

		render(App, { props: { store } });

		const user = userEvent.setup();
		await user.click(screen.getByRole("button", { name: /Surface One/ }));
		ws.sent.length = 0;

		await user.click(screen.getByRole("button", { name: /Surface Two/ }));

		const msgs = sentMessages(ws) as Array<{ type: string; surfaceId: string }>;
		const unsubIdx = msgs.findIndex((m) => m.type === "unsubscribe" && m.surfaceId === "s1");
		const subIdx = msgs.findIndex((m) => m.type === "subscribe" && m.surfaceId === "s2");
		expect(unsubIdx).toBeGreaterThanOrEqual(0);
		expect(subIdx).toBeGreaterThanOrEqual(0);
		expect(unsubIdx).toBeLessThan(subIdx);

		store.dispose();
	});

	it("selected catalog button reflects aria-current", async () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "catalog",
			catalog: [
				{ id: "s1", region: "sidebar", title: "Surface One" },
				{ id: "s2", region: "panel", title: "Surface Two" },
			],
		});

		render(App, { props: { store } });

		const user = userEvent.setup();
		const btn1 = screen.getByRole("button", { name: /Surface One/ });
		const btn2 = screen.getByRole("button", { name: /Surface Two/ });

		await user.click(btn1);
		expect(btn1).toHaveAttribute("aria-current", "true");
		expect(btn2).not.toHaveAttribute("aria-current");

		await user.click(btn2);
		expect(btn2).toHaveAttribute("aria-current", "true");
		expect(btn1).not.toHaveAttribute("aria-current");

		store.dispose();
	});

	it("an error message renders the alert banner", () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "error",
			message: "Something went wrong",
		});

		render(App, { props: { store } });

		const alert = screen.getByRole("alert");
		expect(alert).toHaveTextContent("Something went wrong");

		store.dispose();
	});

	it("invoking a field action sends an invoke", async () => {
		const ws = fakeSocket();
		const store = createAppStore({ socketFactory: () => ws });
		ws.resolveOpen();

		ws.feedMessage({
			type: "catalog",
			catalog: [{ id: "s1", region: "sidebar", title: "Surface One" }],
		});

		render(App, { props: { store } });

		const user = userEvent.setup();
		await user.click(screen.getByRole("button", { name: /Surface One/ }));

		ws.feedMessage({
			type: "surface",
			spec: {
				id: "s1",
				region: "sidebar",
				title: "Surface One",
				fields: [
					{
						kind: "toggle",
						label: "Dark Mode",
						value: false,
						action: { actionId: "toggle-dark" },
					},
				],
			},
		});

		ws.sent.length = 0;
		const checkbox = await screen.findByRole("checkbox", { name: "Dark Mode" });
		await user.click(checkbox);

		const msgs = sentMessages(ws);
		const invoke = msgs.find(
			(m: { type: string; surfaceId: string; actionId: string; payload: unknown }) =>
				m.type === "invoke" &&
				m.surfaceId === "s1" &&
				m.actionId === "toggle-dark" &&
				m.payload === true,
		);
		expect(invoke).toBeTruthy();

		store.dispose();
	});
});