aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/pages/StudyPage.test.tsx
blob: 1fa6e711bb15f59b5ca2df24f521bc46a480b77f (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/**
 * @vitest-environment jsdom
 */
import { QueryClient } from "@tanstack/query-core";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { createStore, Provider } from "jotai";
import { queryClientAtom } from "jotai-tanstack-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Route, Router } from "wouter";
import { memoryLocation } from "wouter/memory-location";
import {
	authLoadingAtom,
	isOnlineAtom,
	type StudyCard,
	type StudyData,
} from "../atoms";
import type { LocalCard } from "../db";
import { StudyPage } from "./StudyPage";

interface RenderOptions {
	path?: string;
	initialStudyData?: StudyData;
	online?: boolean;
}

const mockSubmitReview = vi.fn();
const mockUndoReview = vi.fn();
const mockTriggerSync = vi.fn();

vi.mock(import("../sync"), async (importOriginal) => {
	const actual = await importOriginal();
	return {
		...actual,
		submitReviewLocal: (args: Parameters<typeof actual.submitReviewLocal>[0]) =>
			mockSubmitReview(args),
		undoReviewLocal: (args: Parameters<typeof actual.undoReviewLocal>[0]) =>
			mockUndoReview(args),
	};
});

vi.mock(import("../atoms"), async (importOriginal) => {
	const actual = await importOriginal();
	const { atom } = await import("jotai");
	const stubSync = atom(null, async () => {
		mockTriggerSync();
		return {
			success: true,
			pushResult: null,
			pullResult: null,
			conflictsResolved: 0,
			crdtDocumentsStored: 0,
		};
	});
	return {
		...actual,
		syncActionAtom: stubSync as unknown as typeof actual.syncActionAtom,
	};
});

const mockEditNoteModalOnClose = vi.fn();
const mockEditNoteModalOnNoteUpdated = vi.fn();

vi.mock("../components/EditNoteModal", () => ({
	EditNoteModal: ({
		isOpen,
		deckId,
		noteId,
		onClose,
		onNoteUpdated,
	}: {
		isOpen: boolean;
		deckId: string;
		noteId: string | null;
		onClose: () => void;
		onNoteUpdated: () => void;
	}) => {
		mockEditNoteModalOnClose.mockImplementation(onClose);
		mockEditNoteModalOnNoteUpdated.mockImplementation(onNoteUpdated);

		if (!isOpen) return null;
		return (
			<div
				data-testid="edit-note-modal"
				data-deck-id={deckId}
				data-note-id={noteId}
			>
				<button type="button" data-testid="edit-modal-close" onClick={onClose}>
					Cancel
				</button>
				<button
					type="button"
					data-testid="edit-modal-save"
					onClick={onNoteUpdated}
				>
					Save Changes
				</button>
			</div>
		);
	},
}));

vi.mock("../api/client", () => ({
	apiClient: {
		login: vi.fn(),
		logout: vi.fn(),
		getTokens: vi.fn(),
		getAuthHeader: vi.fn(),
		onSessionExpired: vi.fn(() => vi.fn()),
		rpc: { api: { decks: {} } },
		handleResponse: vi.fn(),
	},
	ApiClientError: class ApiClientError extends Error {
		constructor(
			message: string,
			public status: number,
			public code?: string,
		) {
			super(message);
			this.name = "ApiClientError";
		}
	},
}));

let testQueryClient: QueryClient;

const mockDeck = {
	id: "deck-1",
	name: "Japanese Vocabulary",
};

function makeStudyCard(overrides: Partial<StudyCard>): StudyCard {
	return {
		id: "card-1",
		deckId: "deck-1",
		noteId: "note-1",
		isReversed: false,
		state: 0,
		noteType: { frontTemplate: "{{Front}}", backTemplate: "{{Back}}" },
		fieldValuesMap: { Front: "Hello", Back: "こんにちは" },
		...overrides,
	};
}

const mockFirstCard = makeStudyCard({});
const mockSecondCard = makeStudyCard({
	id: "card-2",
	noteId: "note-2",
	fieldValuesMap: { Front: "Goodbye", Back: "さようなら" },
});
const mockThirdCard = makeStudyCard({
	id: "card-3",
	noteId: "note-3",
	fieldValuesMap: { Front: "Thank you", Back: "ありがとう" },
});

const mockDueCards: StudyCard[] = [mockFirstCard, mockSecondCard];

function makeLocalCard(id: string): LocalCard {
	return {
		id,
		deckId: "deck-1",
		noteId: `note-${id}`,
		isReversed: false,
		front: "",
		back: "",
		state: 0,
		due: new Date("2024-01-01T00:00:00Z"),
		stability: 0,
		difficulty: 0,
		elapsedDays: 0,
		scheduledDays: 0,
		reps: 0,
		lapses: 0,
		lastReview: null,
		createdAt: new Date("2024-01-01T00:00:00Z"),
		updatedAt: new Date("2024-01-01T00:00:00Z"),
		deletedAt: null,
		syncVersion: 0,
		_synced: true,
	};
}

function renderWithProviders({
	path = "/decks/deck-1/study",
	initialStudyData,
	online = true,
}: RenderOptions = {}) {
	const { hook } = memoryLocation({ path, static: true });
	const store = createStore();
	store.set(authLoadingAtom, false);
	store.set(queryClientAtom, testQueryClient);
	store.set(isOnlineAtom, online);

	const deckIdMatch = path.match(/\/decks\/([^/]+)/);
	const deckId = deckIdMatch?.[1] ?? "deck-1";

	if (initialStudyData !== undefined) {
		testQueryClient.setQueryData(["decks", deckId, "study"], initialStudyData);
	}

	return render(
		<Provider store={store}>
			<Router hook={hook}>
				<Route path="/decks/:deckId/study">
					<StudyPage />
				</Route>
			</Router>
		</Provider>,
	);
}

describe("StudyPage", () => {
	beforeEach(() => {
		vi.clearAllMocks();
		testQueryClient = new QueryClient({
			defaultOptions: {
				queries: { staleTime: Number.POSITIVE_INFINITY, retry: false },
			},
		});

		mockSubmitReview.mockImplementation(
			async ({ cardId }: { cardId: string }) => ({
				card: makeLocalCard(cardId),
				prevCard: makeLocalCard(cardId),
				reviewLogId: `log-${cardId}`,
			}),
		);
		mockUndoReview.mockResolvedValue(undefined);
		mockTriggerSync.mockResolvedValue(undefined);
	});

	afterEach(() => {
		cleanup();
		vi.restoreAllMocks();
		testQueryClient.clear();
	});

	describe("Loading and Initial State", () => {
		it("renders deck name and back link", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(
				screen.getByRole("heading", { name: /Japanese Vocabulary/ }),
			).toBeDefined();
			expect(screen.getByText(/Back to Deck/)).toBeDefined();
		});
	});

	describe("No Cards State", () => {
		it("shows no cards message when deck has no due cards", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: [] },
			});

			expect(screen.getByTestId("no-cards")).toBeDefined();
			expect(screen.getByText("All caught up!")).toBeDefined();
		});
	});

	describe("Card Display and Progress", () => {
		it("shows remaining cards count", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.getByTestId("remaining-count").textContent).toBe(
				"2 remaining",
			);
		});

		it("displays the front of the first card", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.getByTestId("card-front").textContent).toBe("Hello");
		});

		it("does not show rating buttons before card is flipped", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.queryByTestId("rating-buttons")).toBeNull();
		});
	});

	describe("Card Flip Interaction", () => {
		it("reveals answer when card is clicked", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));

			expect(screen.getByTestId("card-back").textContent).toBe("こんにちは");
		});

		it("shows rating buttons after card is flipped", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));

			expect(screen.getByTestId("rating-buttons")).toBeDefined();
			expect(screen.getByTestId("rating-1")).toBeDefined();
			expect(screen.getByTestId("rating-4")).toBeDefined();
		});

		it("displays rating labels on buttons", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));

			expect(screen.getByTestId("rating-1").textContent).toContain("Again");
			expect(screen.getByTestId("rating-2").textContent).toContain("Hard");
			expect(screen.getByTestId("rating-3").textContent).toContain("Good");
			expect(screen.getByTestId("rating-4").textContent).toContain("Easy");
		});
	});

	describe("Rating Submission", () => {
		it("submits review locally and advances to next card", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			expect(mockSubmitReview).toHaveBeenCalledTimes(1);
			expect(mockSubmitReview).toHaveBeenCalledWith(
				expect.objectContaining({ cardId: "card-1", rating: 3 }),
			);
		});

		it("triggers sync when online", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
				online: true,
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(mockTriggerSync).toHaveBeenCalled();
			});
		});

		it("does not trigger sync when offline", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
				online: false,
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			expect(mockTriggerSync).not.toHaveBeenCalled();
		});

		it("still advances even when offline", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
				online: false,
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});
			expect(mockSubmitReview).toHaveBeenCalledTimes(1);
		});

		it("updates remaining count after review", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.getByTestId("remaining-count").textContent).toBe(
				"2 remaining",
			);

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("remaining-count").textContent).toBe(
					"1 remaining",
				);
			});
		});

		it("shows error when local review submission fails", async () => {
			const user = userEvent.setup();

			mockSubmitReview.mockRejectedValueOnce(
				new Error("Failed to submit review"),
			);

			renderWithProviders({
				initialStudyData: {
					deck: mockDeck,
					cards: [mockFirstCard, mockSecondCard, mockThirdCard],
				},
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByRole("alert").textContent).toContain(
					"Failed to submit review",
				);
			});

			// Failed review should not advance — count stays at 3.
			expect(screen.getByTestId("remaining-count").textContent).toBe(
				"3 remaining",
			);
		});
	});

	describe("Session Complete", () => {
		it("shows session complete screen after all cards reviewed", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: [mockFirstCard] },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("session-complete")).toBeDefined();
			});
			expect(screen.getByTestId("completed-count").textContent).toBe("1");
		});

		it("provides navigation links after session complete", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: [mockFirstCard] },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("session-complete")).toBeDefined();
			});

			expect(screen.getAllByText("Back to Deck").length).toBeGreaterThan(0);
			expect(screen.getByText("All Decks")).toBeDefined();
		});
	});

	describe("Keyboard Shortcuts", () => {
		it("flips card with Space key", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard(" ");

			expect(screen.getByTestId("card-back").textContent).toBe("こんにちは");
		});

		it("flips card with Enter key", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard("{Enter}");

			expect(screen.getByTestId("card-back").textContent).toBe("こんにちは");
		});

		it("rates card with number keys", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard(" ");
			await user.keyboard("3");

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			expect(mockSubmitReview).toHaveBeenCalledWith(
				expect.objectContaining({ rating: 3 }),
			);
		});

		it("rates card as Good with Space key when card is flipped", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard(" ");
			await user.keyboard(" ");

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			expect(mockSubmitReview).toHaveBeenCalledWith(
				expect.objectContaining({ rating: 3 }),
			);
		});
	});

	describe("Undo", () => {
		it("does not show undo button before any rating", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.queryByTestId("undo-button")).toBeNull();
		});

		it("shows undo button after rating (when not flipped)", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			expect(screen.getByTestId("undo-button")).toBeDefined();
		});

		it("undoes the last rating and returns to previous card", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			await user.click(screen.getByTestId("undo-button"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Hello");
			});

			expect(mockUndoReview).toHaveBeenCalledTimes(1);
			expect(screen.queryByTestId("undo-button")).toBeNull();
		});

		it("decrements completed count on undo", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: {
					deck: mockDeck,
					cards: [mockFirstCard, mockSecondCard, mockThirdCard],
				},
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});
			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-4"));

			await waitFor(() => {
				expect(screen.getByTestId("remaining-count").textContent).toBe(
					"1 remaining",
				);
			});

			await user.click(screen.getByTestId("undo-button"));

			await waitFor(() => {
				expect(screen.getByTestId("remaining-count").textContent).toBe(
					"2 remaining",
				);
			});
		});

		it("undoes with z key when card is not flipped", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			await user.keyboard("z");

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Hello");
			});
		});

		it("undoes with Ctrl+Z", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Goodbye");
			});

			await user.keyboard("{Control>}z{/Control}");

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Hello");
			});
		});

		it("shows undo button on session complete screen", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: [mockFirstCard] },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("session-complete")).toBeDefined();
			});

			expect(screen.getByTestId("undo-button")).toBeDefined();
		});

		it("undoes from session complete screen back to last card", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: [mockFirstCard] },
			});

			await user.click(screen.getByTestId("card-container"));
			await user.click(screen.getByTestId("rating-3"));

			await waitFor(() => {
				expect(screen.getByTestId("session-complete")).toBeDefined();
			});

			await user.click(screen.getByTestId("undo-button"));

			await waitFor(() => {
				expect(screen.getByTestId("card-front").textContent).toBe("Hello");
			});
			expect(screen.queryByTestId("session-complete")).toBeNull();
		});
	});

	describe("Edit Card", () => {
		it("shows edit button on card", () => {
			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			expect(screen.getByTestId("edit-card-button")).toBeDefined();
		});

		it("opens edit modal when edit button is clicked", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("edit-card-button"));

			expect(screen.getByTestId("edit-note-modal")).toBeDefined();
			expect(
				screen.getByTestId("edit-note-modal").getAttribute("data-note-id"),
			).toBe("note-1");
		});

		it("does not flip card when edit button is clicked", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("edit-card-button"));

			expect(screen.getByTestId("card-front")).toBeDefined();
			expect(screen.queryByTestId("card-back")).toBeNull();
		});

		it("closes edit modal when close button is clicked", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.click(screen.getByTestId("edit-card-button"));
			await user.click(screen.getByTestId("edit-modal-close"));

			expect(screen.queryByTestId("edit-note-modal")).toBeNull();
		});

		it("opens edit modal with E key", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard("e");

			expect(screen.getByTestId("edit-note-modal")).toBeDefined();
		});

		it("disables keyboard shortcuts while edit modal is open", async () => {
			const user = userEvent.setup();

			renderWithProviders({
				initialStudyData: { deck: mockDeck, cards: mockDueCards },
			});

			await user.keyboard("e");
			expect(screen.getByTestId("edit-note-modal")).toBeDefined();

			await user.keyboard(" ");
			expect(screen.getByTestId("card-front")).toBeDefined();
			expect(screen.queryByTestId("card-back")).toBeNull();
		});
	});
});