Spaces:
Running
Running
File size: 3,963 Bytes
f287aeb | 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 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
getManagementFarms,
getManagementUsers,
signupStaffMember,
updateUserModules,
updateUserRoleAndFarms,
} from '../services/userManagementApi';
const jsonResponse = (body: unknown, status = 200): Response =>
new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
});
describe('userManagementApi', () => {
const fetchMock = vi.fn();
beforeEach(() => {
fetchMock.mockReset();
vi.stubGlobal('fetch', fetchMock);
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('normalizes users and farms payloads', async () => {
fetchMock
.mockResolvedValueOnce(
jsonResponse({
success: true,
data: [
{
id: 'user-1',
email: 'staff@fishfarm.com',
firstName: 'John',
lastName: 'Doe',
role: 'manager',
status: 'active',
farms: [
{ id: 'farm-1', name: 'Main Farm' },
{ id: 'farm-2', name: 'Delta Farm' },
],
modules: ['inventory', 'sales'],
},
],
}),
)
.mockResolvedValueOnce(
jsonResponse({
success: true,
data: [
{ id: 'farm-1', name: 'Main Farm' },
{ id: 'farm-2', name: 'Delta Farm' },
],
}),
);
const users = await getManagementUsers();
const farms = await getManagementFarms();
expect(users).toEqual([
{
id: 'user-1',
email: 'staff@fishfarm.com',
firstName: 'John',
lastName: 'Doe',
name: 'John Doe',
role: 'MANAGER',
status: 'ACTIVE',
gender: undefined,
farmIds: ['farm-1', 'farm-2'],
farmNames: ['Main Farm', 'Delta Farm'],
modules: ['inventory', 'sales'],
},
]);
expect(farms).toEqual([
{ id: 'farm-1', name: 'Main Farm' },
{ id: 'farm-2', name: 'Delta Farm' },
]);
});
it('sends signup payload with normalized role and farmIds', async () => {
fetchMock.mockResolvedValueOnce(jsonResponse({ success: true }, 201));
await signupStaffMember({
email: 'staff@fishfarm.com',
password: 'SecurePassword123!',
firstName: 'John',
lastName: 'Doe',
role: 'technican',
farmIds: ['farm-1'],
gender: 'MALE',
dateOfBirth: '1990-05-15',
address: 'Aqua Street',
});
expect(fetchMock).toHaveBeenCalledWith(
expect.stringContaining('/auth/signup'),
expect.objectContaining({
method: 'POST',
body: expect.any(String),
}),
);
const signupBody = JSON.parse(fetchMock.mock.calls[0][1].body as string);
expect(signupBody).toMatchObject({
role: 'TECHNICAN',
farmIds: ['farm-1'],
});
});
it('sends update payloads for role/farms and module overrides', async () => {
fetchMock
.mockResolvedValueOnce(jsonResponse({ success: true }))
.mockResolvedValueOnce(jsonResponse({ success: true }));
await updateUserRoleAndFarms('user-1', {
role: 'manager',
farmIds: ['farm-1', 'farm-2'],
});
await updateUserModules('user-1', {
action: 'REMOVE',
moduleIds: ['inventory'],
});
expect(fetchMock).toHaveBeenNthCalledWith(
1,
expect.stringContaining('/users/user-1'),
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({
role: 'MANAGER',
farmIds: ['farm-1', 'farm-2'],
}),
}),
);
expect(fetchMock).toHaveBeenNthCalledWith(
2,
expect.stringContaining('/users/user-1/modules'),
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({
action: 'REMOVE',
moduleIds: ['inventory'],
}),
}),
);
});
});
|