Files
WebApp/src/app/connect/domain/file-source.spec.ts
T

48 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { of, throwError } from 'rxjs';
import { SourceGateway } from '../api/source-gateway';
import { FileConnection } from './file-source';
import { preview } from '../testing/fakes';
describe('FileConnection', () => {
it('exposes one table per Excel sheet, each previewing and loading with its sheet_name', async () => {
const calls: unknown[][] = [];
const gateway = {
file: {
preview: (...args: unknown[]) => {
calls.push(args);
return of(preview({ tables: ['Órdenes', 'Notas'].map((name) => ({ name, options: { sheet_name: name }, kind: 'sheet' })) }));
},
},
} as unknown as SourceGateway;
const connection = new FileConnection(new File(['x'], 'libro.xlsx'), 'excel');
connection.load(gateway).subscribe();
expect(connection.state()).toBe('ready');
expect(connection.tables().map((t) => t.name)).toEqual(['Órdenes', 'Notas']);
expect(connection.tables()[1].label).toBe('libro.xlsx › Notas');
connection.tables()[1].preview(gateway, 'cacu.NotaMedica').subscribe();
expect(calls.at(-1)?.slice(1)).toEqual(['excel', 'cacu.NotaMedica', { sheet_name: 'Notas' }]);
const locator = await connection.tables()[1].locator();
expect(locator).toEqual({ source_type: 'excel', payload: { file_base64: btoa('x'), options: { sheet_name: 'Notas' } } });
});
it('a CSV is a single table labelled by the file name', () => {
const gateway = { file: { preview: () => of(preview()) } } as unknown as SourceGateway;
const connection = new FileConnection(new File(['a,b'], 'p.csv'), 'csv');
connection.load(gateway).subscribe();
expect(connection.singleTable).toBe(true);
expect(connection.tables().map((t) => t.label)).toEqual(['p.csv']);
});
it('a file that fails to parse lands in the error state with the backend message', () => {
const gateway = { file: { preview: () => throwError(() => ({ error: { error: 'could not parse CSV' } })) } } as unknown as SourceGateway;
const connection = new FileConnection(new File(['?'], 'x.csv'), 'csv');
connection.load(gateway).subscribe();
expect(connection.state()).toBe('error');
expect(connection.error()).toBe('could not parse CSV');
expect(connection.tables()).toEqual([]);
});
});