diff --git a/frontend/src/app/features/settings/audit-logs/audit-logs.component.html b/frontend/src/app/features/settings/audit-logs/audit-logs.component.html index df4b91625..a288f249f 100644 --- a/frontend/src/app/features/settings/audit-logs/audit-logs.component.html +++ b/frontend/src/app/features/settings/audit-logs/audit-logs.component.html @@ -25,7 +25,7 @@ class="action-filter">
{ - it('needs route and timer seams to verify filter restoration, pagination, and auto-refresh behavior', () => { - // TODO(seam): Cover query-param sync, table lazy loading, and interval refresh once route/timer dependencies are isolated. +import {getTranslocoModule} from '../../../core/testing/transloco-testing'; +import {AuditLog, AuditLogService, PageableResponse} from './audit-log.service'; +import {AuditLogsComponent} from './audit-logs.component'; + +describe('AuditLogsComponent', () => { + let fixture: ComponentFixture; + let usernameResponse: Subject; + const getAuditLogs = vi.fn(); + const getDistinctUsernames = vi.fn(); + + beforeEach(async () => { + usernameResponse = new Subject(); + getAuditLogs + .mockReset() + .mockReturnValueOnce(of(createResponse('Initial audit log'))) + .mockReturnValueOnce(of(createResponse('Refreshed audit log'))); + getDistinctUsernames + .mockReset() + .mockReturnValue(usernameResponse.asObservable()); + + await TestBed.configureTestingModule({ + imports: [AuditLogsComponent, getTranslocoModule()], + providers: [ + provideZonelessChangeDetection(), + { + provide: AuditLogService, + useValue: { + getAuditLogs, + getDistinctUsernames, + }, + }, + { + provide: ActivatedRoute, + useValue: {snapshot: {queryParams: {}}}, + }, + { + provide: Router, + useValue: {navigate: vi.fn()}, + }, + ], + }) + .overrideComponent(AuditLogsComponent, { + set: { + template: ` + {{ logs()[0]?.description }} + {{ usernameOptions()[1]?.label }} + `, + }, + }) + .compileComponents(); + + fixture = TestBed.createComponent(AuditLogsComponent); + fixture.detectChanges(); + }); + + afterEach(() => { + usernameResponse.complete(); + TestBed.resetTestingModule(); + }); + + it('renders audit-log updates from a background refresh', async () => { + expect(fixture.nativeElement.querySelector('.description-cell').textContent.trim()).toBe('Initial audit log'); + + fixture.componentInstance.loadLogs(false); + await fixture.whenStable(); + + expect(fixture.nativeElement.querySelector('.description-cell').textContent.trim()).toBe('Refreshed audit log'); + }); + + it('renders username options loaded from the service', async () => { + usernameResponse.next(['alice']); + await fixture.whenStable(); + + expect(getDistinctUsernames).toHaveBeenCalled(); + expect(fixture.nativeElement.querySelector('.username-option').textContent.trim()).toBe('alice'); }); }); + +function createResponse(description: string): PageableResponse { + return { + content: [{ + id: 1, + userId: 1, + username: 'alice', + action: 'LOGIN_SUCCESS', + entityType: null, + entityId: null, + description, + ipAddress: '127.0.0.1', + countryCode: 'US', + createdAt: '2026-06-02T18:00:00Z', + }], + page: { + totalElements: 1, + totalPages: 1, + number: 0, + size: 25, + }, + }; +} diff --git a/frontend/src/app/features/settings/audit-logs/audit-logs.component.ts b/frontend/src/app/features/settings/audit-logs/audit-logs.component.ts index f8acfe280..0ee2719b4 100644 --- a/frontend/src/app/features/settings/audit-logs/audit-logs.component.ts +++ b/frontend/src/app/features/settings/audit-logs/audit-logs.component.ts @@ -1,4 +1,4 @@ -import {Component, DestroyRef, inject, OnInit, signal} from '@angular/core'; +import {ChangeDetectionStrategy, Component, DestroyRef, inject, OnInit, signal} from '@angular/core'; import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; import {ActivatedRoute, Router} from '@angular/router'; import {TableLazyLoadEvent, TableModule} from 'primeng/table'; @@ -41,6 +41,7 @@ interface UsernameOption { @Component({ selector: 'app-audit-logs', standalone: true, + changeDetection: ChangeDetectionStrategy.OnPush, imports: [ DatePipe,TableModule, Select, DatePicker, FormsModule, TranslocoDirective, TagComponent], templateUrl: './audit-logs.component.html', @@ -52,8 +53,8 @@ export class AuditLogsComponent implements OnInit { private readonly router = inject(Router); private readonly destroyRef = inject(DestroyRef); - logs: AuditLog[] = []; - totalRecords = 0; + logs = signal([]); + totalRecords = signal(0); rows = 25; loading = signal(false); selectedAction: string | null = null; @@ -63,7 +64,7 @@ export class AuditLogsComponent implements OnInit { autoRefresh = false; private autoRefreshSub?: Subscription; - usernameOptions: UsernameOption[] = [{label: 'All Users', value: ''}]; + usernameOptions = signal([{label: 'All Users', value: ''}]); actionOptions: ActionOption[] = [ {label: 'All Actions', value: ''}, @@ -117,10 +118,10 @@ export class AuditLogsComponent implements OnInit { loadUsernames(): void { this.auditLogService.getDistinctUsernames().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({ next: (usernames) => { - this.usernameOptions = [ + this.usernameOptions.set([ {label: 'All Users', value: ''}, ...usernames.map(u => ({label: u, value: u})) - ]; + ]); } }); } @@ -137,8 +138,8 @@ export class AuditLogsComponent implements OnInit { takeUntilDestroyed(this.destroyRef) ).subscribe({ next: (response) => { - this.logs = response.content; - this.totalRecords = response.page.totalElements; + this.logs.set(response.content); + this.totalRecords.set(response.page.totalElements); this.loading.set(false); }, error: () => { @@ -172,7 +173,7 @@ export class AuditLogsComponent implements OnInit { this.autoRefreshSub?.unsubscribe(); this.autoRefreshSub = interval(10000) .pipe(takeUntilDestroyed(this.destroyRef)) - .pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => this.loadLogs(false)); + .subscribe(() => this.loadLogs(false)); } else { this.autoRefreshSub?.unsubscribe(); }