Skip to content

Commit f618429

Browse files
authored
Merge pull request #2980 from trycompai/tofik/warn-policy-ack-reset
fix(app): warn before invalidating policy acknowledgments
2 parents b1315a0 + 8b0b863 commit f618429

9 files changed

Lines changed: 658 additions & 251 deletions

File tree

‎apps/app/src/app/(app)/[orgId]/overview/components/ConfirmActionDialog.tsx‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎apps/app/src/app/(app)/[orgId]/overview/components/ToDoOverview.test.tsx‎

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { render, screen } from '@testing-library/react';
2-
import { beforeEach, describe, expect, it, vi } from 'vitest';
31
import {
4-
setMockPermissions,
52
ADMIN_PERMISSIONS,
63
AUDITOR_PERMISSIONS,
74
NO_PERMISSIONS,
85
mockHasPermission,
6+
setMockPermissions,
97
} from '@/test-utils/mocks/permissions';
8+
import { render, screen } from '@testing-library/react';
9+
import userEvent from '@testing-library/user-event';
10+
import { beforeEach, describe, expect, it, vi } from 'vitest';
1011

1112
vi.mock('@/hooks/use-permissions', () => ({
1213
usePermissions: () => ({
@@ -27,6 +28,15 @@ vi.mock('next/link', () => ({
2728
default: ({ children, href }: any) => <a href={href}>{children}</a>,
2829
}));
2930

31+
const { mockRefresh } = vi.hoisted(() => ({
32+
mockRefresh: vi.fn(),
33+
}));
34+
vi.mock('next/navigation', () => ({
35+
useRouter: () => ({
36+
refresh: mockRefresh,
37+
}),
38+
}));
39+
3040
vi.mock('@trycompai/ui/button', () => ({
3141
Button: ({ children, disabled, asChild, ...props }: any) => (
3242
<button disabled={disabled} {...props}>
@@ -61,9 +71,24 @@ vi.mock('@trycompai/ui/tabs', () => ({
6171
),
6272
}));
6373

64-
vi.mock('./ConfirmActionDialog', () => ({
65-
ConfirmActionDialog: ({ isOpen, title }: any) =>
66-
isOpen ? <div data-testid="confirm-dialog">{title}</div> : null,
74+
vi.mock('@/components/policies/PolicyAcknowledgmentInvalidationDialog', () => ({
75+
getPolicyAcknowledgmentTotal: (policies: Array<{ signedBy?: string[] }>) =>
76+
policies.reduce((total, policy) => total + (policy.signedBy?.length ?? 0), 0),
77+
PolicyAcknowledgmentInvalidationDialog: ({
78+
acknowledgmentCount,
79+
onConfirm,
80+
open,
81+
}: {
82+
acknowledgmentCount: number;
83+
onConfirm: () => void;
84+
open: boolean;
85+
}) =>
86+
open ? (
87+
<div data-testid="acknowledgment-dialog">
88+
<span>{acknowledgmentCount}</span>
89+
<button onClick={onConfirm}>Publish and invalidate</button>
90+
</div>
91+
) : null,
6792
}));
6893

6994
vi.mock('lucide-react', () => ({
@@ -89,12 +114,14 @@ const mockUnpublishedPolicies = [
89114
name: 'Security Policy',
90115
status: 'draft',
91116
organizationId: 'org-1',
117+
signedBy: ['user-1', 'user-2'],
92118
},
93119
{
94120
id: 'pol-2',
95121
name: 'Privacy Policy',
96122
status: 'draft',
97123
organizationId: 'org-1',
124+
signedBy: [],
98125
},
99126
] as any[];
100127

@@ -123,6 +150,14 @@ const defaultProps = {
123150
describe('ToDoOverview', () => {
124151
beforeEach(() => {
125152
vi.clearAllMocks();
153+
vi.stubGlobal(
154+
'fetch',
155+
vi.fn(() =>
156+
Promise.resolve({
157+
ok: true,
158+
}),
159+
),
160+
);
126161
});
127162

128163
describe('Permission gating', () => {
@@ -131,9 +166,7 @@ describe('ToDoOverview', () => {
131166

132167
render(<ToDoOverview {...defaultProps} />);
133168

134-
expect(
135-
screen.getByRole('button', { name: /publish all policies/i }),
136-
).toBeInTheDocument();
169+
expect(screen.getByRole('button', { name: /publish all policies/i })).toBeInTheDocument();
137170
});
138171

139172
it('hides "Publish All Policies" button when user lacks policy:update permission (auditor)', () => {
@@ -188,37 +221,21 @@ describe('ToDoOverview', () => {
188221

189222
render(<ToDoOverview {...defaultProps} />);
190223

191-
expect(
192-
screen.getByText('Complete SOC 2 audit'),
193-
).toBeInTheDocument();
224+
expect(screen.getByText('Complete SOC 2 audit')).toBeInTheDocument();
194225
});
195226

196227
it('shows "All policies are published!" when no unpublished policies exist', () => {
197228
setMockPermissions(ADMIN_PERMISSIONS);
198229

199-
render(
200-
<ToDoOverview
201-
{...defaultProps}
202-
unpublishedPolicies={[]}
203-
remainingPolicies={0}
204-
/>,
205-
);
230+
render(<ToDoOverview {...defaultProps} unpublishedPolicies={[]} remainingPolicies={0} />);
206231

207-
expect(
208-
screen.getByText('All policies are published!'),
209-
).toBeInTheDocument();
232+
expect(screen.getByText('All policies are published!')).toBeInTheDocument();
210233
});
211234

212235
it('does not show publish button even with permissions when no unpublished policies', () => {
213236
setMockPermissions(ADMIN_PERMISSIONS);
214237

215-
render(
216-
<ToDoOverview
217-
{...defaultProps}
218-
unpublishedPolicies={[]}
219-
remainingPolicies={0}
220-
/>,
221-
);
238+
render(<ToDoOverview {...defaultProps} unpublishedPolicies={[]} remainingPolicies={0} />);
222239

223240
expect(
224241
screen.queryByRole('button', { name: /publish all policies/i }),
@@ -232,9 +249,47 @@ describe('ToDoOverview', () => {
232249

233250
expect(screen.getByTestId('tab-trigger-policies')).toBeInTheDocument();
234251
expect(screen.getByTestId('tab-trigger-tasks')).toBeInTheDocument();
235-
expect(
236-
screen.getByTestId('tab-trigger-offboarding'),
237-
).toBeInTheDocument();
252+
expect(screen.getByTestId('tab-trigger-offboarding')).toBeInTheDocument();
253+
});
254+
255+
it('shows acknowledgment warning before publishing policies with existing acknowledgments', async () => {
256+
const user = userEvent.setup();
257+
setMockPermissions(ADMIN_PERMISSIONS);
258+
259+
render(<ToDoOverview {...defaultProps} />);
260+
261+
await user.click(screen.getByRole('button', { name: /publish all policies/i }));
262+
263+
expect(screen.getByTestId('acknowledgment-dialog')).toHaveTextContent('2');
264+
expect(fetch).not.toHaveBeenCalled();
265+
266+
await user.click(screen.getByRole('button', { name: /publish and invalidate/i }));
267+
268+
expect(fetch).toHaveBeenCalledWith('/api/policies/publish-all', {
269+
method: 'POST',
270+
headers: { 'Content-Type': 'application/json' },
271+
credentials: 'include',
272+
});
273+
});
274+
275+
it('publishes immediately when no acknowledgments will be invalidated', async () => {
276+
const user = userEvent.setup();
277+
setMockPermissions(ADMIN_PERMISSIONS);
278+
279+
render(
280+
<ToDoOverview
281+
{...defaultProps}
282+
unpublishedPolicies={mockUnpublishedPolicies.map((policy) => ({
283+
...policy,
284+
signedBy: [],
285+
}))}
286+
/>,
287+
);
288+
289+
await user.click(screen.getByRole('button', { name: /publish all policies/i }));
290+
291+
expect(screen.queryByTestId('acknowledgment-dialog')).not.toBeInTheDocument();
292+
expect(fetch).toHaveBeenCalledOnce();
238293
});
239294
});
240295
});

0 commit comments

Comments
 (0)