1 import { readCSV } from './csv.reader';
2 import { ImportReaderError } from './error';
4 type CSVItem = { id: string; name: string };
6 describe('readCSV', () => {
7 it('should read CSV data with expected headers', async () => {
8 const csvContent = 'id,name\n1,John Doe\n2,Jane Doe\n3,Bob Smith\n';
9 const result = await readCSV<CSVItem>({ data: csvContent, headers: ['id', 'name'] });
11 expect(result.items).toEqual([
12 { id: '1', name: 'John Doe' },
13 { id: '2', name: 'Jane Doe' },
14 { id: '3', name: 'Bob Smith' },
17 expect(result.ignored).toEqual([]);
20 it('should read CSV data with extra headers not in expected headers', async () => {
21 const csvContent = 'id,name,age\n1,John Doe,20\n2,Jane Doe,21\n3,Bob Smith,22\n';
22 const result = await readCSV<CSVItem>({ data: csvContent, headers: ['id', 'name'] });
24 expect(result.items).toEqual([
25 { id: '1', name: 'John Doe', age: '20' },
26 { id: '2', name: 'Jane Doe', age: '21' },
27 { id: '3', name: 'Bob Smith', age: '22' },
29 expect(result.ignored).toEqual([]);
32 it('should throw error if CSV is empty', async () => {
33 await expect(readCSV<CSVItem>({ data: '', headers: [] })).rejects.toThrow(
34 new ImportReaderError('Empty CSV file')
36 await expect(readCSV<CSVItem>({ data: 'id,name', headers: [] })).rejects.toThrow(
37 new ImportReaderError('Empty CSV file')
41 it('should throw error if CSV file has missing headers', async () => {
42 const csvContent = 'id\n1\n2\n3\n';
43 await expect(readCSV<CSVItem>({ data: csvContent, headers: ['id', 'name'] })).rejects.toThrow(
44 new ImportReaderError('CSV file is missing expected headers: name')
48 it('should hydrate ignored array if some entries are invalid', async () => {
49 const csvContent = 'id,name\n1,John Doe\n2\n3,Bob Smith\n';
50 const result = await readCSV<CSVItem>({ data: csvContent, headers: ['id', 'name'] });
52 expect(result.items).toEqual([
53 { id: '1', name: 'John Doe' },
54 { id: '3', name: 'Bob Smith' },
57 expect(result.ignored).toEqual([{ id: '2' }]);