refactor(books): refresh paginated cache after legacy actions (#2222)

This commit is contained in:
Alex
2026-09-21 13:01:54 +01:00
committed by GitHub
parent 79ebf13be3
commit 49cc7add81
16 changed files with 216 additions and 16 deletions
@@ -1,22 +1,28 @@
import {provideHttpClient} from '@angular/common/http';
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
import {TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {QueryClient} from '@tanstack/angular-query-experimental';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {API_CONFIG} from '../../core/config/api-config';
import {bookQueryKeys} from '../../features/book/data/book-query-keys';
import {BOOKS_QUERY_KEY} from '../../features/book/service/book-query-keys';
import {MetadataMatchWeightsService} from './metadata-match-weights.service';
describe('MetadataMatchWeightsService', () => {
let service: MetadataMatchWeightsService;
let httpTestingController: HttpTestingController;
let queryClient: QueryClient;
beforeEach(() => {
TestBed.resetTestingModule();
queryClient = new QueryClient();
TestBed.configureTestingModule({
providers: [
provideHttpClient(),
provideHttpClientTesting(),
MetadataMatchWeightsService,
{provide: QueryClient, useValue: queryClient},
]
});
@@ -26,6 +32,7 @@ describe('MetadataMatchWeightsService', () => {
afterEach(() => {
httpTestingController.verify();
queryClient.clear();
TestBed.resetTestingModule();
});
@@ -41,4 +48,17 @@ describe('MetadataMatchWeightsService', () => {
request.flush(null);
});
it('refreshes book caches once recalculated scores are saved', () => {
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries').mockResolvedValue(undefined);
service.recalculateAll().subscribe();
httpTestingController.expectOne(
`${API_CONFIG.BASE_URL}/api/v1/books/metadata/recalculate-match-scores`
).flush(null);
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: BOOKS_QUERY_KEY, exact: true});
expect(invalidateQueriesSpy).toHaveBeenCalledWith({queryKey: bookQueryKeys.all()});
});
});
@@ -1,15 +1,20 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Observable, tap } from 'rxjs';
import { QueryClient } from '@tanstack/angular-query-experimental';
import { API_CONFIG } from '../../core/config/api-config';
import { invalidateAllBookCaches } from '../../features/book/service/legacy-book-cache';
@Injectable({ providedIn: 'root' })
export class MetadataMatchWeightsService {
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1`;
private http = inject(HttpClient);
private queryClient = inject(QueryClient);
recalculateAll(): Observable<void> {
return this.http.post<void>(`${this.baseUrl}/books/metadata/recalculate-match-scores`, {});
return this.http.post<void>(`${this.baseUrl}/books/metadata/recalculate-match-scores`, {}).pipe(
tap(() => invalidateAllBookCaches(this.queryClient))
);
}
}