-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPRDVersionHistoryModal.test.tsx
More file actions
295 lines (239 loc) · 10.2 KB
/
Copy pathPRDVersionHistoryModal.test.tsx
File metadata and controls
295 lines (239 loc) · 10.2 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
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
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import useSWR from 'swr';
import { PRDVersionHistoryModal } from '@/components/prd/PRDVersionHistoryModal';
import { prdApi } from '@/lib/api';
import type { PrdResponse, PrdDiffResponse } from '@/types';
// ResizeObserver is not available in jsdom
global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
jest.mock('swr');
jest.mock('sonner', () => ({
toast: { success: jest.fn(), error: jest.fn() },
}));
// Radix ScrollArea Viewport hides children in jsdom — render children directly
jest.mock('@/components/ui/scroll-area', () => ({
ScrollArea: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
ScrollBar: () => null,
}));
jest.mock('@/lib/api', () => ({
prdApi: {
getVersions: jest.fn(),
diff: jest.fn(),
createVersion: jest.fn(),
},
}));
const mockUseSWR = useSWR as jest.MockedFunction<typeof useSWR>;
const mockDiff = prdApi.diff as jest.MockedFunction<typeof prdApi.diff>;
const mockCreateVersion = prdApi.createVersion as jest.MockedFunction<typeof prdApi.createVersion>;
const WORKSPACE = '/home/user/project';
const makeVersion = (v: number, summary: string | null = null): PrdResponse => ({
id: `prd-${v}`,
workspace_id: 'ws-1',
title: 'My PRD',
content: `# Version ${v} content`,
metadata: {},
created_at: `2026-01-0${v}T00:00:00Z`,
version: v,
parent_id: v > 1 ? `prd-${v - 1}` : null,
change_summary: summary,
chain_id: 'chain-1',
});
const fakeVersions: PrdResponse[] = [
makeVersion(3, 'Added section 3'),
makeVersion(2, 'Updated intro'),
makeVersion(1, null),
];
const currentPrd = fakeVersions[0];
const defaultProps = {
open: true,
onOpenChange: jest.fn(),
prd: currentPrd,
workspacePath: WORKSPACE,
onVersionRestored: jest.fn(),
};
function setupSWR(versions = fakeVersions) {
mockUseSWR.mockReturnValue({
data: versions,
error: undefined,
isLoading: false,
mutate: jest.fn(),
} as unknown as ReturnType<typeof useSWR>);
}
describe('PRDVersionHistoryModal', () => {
beforeEach(() => {
jest.clearAllMocks();
setupSWR();
});
it('renders the dialog with version list', () => {
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText('Version History')).toBeInTheDocument();
expect(screen.getByText('Version 3')).toBeInTheDocument();
expect(screen.getByText('Version 2')).toBeInTheDocument();
expect(screen.getByText('Version 1')).toBeInTheDocument();
});
it('shows change_summary when present', () => {
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText('Added section 3')).toBeInTheDocument();
expect(screen.getByText('Updated intro')).toBeInTheDocument();
});
it('shows "No summary" for versions with null change_summary', () => {
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText('No summary')).toBeInTheDocument();
});
it('highlights the current version with a "Current" badge', () => {
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText('Current')).toBeInTheDocument();
});
it('shows loading state while fetching', () => {
mockUseSWR.mockReturnValue({
data: undefined,
error: undefined,
isLoading: true,
mutate: jest.fn(),
} as unknown as ReturnType<typeof useSWR>);
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText(/loading/i)).toBeInTheDocument();
});
it('shows error state on fetch failure', () => {
mockUseSWR.mockReturnValue({
data: undefined,
error: new Error('Network error'),
isLoading: false,
mutate: jest.fn(),
} as unknown as ReturnType<typeof useSWR>);
render(<PRDVersionHistoryModal {...defaultProps} />);
expect(screen.getByText(/failed to load/i)).toBeInTheDocument();
});
describe('View version', () => {
it('shows content preview when View button is clicked', async () => {
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]); // click View on version 2
expect(screen.getByText(/Version 2 content/)).toBeInTheDocument();
});
it('shows "Back to list" button in preview mode', async () => {
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]);
expect(screen.getByRole('button', { name: /back to list/i })).toBeInTheDocument();
});
it('returns to version list when Back is clicked', async () => {
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]);
await user.click(screen.getByRole('button', { name: /back to list/i }));
expect(screen.getByText('Version History')).toBeInTheDocument();
expect(screen.queryByText('# Version 2 content')).not.toBeInTheDocument();
});
});
describe('Compare with current', () => {
it('calls prdApi.diff and shows diff output', async () => {
const fakeDiff: PrdDiffResponse = {
version1: 2,
version2: 3,
diff: '@@ -1 +1 @@\n-# Version 2 content\n+# Version 3 content',
};
mockDiff.mockResolvedValueOnce(fakeDiff);
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
// versions ordered newest-first; version 3 is current (no View btn), so index 0 = version 2
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]); // version 2
const compareBtn = screen.getByRole('button', { name: /compare with current/i });
await user.click(compareBtn);
await waitFor(() => {
expect(mockDiff).toHaveBeenCalledWith(
currentPrd.id,
WORKSPACE,
2,
3
);
});
await waitFor(() => {
expect(screen.getByText(/@@ -1 \+1 @@/)).toBeInTheDocument();
});
});
it('shows error message and re-enables Compare button on diff failure', async () => {
mockDiff.mockRejectedValueOnce(new Error('Network error'));
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]); // version 2
const compareBtn = screen.getByRole('button', { name: /compare with current/i });
await user.click(compareBtn);
await waitFor(() => {
expect(mockDiff).toHaveBeenCalled();
});
await waitFor(() => {
expect(screen.getByText(/failed to load diff/i)).toBeInTheDocument();
});
// Compare button should be re-enabled so the user can retry
expect(screen.getByRole('button', { name: /compare with current/i })).not.toBeDisabled();
});
});
describe('Restore version', () => {
it('shows confirmation UI when Restore is clicked', async () => {
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]); // version 2
const restoreBtn = screen.getByRole('button', { name: /restore this version/i });
await user.click(restoreBtn);
expect(screen.getByText(/restore version 2/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /confirm restore/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
});
it('calls createVersion with restored content on confirm', async () => {
const restoredPrd = makeVersion(4, 'Restored from version 2');
mockCreateVersion.mockResolvedValueOnce(restoredPrd);
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]); // version 2
await user.click(screen.getByRole('button', { name: /restore this version/i }));
await user.click(screen.getByRole('button', { name: /confirm restore/i }));
await waitFor(() => {
expect(mockCreateVersion).toHaveBeenCalledWith(
currentPrd.id,
WORKSPACE,
'# Version 2 content',
'Restored from version 2'
);
});
await waitFor(() => {
expect(defaultProps.onVersionRestored).toHaveBeenCalledWith(restoredPrd);
});
});
it('cancels restore without calling API', async () => {
const user = userEvent.setup();
render(<PRDVersionHistoryModal {...defaultProps} />);
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
await user.click(viewButtons[0]);
await user.click(screen.getByRole('button', { name: /restore this version/i }));
await user.click(screen.getByRole('button', { name: /cancel/i }));
expect(mockCreateVersion).not.toHaveBeenCalled();
// Should return to preview without confirmation UI
expect(screen.queryByRole('button', { name: /confirm restore/i })).not.toBeInTheDocument();
});
it('does not show Restore button for the current version', () => {
render(<PRDVersionHistoryModal {...defaultProps} />);
// Version 3 is current — its View button should not be visible (or Restore should be absent)
// The current version row should not have a "View" button at all
const viewButtons = screen.getAllByRole('button', { name: /^view$/i });
// Only versions 1 and 2 should have View buttons (not version 3 which is current)
expect(viewButtons).toHaveLength(2);
});
});
it('does not render version list when closed', () => {
render(<PRDVersionHistoryModal {...defaultProps} open={false} />);
expect(screen.queryByText('Version History')).not.toBeInTheDocument();
});
});