fix: refresh admin self profile state
This commit is contained in:
@@ -71,12 +71,14 @@ describe('Users directory', () => {
|
||||
let http: HttpTestingController;
|
||||
let isAdmin: ReturnType<typeof signal<boolean>>;
|
||||
let currentUser: ReturnType<typeof signal<{ id: number; firstName: string; lastName: string; role: { id: number } }>>;
|
||||
let updateUser: ReturnType<typeof vi.fn>;
|
||||
let closeDialog: Subject<boolean>;
|
||||
let dialog: { open: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(async () => {
|
||||
isAdmin = signal(false);
|
||||
currentUser = signal({ id: 99, firstName: 'Nora', lastName: 'Viewer', role: { id: 2 } });
|
||||
updateUser = vi.fn();
|
||||
closeDialog = new Subject<boolean>();
|
||||
dialog = { open: vi.fn(() => ({ afterClosed: () => closeDialog.asObservable() })) };
|
||||
|
||||
@@ -86,7 +88,7 @@ describe('Users directory', () => {
|
||||
provideHttpClient(),
|
||||
provideHttpClientTesting(),
|
||||
provideRouter([]),
|
||||
{ provide: AuthStore, useValue: { isGlobalAdmin: isAdmin, currentUser } },
|
||||
{ provide: AuthStore, useValue: { isGlobalAdmin: isAdmin, currentUser, updateUser } },
|
||||
{ provide: MatDialog, useValue: dialog },
|
||||
],
|
||||
}).compileComponents();
|
||||
@@ -113,6 +115,42 @@ describe('Users directory', () => {
|
||||
return match as HTMLButtonElement;
|
||||
}
|
||||
|
||||
it('renders unknown role and status values explicitly', () => {
|
||||
create();
|
||||
flushDirectory();
|
||||
|
||||
expect(fixture.componentInstance['statusName']()).toBe('Unbekannt');
|
||||
expect(fixture.componentInstance['statusName'](99)).toBe('Unbekannt');
|
||||
expect(fixture.componentInstance['roleName']()).toBe('Unbekannt');
|
||||
expect(fixture.componentInstance['roleName'](99)).toBe('Unbekannt');
|
||||
});
|
||||
|
||||
it('persists returned names when an admin edits their own profile', () => {
|
||||
isAdmin.set(true);
|
||||
currentUser.set({ id: 7, firstName: 'Ada', lastName: 'Lovelace', role: { id: 1 } });
|
||||
create();
|
||||
flushDirectory(directoryPage([admin, ada]));
|
||||
|
||||
fixture.componentInstance['saveEdit'](ada, {
|
||||
firstName: 'Augusta',
|
||||
lastName: 'King',
|
||||
role: 2,
|
||||
});
|
||||
http.expectOne(`${adminApi}/7/profile`).flush({
|
||||
...ada,
|
||||
firstName: 'Augusta',
|
||||
lastName: 'King',
|
||||
});
|
||||
http.expectOne(`${api}?page=1&limit=20`).flush(directoryPage());
|
||||
|
||||
expect(updateUser).toHaveBeenCalledWith({
|
||||
id: 7,
|
||||
firstName: 'Augusta',
|
||||
lastName: 'King',
|
||||
role: { id: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function childButton(host: HTMLElement, label: string): HTMLButtonElement {
|
||||
const match = [...host.querySelectorAll('button')].find((element) => element.textContent?.includes(label));
|
||||
|
||||
@@ -125,11 +125,15 @@ export class Users {
|
||||
}
|
||||
|
||||
protected statusName(statusId?: number): string {
|
||||
return statusId === 2 ? 'Inaktiv' : 'Aktiv';
|
||||
if (statusId === 1) return 'Aktiv';
|
||||
if (statusId === 2) return 'Inaktiv';
|
||||
return 'Unbekannt';
|
||||
}
|
||||
|
||||
protected roleName(roleId?: number): string {
|
||||
return roleId === 1 ? 'Administrator' : 'Benutzer';
|
||||
if (roleId === 1) return 'Administrator';
|
||||
if (roleId === 2) return 'Benutzer';
|
||||
return 'Unbekannt';
|
||||
}
|
||||
|
||||
protected teamRoleName(name?: string): string {
|
||||
@@ -170,7 +174,17 @@ export class Users {
|
||||
let profileSaved = false;
|
||||
profileRequest
|
||||
.pipe(
|
||||
tap(() => (profileSaved = true)),
|
||||
tap((updatedUser) => {
|
||||
profileSaved = true;
|
||||
const currentUser = this.currentUser();
|
||||
if (currentUser?.id === updatedUser.id) {
|
||||
this.authStore.updateUser({
|
||||
...currentUser,
|
||||
firstName: updatedUser.firstName,
|
||||
lastName: updatedUser.lastName,
|
||||
});
|
||||
}
|
||||
}),
|
||||
switchMap(() =>
|
||||
roleId === value.role ? of(user) : this.adminUsersApi.updateRole(user.id, { role: value.role }),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user