diff --git a/frontend/src/app/features/settings/user-management/user-management.component.html b/frontend/src/app/features/settings/user-management/user-management.component.html index c0881c826..777cd78fa 100644 --- a/frontend/src/app/features/settings/user-management/user-management.component.html +++ b/frontend/src/app/features/settings/user-management/user-management.component.html @@ -34,7 +34,7 @@
- @if (users.length === 0) { + @if (users().length === 0) {

{{ t('emptyState.title') }}

@@ -42,7 +42,7 @@
} @else {
- @for (user of users; track user.id) { + @for (user of users(); track user.id) {
diff --git a/frontend/src/app/features/settings/user-management/user-management.component.spec.ts b/frontend/src/app/features/settings/user-management/user-management.component.spec.ts index 0c7092dce..22f6b5a5b 100644 --- a/frontend/src/app/features/settings/user-management/user-management.component.spec.ts +++ b/frontend/src/app/features/settings/user-management/user-management.component.spec.ts @@ -1,14 +1,86 @@ -import {describe, it} from 'vitest'; +import {signal} from '@angular/core'; +import {TestBed} from '@angular/core/testing'; +import {MessageService} from 'primeng/api'; +import {of, type Observable} from 'rxjs'; +import {describe, expect, it, vi} from 'vitest'; -// NOTE(frontend-seam): Real coverage here needs seams around table row editing, confirm dialog -// usage, password dialog state, and dialog-launcher interactions so user administration can be -// exercised without building the full Prime table and modal stack. -describe.skip('UserManagementComponent', () => { - it('needs service seams to verify user loading, edit toggling, and save payload shaping', () => { +import {getTranslocoModule} from '../../../core/testing/transloco-testing'; +import {AppSettings} from '../../../shared/model/app-settings.model'; +import {AppSettingsService} from '../../../shared/service/app-settings.service'; +import {DialogLauncherService} from '../../../shared/services/dialog-launcher.service'; +import {LibraryService} from '../../book/service/library.service'; +import {UserManagementComponent, type UserWithEditing} from './user-management.component'; +import {User, UserService} from './user.service'; + +function buildUser(overrides: Partial = {}): User { + return { + id: 1, + username: 'admin', + name: 'Admin', + email: 'admin@example.com', + locale: 'en', + theme: 'grimmory', + themeAccent: null, + themeSyncEnabled: true, + assignedLibraries: [], + permissions: {admin: false, ...overrides} as User['permissions'], + userSettings: {} as User['userSettings'], + }; +} + +interface UserManagementTestEnv { + userState: ReturnType>; + appSettingsState: ReturnType>; + getUsers: () => Observable; +} + +function setupUserManagementTest(env: UserManagementTestEnv): void { + TestBed.configureTestingModule({ + imports: [UserManagementComponent, getTranslocoModule()], + providers: [ + { + provide: UserService, + useValue: { + currentUser: () => env.userState(), + getUsers: env.getUsers + } + }, + { provide: LibraryService, useValue: { libraries: signal([]) } }, + { + provide: DialogLauncherService, + useValue: { + openCreateUserDialog: vi.fn(() => Promise.resolve(null)), + }, + }, + {provide: AppSettingsService, useValue: {appSettings: () => env.appSettingsState()}}, + {provide: MessageService, useValue: {add: vi.fn()}}, + ], + }); +} + +describe('UserManagementComponent', () => { + it('loads and hydrates users on init', () => { + const userState = signal(buildUser({admin: true})); + const appSettingsState = signal(null); + const getUsers = vi.fn(() => of([buildUser({admin: true})] as UserWithEditing[])); + + setupUserManagementTest({userState, appSettingsState, getUsers}); + + const fixture = TestBed.createComponent(UserManagementComponent); + const component = fixture.componentInstance; + fixture.detectChanges(); + + expect(getUsers).toHaveBeenCalledOnce(); + expect(component.users()).toHaveLength(1); + + fixture.destroy(); + }); + + it.skip('needs service seams to verify user loading, edit toggling, and save payload shaping', () => { // TODO(seam): Cover loadUsers, toggleEdit, and saveUser once the table-editing state and async user-service flows are isolated. }); - it('needs dialog seams to verify create-user refresh, delete confirmation, and password-change validation', () => { + it.skip('needs dialog seams to verify create-user refresh, delete confirmation, and password-change validation', () => { // TODO(seam): Cover openCreateUserDialog, deleteUser, and submitPasswordChange after extracting confirm and modal runtime concerns. }); }); diff --git a/frontend/src/app/features/settings/user-management/user-management.component.ts b/frontend/src/app/features/settings/user-management/user-management.component.ts index 0f0fb99d0..5bd738c3c 100644 --- a/frontend/src/app/features/settings/user-management/user-management.component.ts +++ b/frontend/src/app/features/settings/user-management/user-management.component.ts @@ -1,4 +1,4 @@ -import {Component, DestroyRef, inject, OnInit} from '@angular/core'; +import {Component, DestroyRef, inject, OnInit, signal, WritableSignal} from '@angular/core'; import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; import {FormsModule} from '@angular/forms'; import {Button} from 'primeng/button'; @@ -19,7 +19,7 @@ import {DialogLauncherService} from '../../../shared/services/dialog-launcher.se import {ContentRestrictionsEditorComponent} from './content-restrictions-editor/content-restrictions-editor.component'; import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco'; -interface UserWithEditing extends User { +export interface UserWithEditing extends User { isEditing?: boolean; selectedLibraryIds?: number[]; libraryNames?: string; @@ -56,7 +56,7 @@ export class UserManagementComponent implements OnInit { private destroyRef = inject(DestroyRef); get allLibraries() { return this.libraryService.libraries(); } - users: UserWithEditing[] = []; + users: WritableSignal = signal([]); currentUser: User | null = null; editingLibraryIds: number[] = []; expandedRows: Record = {}; @@ -82,13 +82,13 @@ export class UserManagementComponent implements OnInit { loadUsers() { this.userService.getUsers().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({ next: (data) => { - this.users = data.map((user) => ({ + this.users.set(data.map((user: UserWithEditing) => ({ ...user, isEditing: false, selectedLibraryIds: user.assignedLibraries?.map((lib) => lib.id!).filter(id => id !== undefined) as number[] || [], libraryNames: user.assignedLibraries?.map((lib) => lib.name).join(', ') || '', - })); + }))); }, error: () => { this.messageService.add({