fix(audit-logs): refresh table reactively (#1611)
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
class="action-filter">
|
||||
</p-select>
|
||||
<p-select
|
||||
[options]="usernameOptions"
|
||||
[options]="usernameOptions()"
|
||||
[(ngModel)]="selectedUsername"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
@@ -62,12 +62,12 @@
|
||||
</div>
|
||||
<div class="section-body">
|
||||
<p-table
|
||||
[value]="logs"
|
||||
[value]="logs()"
|
||||
[lazy]="true"
|
||||
[paginator]="true"
|
||||
[rows]="rows"
|
||||
[first]="currentPage * rows"
|
||||
[totalRecords]="totalRecords"
|
||||
[totalRecords]="totalRecords()"
|
||||
[loading]="loading()"
|
||||
[showCurrentPageReport]="true"
|
||||
[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,
|
||||
// Prime table lazy-load events, interval-based auto-refresh, and router writes so the audit log
|
||||
// page can be exercised without a brittle integration harness for timers and navigation state.
|
||||
describe.skip('AuditLogsComponent', () => {
|
||||
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<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 {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<AuditLog[]>([]);
|
||||
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<UsernameOption[]>([{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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user