127 lines
5.1 KiB
TypeScript
127 lines
5.1 KiB
TypeScript
import { TestBed } from '@angular/core/testing';
|
|
import { FileDropzone } from './file-dropzone';
|
|
|
|
describe('FileDropzone', () => {
|
|
function createFixture(keepSelection = true) {
|
|
const fixture = TestBed.createComponent(FileDropzone);
|
|
fixture.componentRef.setInput('accept', ['xlsx', 'csv', 'json', 'pdf']);
|
|
fixture.componentRef.setInput('keepSelection', keepSelection);
|
|
fixture.detectChanges();
|
|
return fixture;
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
await TestBed.configureTestingModule({ imports: [FileDropzone] }).compileComponents();
|
|
});
|
|
|
|
it('accepts valid files and rejects invalid extension, empty, and duplicate files', () => {
|
|
const fixture = createFixture();
|
|
const compiled = fixture.nativeElement as HTMLElement;
|
|
const validFile = new File(['contenido'], 'pacientes.XLSX', { type: 'application/octet-stream' });
|
|
const csvFile = new File(['a,b'], 'consultas.csv', { type: 'text/csv' });
|
|
const invalidFile = new File(['x'], 'documento.docx');
|
|
const emptyFile = new File([], 'vacio.csv');
|
|
|
|
const fileInput = compiled.querySelector<HTMLInputElement>('input[type="file"]')!;
|
|
Object.defineProperty(fileInput, 'files', {
|
|
configurable: true,
|
|
value: [validFile, csvFile, invalidFile, emptyFile],
|
|
});
|
|
fileInput.dispatchEvent(new Event('change', { bubbles: true }));
|
|
fixture.detectChanges();
|
|
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(2);
|
|
expect(compiled.querySelector('.file-messages')?.textContent).toContain(
|
|
'documento.docx no fue agregado. Formato no compatible.',
|
|
);
|
|
expect(compiled.querySelector('.file-messages')?.textContent).toContain(
|
|
'vacio.csv no fue agregado. El archivo está vacío.',
|
|
);
|
|
});
|
|
|
|
it('rejects a duplicate file (same name + size) already selected', () => {
|
|
const fixture = createFixture();
|
|
const compiled = fixture.nativeElement as HTMLElement;
|
|
const validFile = new File(['contenido'], 'pacientes.xlsx', { type: 'application/octet-stream' });
|
|
const fileInput = compiled.querySelector<HTMLInputElement>('input[type="file"]')!;
|
|
|
|
const selectFiles = (files: File[]) => {
|
|
Object.defineProperty(fileInput, 'files', { configurable: true, value: files });
|
|
fileInput.dispatchEvent(new Event('change', { bubbles: true }));
|
|
fixture.detectChanges();
|
|
};
|
|
|
|
selectFiles([validFile]);
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(1);
|
|
selectFiles([validFile]);
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(1);
|
|
expect(compiled.querySelector('.file-messages')?.textContent).toContain(
|
|
'pacientes.xlsx ya se encuentra seleccionado.',
|
|
);
|
|
});
|
|
|
|
it('supports drag and drop and removing a file, and emits filesChange', () => {
|
|
const fixture = createFixture();
|
|
const compiled = fixture.nativeElement as HTMLElement;
|
|
const component = fixture.componentInstance;
|
|
const emitted: File[][] = [];
|
|
component.filesChange.subscribe((files) => emitted.push(files));
|
|
|
|
const jsonFile = new File(['{}'], 'nuevo.json', { type: 'application/json' });
|
|
component.onDrop({
|
|
preventDefault: () => undefined,
|
|
dataTransfer: { files: [jsonFile] },
|
|
} as unknown as DragEvent);
|
|
fixture.detectChanges();
|
|
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(1);
|
|
expect(emitted.at(-1)).toEqual([jsonFile]);
|
|
|
|
compiled.querySelector<HTMLButtonElement>('.file-row__remove')!.click();
|
|
fixture.detectChanges();
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(0);
|
|
expect(emitted.at(-1)).toEqual([]);
|
|
});
|
|
|
|
it('without keepSelection emits only each new batch and lists nothing', () => {
|
|
const fixture = createFixture(false);
|
|
const compiled = fixture.nativeElement as HTMLElement;
|
|
const component = fixture.componentInstance;
|
|
const emitted: File[][] = [];
|
|
component.filesChange.subscribe((files) => emitted.push(files));
|
|
const drop = (files: File[]) =>
|
|
component.onDrop({ preventDefault: () => undefined, dataTransfer: { files } } as unknown as DragEvent);
|
|
|
|
const first = new File(['a'], 'uno.csv');
|
|
const second = new File(['b'], 'dos.csv');
|
|
drop([first]);
|
|
drop([second]);
|
|
drop([new File(['x'], 'malo.docx')]);
|
|
fixture.detectChanges();
|
|
|
|
expect(emitted).toEqual([[first], [second]]);
|
|
expect(compiled.querySelectorAll('.file-row')).toHaveLength(0);
|
|
});
|
|
|
|
it('honors a custom accept input', () => {
|
|
const fixture = createFixture();
|
|
expect(fixture.componentInstance.allowedExtensions.has('pdf')).toBe(true);
|
|
|
|
fixture.componentRef.setInput('accept', ['csv']);
|
|
fixture.detectChanges();
|
|
expect(fixture.componentInstance.allowedExtensions.has('pdf')).toBe(false);
|
|
expect(fixture.componentInstance.allowedExtensions.has('csv')).toBe(true);
|
|
});
|
|
|
|
it('reflects drag-over state via dragenter/dragleave', () => {
|
|
const fixture = createFixture();
|
|
const component = fixture.componentInstance;
|
|
const preventDefault = () => undefined;
|
|
|
|
component.onDragEnter({ preventDefault } as unknown as DragEvent);
|
|
expect(component.isDragOver()).toBe(true);
|
|
component.onDragLeave({ preventDefault } as unknown as DragEvent);
|
|
expect(component.isDragOver()).toBe(false);
|
|
});
|
|
});
|