-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInviteMemberDialog.placement.test.tsx
More file actions
165 lines (144 loc) · 6.28 KB
/
Copy pathInviteMemberDialog.placement.test.tsx
File metadata and controls
165 lines (144 loc) · 6.28 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* InviteMemberDialog — scoped-invitation placement (framework ADR-0105 D8).
*
* The placement section NARROWS to what the issuer may actually delegate
* (`security/my-delegable-scope`); the server still authorizes the pair
* against the issuer's `adminScope`. So the behaviours worth pinning are the
* ones a user would notice going wrong:
*
* 1. no delegated authority (or no runtime exposing it) ⇒ no placement UI at
* all — never a form the server would refuse;
* 2. the options offered are exactly the delegable ones;
* 3. a chosen placement reaches `inviteMember`, and a HALF-chosen one does
* not (a unit with no positions is not a placement).
*/
import '@testing-library/jest-dom/vitest';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor, within } from '@testing-library/react';
vi.mock('@object-ui/i18n', () => ({
useObjectTranslation: () => ({
t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key),
}),
}));
const inviteMember = vi.fn();
const describeDelegableScope = vi.fn();
vi.mock('@object-ui/auth', () => ({
useAuth: () => ({ inviteMember, describeDelegableScope }),
}));
// Passthrough primitives — the Select is rendered as a native <select> so the
// test can drive it without Radix portal machinery.
vi.mock('@object-ui/components', () => ({
Dialog: ({ open, children }: any) => (open ? <div>{children}</div> : null),
DialogContent: (p: any) => <div {...p} />,
DialogDescription: (p: any) => <p {...p} />,
DialogFooter: (p: any) => <div {...p} />,
DialogHeader: (p: any) => <div {...p} />,
DialogTitle: (p: any) => <h2 {...p} />,
Button: ({ children, ...rest }: any) => <button {...rest}>{children}</button>,
Input: (p: any) => <input {...p} />,
Label: (p: any) => <label {...p} />,
Select: ({ value, onValueChange, children }: any) => (
<select data-testid="select" value={value} onChange={(e) => onValueChange(e.target.value)}>
<option value="" />
{children}
</select>
),
SelectContent: (p: any) => <>{p.children}</>,
SelectItem: ({ value, children }: any) => <option value={value}>{children}</option>,
SelectTrigger: (p: any) => <>{p.children}</>,
SelectValue: () => null,
}));
vi.mock('lucide-react', () => ({
Loader2: () => <span />,
Copy: () => <span />,
Check: () => <span />,
}));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
import { InviteMemberDialog } from '../manage/InviteMemberDialog';
const DELEGABLE = {
isTenantAdmin: false,
placeableBusinessUnitIds: ['bu_plant_a', 'bu_plant_a_qc'],
assignablePositions: ['qc_inspector'],
scopes: [],
};
const renderDialog = () =>
render(
<InviteMemberDialog organizationId="org-42" open onOpenChange={() => {}} />,
);
const fillEmail = () =>
fireEvent.change(screen.getByTestId('invite-email-input'), {
target: { value: 'p@x.test' },
});
beforeEach(() => {
vi.clearAllMocks();
inviteMember.mockResolvedValue({ id: 'inv-1', email: 'p@x.test', role: 'member' });
});
describe('InviteMemberDialog — placement (ADR-0105 D8)', () => {
it('hides placement entirely when the caller may delegate nothing', async () => {
describeDelegableScope.mockResolvedValue({
isTenantAdmin: false,
placeableBusinessUnitIds: [],
assignablePositions: [],
scopes: [],
});
renderDialog();
await waitFor(() => expect(describeDelegableScope).toHaveBeenCalled());
expect(screen.queryByTestId('invite-placement-section')).not.toBeInTheDocument();
});
it('hides placement when the deployment does not expose the surface at all', async () => {
describeDelegableScope.mockResolvedValue(null);
renderDialog();
await waitFor(() => expect(describeDelegableScope).toHaveBeenCalled());
expect(screen.queryByTestId('invite-placement-section')).not.toBeInTheDocument();
});
it('offers exactly the delegable units, and positions only once a unit is chosen', async () => {
describeDelegableScope.mockResolvedValue(DELEGABLE);
renderDialog();
await screen.findByTestId('invite-placement-section');
const unitSelect = within(screen.getByTestId('invite-placement-section')).getByTestId('select');
expect(unitSelect).toHaveTextContent('bu_plant_a');
expect(unitSelect).toHaveTextContent('bu_plant_a_qc');
// Positions appear only after a unit is picked — an unanchored assignment
// is refused by the gate, so offering it first would be misleading.
expect(screen.queryByTestId('invite-positions')).not.toBeInTheDocument();
fireEvent.change(unitSelect, { target: { value: 'bu_plant_a' } });
await screen.findByTestId('invite-positions');
expect(screen.getByTestId('invite-position-qc_inspector')).toBeInTheDocument();
});
it('sends a complete placement with the invitation', async () => {
describeDelegableScope.mockResolvedValue(DELEGABLE);
const { container } = renderDialog();
await screen.findByTestId('invite-placement-section');
fillEmail();
fireEvent.change(within(screen.getByTestId('invite-placement-section')).getByTestId('select'), {
target: { value: 'bu_plant_a' },
});
fireEvent.click(await screen.findByTestId('invite-position-qc_inspector'));
fireEvent.submit(container.querySelector('form')!);
await waitFor(() => expect(inviteMember).toHaveBeenCalledTimes(1));
expect(inviteMember).toHaveBeenCalledWith({
organizationId: 'org-42',
email: 'p@x.test',
role: 'member',
businessUnitId: 'bu_plant_a',
positions: ['qc_inspector'],
});
});
it('a unit with no positions is NOT a placement — the invite goes out plain', async () => {
describeDelegableScope.mockResolvedValue(DELEGABLE);
const { container } = renderDialog();
await screen.findByTestId('invite-placement-section');
fillEmail();
fireEvent.change(within(screen.getByTestId('invite-placement-section')).getByTestId('select'), {
target: { value: 'bu_plant_a' },
});
fireEvent.submit(container.querySelector('form')!);
await waitFor(() => expect(inviteMember).toHaveBeenCalledTimes(1));
expect(inviteMember).toHaveBeenCalledWith({
organizationId: 'org-42',
email: 'p@x.test',
role: 'member',
});
});
});