fix(audit-logs): refresh table reactively (#1611)
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
class="action-filter">
|
class="action-filter">
|
||||||
</p-select>
|
</p-select>
|
||||||
<p-select
|
<p-select
|
||||||
[options]="usernameOptions"
|
[options]="usernameOptions()"
|
||||||
[(ngModel)]="selectedUsername"
|
[(ngModel)]="selectedUsername"
|
||||||
optionLabel="label"
|
optionLabel="label"
|
||||||
optionValue="value"
|
optionValue="value"
|
||||||
@@ -62,12 +62,12 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="section-body">
|
<div class="section-body">
|
||||||
<p-table
|
<p-table
|
||||||
[value]="logs"
|
[value]="logs()"
|
||||||
[lazy]="true"
|
[lazy]="true"
|
||||||
[paginator]="true"
|
[paginator]="true"
|
||||||
[rows]="rows"
|
[rows]="rows"
|
||||||
[first]="currentPage * rows"
|
[first]="currentPage * rows"
|
||||||
[totalRecords]="totalRecords"
|
[totalRecords]="totalRecords()"
|
||||||
[loading]="loading()"
|
[loading]="loading()"
|
||||||
[showCurrentPageReport]="true"
|
[showCurrentPageReport]="true"
|
||||||
[currentPageReportTemplate]="t('pageReport')"
|
[currentPageReportTemplate]="t('pageReport')"
|
||||||
|
|||||||
@@ -1,10 +1,106 @@
|
|||||||
import {describe, it} from 'vitest';
|
import {provideZonelessChangeDetection} from '@angular/core';
|
||||||
|
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
|
import {ActivatedRoute, Router} from '@angular/router';
|
||||||
|
import {of, Subject} from 'rxjs';
|
||||||
|
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||||
|
|
||||||
// NOTE(frontend-seam): Real coverage here needs seams around route query-param restoration,
|
import {getTranslocoModule} from '../../../core/testing/transloco-testing';
|
||||||
// Prime table lazy-load events, interval-based auto-refresh, and router writes so the audit log
|
import {AuditLog, AuditLogService, PageableResponse} from './audit-log.service';
|
||||||
// page can be exercised without a brittle integration harness for timers and navigation state.
|
import {AuditLogsComponent} from './audit-logs.component';
|
||||||
describe.skip('AuditLogsComponent', () => {
|
|
||||||
it('needs route and timer seams to verify filter restoration, pagination, and auto-refresh behavior', () => {
|
describe('AuditLogsComponent', () => {
|
||||||
// TODO(seam): Cover query-param sync, table lazy loading, and interval refresh once route/timer dependencies are isolated.
|
let fixture: ComponentFixture<AuditLogsComponent>;
|
||||||
|
let usernameResponse: Subject<string[]>;
|
||||||
|
const getAuditLogs = vi.fn();
|
||||||
|
const getDistinctUsernames = vi.fn();
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
usernameResponse = new Subject<string[]>();
|
||||||
|
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: `
|
||||||
|
<span class="description-cell">{{ logs()[0]?.description }}</span>
|
||||||
|
<span class="username-option">{{ usernameOptions()[1]?.label }}</span>
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.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<AuditLog> {
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||||
import {ActivatedRoute, Router} from '@angular/router';
|
import {ActivatedRoute, Router} from '@angular/router';
|
||||||
import {TableLazyLoadEvent, TableModule} from 'primeng/table';
|
import {TableLazyLoadEvent, TableModule} from 'primeng/table';
|
||||||
@@ -41,6 +41,7 @@ interface UsernameOption {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'app-audit-logs',
|
selector: 'app-audit-logs',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
imports: [
|
imports: [
|
||||||
DatePipe,TableModule, Select, DatePicker, FormsModule, TranslocoDirective, TagComponent],
|
DatePipe,TableModule, Select, DatePicker, FormsModule, TranslocoDirective, TagComponent],
|
||||||
templateUrl: './audit-logs.component.html',
|
templateUrl: './audit-logs.component.html',
|
||||||
@@ -52,8 +53,8 @@ export class AuditLogsComponent implements OnInit {
|
|||||||
private readonly router = inject(Router);
|
private readonly router = inject(Router);
|
||||||
private readonly destroyRef = inject(DestroyRef);
|
private readonly destroyRef = inject(DestroyRef);
|
||||||
|
|
||||||
logs: AuditLog[] = [];
|
logs = signal<AuditLog[]>([]);
|
||||||
totalRecords = 0;
|
totalRecords = signal(0);
|
||||||
rows = 25;
|
rows = 25;
|
||||||
loading = signal(false);
|
loading = signal(false);
|
||||||
selectedAction: string | null = null;
|
selectedAction: string | null = null;
|
||||||
@@ -63,7 +64,7 @@ export class AuditLogsComponent implements OnInit {
|
|||||||
autoRefresh = false;
|
autoRefresh = false;
|
||||||
private autoRefreshSub?: Subscription;
|
private autoRefreshSub?: Subscription;
|
||||||
|
|
||||||
usernameOptions: UsernameOption[] = [{label: 'All Users', value: ''}];
|
usernameOptions = signal<UsernameOption[]>([{label: 'All Users', value: ''}]);
|
||||||
|
|
||||||
actionOptions: ActionOption[] = [
|
actionOptions: ActionOption[] = [
|
||||||
{label: 'All Actions', value: ''},
|
{label: 'All Actions', value: ''},
|
||||||
@@ -117,10 +118,10 @@ export class AuditLogsComponent implements OnInit {
|
|||||||
loadUsernames(): void {
|
loadUsernames(): void {
|
||||||
this.auditLogService.getDistinctUsernames().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
this.auditLogService.getDistinctUsernames().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||||
next: (usernames) => {
|
next: (usernames) => {
|
||||||
this.usernameOptions = [
|
this.usernameOptions.set([
|
||||||
{label: 'All Users', value: ''},
|
{label: 'All Users', value: ''},
|
||||||
...usernames.map(u => ({label: u, value: u}))
|
...usernames.map(u => ({label: u, value: u}))
|
||||||
];
|
]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -137,8 +138,8 @@ export class AuditLogsComponent implements OnInit {
|
|||||||
takeUntilDestroyed(this.destroyRef)
|
takeUntilDestroyed(this.destroyRef)
|
||||||
).subscribe({
|
).subscribe({
|
||||||
next: (response) => {
|
next: (response) => {
|
||||||
this.logs = response.content;
|
this.logs.set(response.content);
|
||||||
this.totalRecords = response.page.totalElements;
|
this.totalRecords.set(response.page.totalElements);
|
||||||
this.loading.set(false);
|
this.loading.set(false);
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
@@ -172,7 +173,7 @@ export class AuditLogsComponent implements OnInit {
|
|||||||
this.autoRefreshSub?.unsubscribe();
|
this.autoRefreshSub?.unsubscribe();
|
||||||
this.autoRefreshSub = interval(10000)
|
this.autoRefreshSub = interval(10000)
|
||||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||||
.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => this.loadLogs(false));
|
.subscribe(() => this.loadLogs(false));
|
||||||
} else {
|
} else {
|
||||||
this.autoRefreshSub?.unsubscribe();
|
this.autoRefreshSub?.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user