117 lines
4.3 KiB
TypeScript
117 lines
4.3 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { Observable, of } from 'rxjs';
|
|
import { PdfSourceApi } from '../../api/pdf-source.api';
|
|
import { PdfPageViewer } from './pdf-page-viewer';
|
|
|
|
class FakePdfSourceApi {
|
|
readonly calls: number[] = [];
|
|
|
|
pageImage(_file: File, pageNumber: number): Observable<Blob> {
|
|
this.calls.push(pageNumber);
|
|
return of(new Blob([`page-${pageNumber}`], { type: 'image/png' }));
|
|
}
|
|
}
|
|
|
|
describe('PdfPageViewer', () => {
|
|
let fakeApi: FakePdfSourceApi;
|
|
let createObjectURL: ReturnType<typeof vi.spyOn>;
|
|
let revokeObjectURL: ReturnType<typeof vi.spyOn>;
|
|
let urlCounter = 0;
|
|
|
|
beforeEach(async () => {
|
|
fakeApi = new FakePdfSourceApi();
|
|
urlCounter = 0;
|
|
createObjectURL = vi.spyOn(URL, 'createObjectURL').mockImplementation(() => `blob:mock-${++urlCounter}`);
|
|
revokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
|
|
|
|
await TestBed.configureTestingModule({
|
|
imports: [PdfPageViewer],
|
|
providers: [{ provide: PdfSourceApi, useValue: fakeApi }],
|
|
}).compileComponents();
|
|
});
|
|
|
|
afterEach(() => {
|
|
createObjectURL.mockRestore();
|
|
revokeObjectURL.mockRestore();
|
|
});
|
|
|
|
function createFixture(pageNumber = 1, pageCount = 3) {
|
|
const fixture = TestBed.createComponent(PdfPageViewer);
|
|
fixture.componentRef.setInput('file', new File(['%PDF'], 'doc.pdf', { type: 'application/pdf' }));
|
|
fixture.componentRef.setInput('pageNumber', pageNumber);
|
|
fixture.componentRef.setInput('pageCount', pageCount);
|
|
fixture.detectChanges();
|
|
return fixture;
|
|
}
|
|
|
|
it('fetches and displays page 1 on init via an object URL', () => {
|
|
const fixture = createFixture(1);
|
|
const component = fixture.componentInstance;
|
|
|
|
expect(fakeApi.calls).toEqual([1]);
|
|
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
|
expect(component.imageUrl()).toBe('blob:mock-1');
|
|
});
|
|
|
|
it('revokes the previous object URL and creates a new one when navigating to a new page', () => {
|
|
const fixture = createFixture(1);
|
|
fixture.componentRef.setInput('pageNumber', 2);
|
|
fixture.detectChanges();
|
|
|
|
expect(fakeApi.calls).toEqual([1, 2]);
|
|
expect(createObjectURL).toHaveBeenCalledTimes(2);
|
|
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-1');
|
|
expect(fixture.componentInstance.imageUrl()).toBe('blob:mock-2');
|
|
});
|
|
|
|
it('does not refetch an already-fetched page, but still swaps to a fresh object URL for it', () => {
|
|
const fixture = createFixture(1);
|
|
fixture.componentRef.setInput('pageNumber', 2);
|
|
fixture.detectChanges();
|
|
fixture.componentRef.setInput('pageNumber', 1);
|
|
fixture.detectChanges();
|
|
|
|
// Only two distinct pages were ever fetched from the API, even though
|
|
// page 1 was visited twice.
|
|
expect(fakeApi.calls).toEqual([1, 2]);
|
|
// But a fresh object URL is still minted from the cached Blob each time
|
|
// the page is (re)displayed, and the previously-shown one is revoked.
|
|
expect(createObjectURL).toHaveBeenCalledTimes(3);
|
|
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-2');
|
|
expect(fixture.componentInstance.imageUrl()).toBe('blob:mock-3');
|
|
});
|
|
|
|
it('revokes the current object URL on destroy', () => {
|
|
const fixture = createFixture(1);
|
|
fixture.destroy();
|
|
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock-1');
|
|
});
|
|
|
|
it('emits pageNumberChange from the pager buttons, clamped to [1, pageCount]', () => {
|
|
const fixture = createFixture(2, 3);
|
|
const component = fixture.componentInstance;
|
|
const emitted: number[] = [];
|
|
component.pageNumberChange.subscribe((n) => emitted.push(n));
|
|
|
|
component.next();
|
|
component.previous();
|
|
fixture.componentRef.setInput('pageNumber', 1);
|
|
fixture.detectChanges();
|
|
component.previous(); // no-op, already at page 1
|
|
|
|
expect(emitted).toEqual([3, 1]);
|
|
});
|
|
|
|
it('filters regions to only those whose page_numbers include the current page', () => {
|
|
const fixture = createFixture(1);
|
|
fixture.componentRef.setInput('regions', [
|
|
{ target_field: 'a', page_numbers: [1], bbox: [0, 0, 0.5, 0.5] },
|
|
{ target_field: 'b', page_numbers: [2], bbox: [0, 0, 0.5, 0.5] },
|
|
{ target_field: 'c', page_numbers: [1, 2], bbox: [0, 0, 0.5, 0.5] },
|
|
]);
|
|
fixture.detectChanges();
|
|
|
|
expect(fixture.componentInstance.regionsOnCurrentPage().map((r) => r.target_field)).toEqual(['a', 'c']);
|
|
});
|
|
});
|