-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrole-assignment-item.test.tsx
More file actions
316 lines (271 loc) · 9 KB
/
role-assignment-item.test.tsx
File metadata and controls
316 lines (271 loc) · 9 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { fireEvent, render, screen } from '@testing-library/react-native';
import React from 'react';
import { type PersonnelInfoResultData } from '@/models/v4/personnel/personnelInfoResultData';
import { type UnitRoleResultData } from '@/models/v4/unitRoles/unitRoleResultData';
import { RoleAssignmentItem } from '../role-assignment-item';
// Mock react-i18next
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string, defaultValue?: string | Record<string, string>) => {
if (typeof defaultValue === 'string') return defaultValue;
if (typeof defaultValue === 'object' && defaultValue.defaultValue) return defaultValue.defaultValue;
return key;
},
}),
}));
// Mock nativewind
jest.mock('nativewind', () => ({
useColorScheme: () => ({ colorScheme: 'light' }),
cssInterop: jest.fn(),
}));
// Mock the RoleUserSelectionModal to simplify testing
let mockModalOnSelectUser: ((userId?: string) => void) | undefined;
jest.mock('../role-user-selection-modal', () => ({
RoleUserSelectionModal: ({ isOpen, onClose, roleName, selectedUserId, users, onSelectUser }: any) => {
const { View, Text, TouchableOpacity } = require('react-native');
mockModalOnSelectUser = onSelectUser;
if (!isOpen) return null;
return (
<View testID="user-selection-modal">
<Text testID="modal-role-name">{roleName}</Text>
<TouchableOpacity testID="select-unassigned" onPress={() => { onSelectUser(undefined); onClose(); }}>
<Text>Unassigned</Text>
</TouchableOpacity>
{users.map((user: any) => (
<TouchableOpacity key={user.UserId} testID={`select-user-${user.UserId}`} onPress={() => { onSelectUser(user.UserId); onClose(); }}>
<Text>{`${user.FirstName} ${user.LastName}`}</Text>
</TouchableOpacity>
))}
</View>
);
},
}));
// Mock UI components
jest.mock('@/components/ui/text', () => ({
Text: ({ children, className, testID }: any) => {
const { Text } = require('react-native');
return <Text testID={testID || 'text'}>{children}</Text>;
},
}));
jest.mock('@/components/ui/vstack', () => ({
VStack: ({ children }: any) => {
const { View } = require('react-native');
return <View testID="vstack">{children}</View>;
},
}));
jest.mock('@/components/ui/hstack', () => ({
HStack: ({ children }: any) => {
const { View } = require('react-native');
return <View testID="hstack">{children}</View>;
},
}));
describe('RoleAssignmentItem', () => {
const mockOnAssignUser = jest.fn();
const mockRole: UnitRoleResultData = {
UnitRoleId: 'role1',
Name: 'Captain',
UnitId: 'unit1',
};
const mockUsers: PersonnelInfoResultData[] = [
{
UserId: 'user1',
FirstName: 'John',
LastName: 'Doe',
EmailAddress: 'john.doe@example.com',
DepartmentId: 'dept1',
IdentificationNumber: '',
MobilePhone: '',
GroupId: 'group1',
GroupName: 'Engine 1',
StatusId: '',
Status: 'Available',
StatusColor: '#00ff00',
StatusTimestamp: '',
StatusDestinationId: '',
StatusDestinationName: '',
StaffingId: '',
Staffing: 'On Shift',
StaffingColor: '#0000ff',
StaffingTimestamp: '',
Roles: ['Firefighter'],
},
{
UserId: 'user2',
FirstName: 'Jane',
LastName: 'Smith',
EmailAddress: 'jane.smith@example.com',
DepartmentId: 'dept1',
IdentificationNumber: '',
MobilePhone: '',
GroupId: '',
GroupName: '',
StatusId: '',
Status: '',
StatusColor: '',
StatusTimestamp: '',
StatusDestinationId: '',
StatusDestinationName: '',
StaffingId: '',
Staffing: '',
StaffingColor: '',
StaffingTimestamp: '',
Roles: [],
},
];
beforeEach(() => {
jest.clearAllMocks();
mockModalOnSelectUser = undefined;
});
it('renders the role name', () => {
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
expect(screen.getByText('Captain')).toBeTruthy();
});
it('displays "Unassigned" when no user is assigned', () => {
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
expect(screen.getByText('Unassigned')).toBeTruthy();
});
it('displays assigned user name inline when user is assigned', () => {
const assignedUser = mockUsers[0];
render(
<RoleAssignmentItem
role={mockRole}
assignedUser={assignedUser}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
expect(screen.getByText('John Doe')).toBeTruthy();
});
it('displays user details inline (group, status, staffing)', () => {
const assignedUser = mockUsers[0];
render(
<RoleAssignmentItem
role={mockRole}
assignedUser={assignedUser}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
expect(screen.getByText('Engine 1')).toBeTruthy();
expect(screen.getByText('Available')).toBeTruthy();
expect(screen.getByText('On Shift')).toBeTruthy();
});
it('opens selection modal on tap', () => {
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
// Modal should not be visible initially
expect(screen.queryByTestId('user-selection-modal')).toBeNull();
// Tap the item
fireEvent.press(screen.getByTestId('role-assignment-role1'));
// Modal should now be visible
expect(screen.getByTestId('user-selection-modal')).toBeTruthy();
expect(screen.getByTestId('modal-role-name')).toBeTruthy();
});
it('shows all users including those assigned to other roles in the modal', () => {
const currentAssignments = [
{ roleId: 'role2', userId: 'user2', roleName: 'Engineer' }, // user2 is assigned to a different role
];
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={currentAssignments}
/>
);
// Open modal
fireEvent.press(screen.getByTestId('role-assignment-role1'));
// Should show both users (no filtering)
expect(screen.getByTestId('select-user-user1')).toBeTruthy();
expect(screen.getByTestId('select-user-user2')).toBeTruthy();
// Unassigned option should always be present
expect(screen.getByTestId('select-unassigned')).toBeTruthy();
});
it('shows all users in the modal including those assigned to same and other roles', () => {
const assignedUser = mockUsers[0];
const currentAssignments = [
{ roleId: 'role1', userId: 'user1', roleName: 'Captain' }, // user1 is assigned to this role
{ roleId: 'role2', userId: 'user2', roleName: 'Engineer' }, // user2 is assigned to a different role
];
render(
<RoleAssignmentItem
role={mockRole}
assignedUser={assignedUser}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={currentAssignments}
/>
);
// Open modal
fireEvent.press(screen.getByTestId('role-assignment-role1'));
// Should show both users (no filtering, all users visible)
expect(screen.getByTestId('select-user-user1')).toBeTruthy();
expect(screen.getByTestId('select-user-user2')).toBeTruthy();
});
it('calls onAssignUser when selecting a user in the modal', () => {
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
// Open modal
fireEvent.press(screen.getByTestId('role-assignment-role1'));
// Select user1
fireEvent.press(screen.getByTestId('select-user-user1'));
expect(mockOnAssignUser).toHaveBeenCalledWith('user1');
});
it('calls onAssignUser with undefined when selecting Unassigned', () => {
const assignedUser = mockUsers[0];
render(
<RoleAssignmentItem
role={mockRole}
assignedUser={assignedUser}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
// Open modal
fireEvent.press(screen.getByTestId('role-assignment-role1'));
// Select Unassigned
fireEvent.press(screen.getByTestId('select-unassigned'));
expect(mockOnAssignUser).toHaveBeenCalledWith(undefined);
});
it('has proper accessibility role on the pressable', () => {
render(
<RoleAssignmentItem
role={mockRole}
availableUsers={mockUsers}
onAssignUser={mockOnAssignUser}
currentAssignments={[]}
/>
);
const pressable = screen.getByTestId('role-assignment-role1');
expect(pressable.props.accessibilityRole).toBe('button');
});
});