fix(ui): settings forms not reflecting saved values reliably (#648)
This commit is contained in:
+92
-31
@@ -1,3 +1,4 @@
|
||||
import {signal, type WritableSignal} from '@angular/core';
|
||||
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||
import {of, throwError} from 'rxjs';
|
||||
@@ -5,6 +6,7 @@ import {of, throwError} from 'rxjs';
|
||||
import {MessageService} from 'primeng/api';
|
||||
|
||||
import {getTranslocoModule} from '../../../../core/testing/transloco-testing';
|
||||
import {type AppSettings, type MetadataMatchWeights} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {MetadataMatchWeightsService} from '../../../../shared/service/metadata-match-weights.service';
|
||||
import {AppSettingKey} from '../../../../shared/model/app-settings.model';
|
||||
@@ -16,11 +18,13 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
let saveSettings: ReturnType<typeof vi.fn>;
|
||||
let recalculateAll: ReturnType<typeof vi.fn>;
|
||||
let messageService: {add: ReturnType<typeof vi.fn>};
|
||||
let appSettingsSignal: WritableSignal<AppSettings | null>;
|
||||
|
||||
beforeEach(async () => {
|
||||
saveSettings = vi.fn(() => of(void 0));
|
||||
recalculateAll = vi.fn(() => of(void 0));
|
||||
messageService = {add: vi.fn()};
|
||||
appSettingsSignal = signal<AppSettings | null>(null);
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MetadataMatchWeightsComponent, getTranslocoModule()],
|
||||
@@ -28,36 +32,7 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
{
|
||||
provide: AppSettingsService,
|
||||
useValue: {
|
||||
appSettings: () => ({
|
||||
metadataMatchWeights: {
|
||||
title: 10,
|
||||
subtitle: 5,
|
||||
description: 3,
|
||||
publisher: 2,
|
||||
publishedDate: 1,
|
||||
authors: 9,
|
||||
categories: 4,
|
||||
seriesName: 6,
|
||||
seriesNumber: 2,
|
||||
seriesTotal: 1,
|
||||
isbn13: 8,
|
||||
isbn10: 7,
|
||||
pageCount: 2,
|
||||
language: 1,
|
||||
amazonRating: 2,
|
||||
amazonReviewCount: 2,
|
||||
goodreadsRating: 2,
|
||||
goodreadsReviewCount: 2,
|
||||
hardcoverRating: 2,
|
||||
hardcoverReviewCount: 2,
|
||||
doubanRating: 2,
|
||||
doubanReviewCount: 2,
|
||||
ranobedbRating: 2,
|
||||
audibleRating: 2,
|
||||
audibleReviewCount: 2,
|
||||
coverImage: 3,
|
||||
},
|
||||
}),
|
||||
appSettings: appSettingsSignal,
|
||||
saveSettings,
|
||||
},
|
||||
},
|
||||
@@ -68,7 +43,6 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
|
||||
fixture = TestBed.createComponent(MetadataMatchWeightsComponent);
|
||||
component = fixture.componentInstance;
|
||||
component.ngOnInit();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -77,12 +51,63 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
});
|
||||
|
||||
it('hydrates the reactive form from saved match weights', () => {
|
||||
appSettingsSignal.set(buildSettings());
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(10);
|
||||
expect(component.form.get('authors')?.value).toBe(9);
|
||||
expect(component.orderedKeys[0]).toBe('title');
|
||||
});
|
||||
|
||||
it('hydrates the reactive form when weights resolve after initial render', () => {
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(0);
|
||||
|
||||
appSettingsSignal.set(buildSettings());
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(10);
|
||||
expect(component.form.get('authors')?.value).toBe(9);
|
||||
});
|
||||
|
||||
it('hydrates pristine controls when weights resolve after a user edit', () => {
|
||||
fixture.detectChanges();
|
||||
|
||||
const titleControl = component.form.get('title');
|
||||
titleControl?.setValue(42);
|
||||
titleControl?.markAsDirty();
|
||||
|
||||
appSettingsSignal.set(buildSettings());
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(42);
|
||||
expect(component.form.get('authors')?.value).toBe(9);
|
||||
expect(component.form.get('subtitle')?.value).toBe(5);
|
||||
});
|
||||
|
||||
it('keeps dirty controls while refreshing pristine controls on later settings updates', () => {
|
||||
appSettingsSignal.set(buildSettings());
|
||||
fixture.detectChanges();
|
||||
|
||||
const titleControl = component.form.get('title');
|
||||
titleControl?.setValue(42);
|
||||
titleControl?.markAsDirty();
|
||||
|
||||
appSettingsSignal.set(buildSettings({
|
||||
title: 17,
|
||||
authors: 12,
|
||||
subtitle: 8,
|
||||
}));
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(component.form.get('title')?.value).toBe(42);
|
||||
expect(component.form.get('authors')?.value).toBe(12);
|
||||
expect(component.form.get('subtitle')?.value).toBe(8);
|
||||
});
|
||||
|
||||
it('persists valid form values through app settings', () => {
|
||||
fixture.detectChanges();
|
||||
component.save();
|
||||
|
||||
expect(saveSettings).toHaveBeenCalledWith([
|
||||
@@ -96,6 +121,7 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
});
|
||||
|
||||
it('does not save invalid forms', () => {
|
||||
fixture.detectChanges();
|
||||
component.form.get('title')?.setValue(-1);
|
||||
|
||||
component.save();
|
||||
@@ -104,6 +130,7 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
});
|
||||
|
||||
it('reports recalculate failures and resets the loading flag', () => {
|
||||
fixture.detectChanges();
|
||||
recalculateAll.mockReturnValueOnce(throwError(() => new Error('boom')));
|
||||
|
||||
component.recalculate();
|
||||
@@ -112,3 +139,37 @@ describe('MetadataMatchWeightsComponent', () => {
|
||||
expect(component.isRecalculating).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
function buildSettings(overrides: Partial<MetadataMatchWeights> = {}): AppSettings {
|
||||
return {
|
||||
metadataMatchWeights: {
|
||||
title: 10,
|
||||
subtitle: 5,
|
||||
description: 3,
|
||||
publisher: 2,
|
||||
publishedDate: 1,
|
||||
authors: 9,
|
||||
categories: 4,
|
||||
seriesName: 6,
|
||||
seriesNumber: 2,
|
||||
seriesTotal: 1,
|
||||
isbn13: 8,
|
||||
isbn10: 7,
|
||||
pageCount: 2,
|
||||
language: 1,
|
||||
amazonRating: 2,
|
||||
amazonReviewCount: 2,
|
||||
goodreadsRating: 2,
|
||||
goodreadsReviewCount: 2,
|
||||
hardcoverRating: 2,
|
||||
hardcoverReviewCount: 2,
|
||||
doubanRating: 2,
|
||||
doubanReviewCount: 2,
|
||||
ranobedbRating: 2,
|
||||
audibleRating: 2,
|
||||
audibleReviewCount: 2,
|
||||
coverImage: 3,
|
||||
...overrides,
|
||||
},
|
||||
} as AppSettings;
|
||||
}
|
||||
|
||||
+24
-11
@@ -1,10 +1,10 @@
|
||||
import {Component, DestroyRef, inject, OnInit} from '@angular/core';
|
||||
import {Component, DestroyRef, effect, inject} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {FormBuilder, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {MetadataMatchWeightsService} from '../../../../shared/service/metadata-match-weights.service';
|
||||
import {Button} from 'primeng/button';
|
||||
import {AppSettingKey} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingKey, MetadataMatchWeights} from '../../../../shared/model/app-settings.model';
|
||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||
import {InputNumber} from 'primeng/inputnumber';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
@@ -20,7 +20,7 @@ import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
templateUrl: './metadata-match-weights-component.html',
|
||||
styleUrl: './metadata-match-weights-component.scss'
|
||||
})
|
||||
export class MetadataMatchWeightsComponent implements OnInit {
|
||||
export class MetadataMatchWeightsComponent {
|
||||
|
||||
readonly orderedFieldKeys: string[] = [
|
||||
'title', 'subtitle', 'authors', 'description', 'publisher', 'publishedDate',
|
||||
@@ -31,19 +31,26 @@ export class MetadataMatchWeightsComponent implements OnInit {
|
||||
'doubanRating', 'doubanReviewCount', 'ranobedbRating'
|
||||
];
|
||||
|
||||
form!: FormGroup;
|
||||
private fb = inject(FormBuilder);
|
||||
form: FormGroup = this.buildForm();
|
||||
isSaving = false;
|
||||
isRecalculating = false;
|
||||
|
||||
private weightsService = inject(MetadataMatchWeightsService);
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private messageService = inject(MessageService);
|
||||
private fb = inject(FormBuilder);
|
||||
private t = inject(TranslocoService);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
|
||||
ngOnInit(): void {
|
||||
this.form = this.fb.group({
|
||||
private readonly syncSettingsEffect = effect(() => {
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
if (settings?.metadataMatchWeights) {
|
||||
this.patchPristineControls(settings.metadataMatchWeights);
|
||||
}
|
||||
});
|
||||
|
||||
private buildForm(): FormGroup {
|
||||
return this.fb.group({
|
||||
title: [0, [Validators.required, Validators.min(0)]],
|
||||
subtitle: [0, [Validators.required, Validators.min(0)]],
|
||||
description: [0, [Validators.required, Validators.min(0)]],
|
||||
@@ -71,10 +78,6 @@ export class MetadataMatchWeightsComponent implements OnInit {
|
||||
audibleReviewCount: [0, [Validators.required, Validators.min(0)]],
|
||||
coverImage: [0, [Validators.required, Validators.min(0)]],
|
||||
});
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
if (settings?.metadataMatchWeights) {
|
||||
this.form.patchValue(settings.metadataMatchWeights);
|
||||
}
|
||||
}
|
||||
|
||||
get orderedKeys(): string[] {
|
||||
@@ -101,6 +104,7 @@ export class MetadataMatchWeightsComponent implements OnInit {
|
||||
takeUntilDestroyed(this.destroyRef)
|
||||
).subscribe({
|
||||
next: () => {
|
||||
this.form.markAsPristine();
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: this.t.translate('common.success'),
|
||||
@@ -142,4 +146,13 @@ export class MetadataMatchWeightsComponent implements OnInit {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private patchPristineControls(weights: MetadataMatchWeights): void {
|
||||
Object.entries(weights).forEach(([key, value]) => {
|
||||
const control = this.form.get(key);
|
||||
if (control?.pristine) {
|
||||
control.patchValue(value, {emitEvent: false});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user