Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import {} from 'ts-jest';
  2. import * as parse from 'csv-parse';
  3. import {StreamReadCallback, StreamReadable, streamReadable} from './Readable'
  4. import {CsvRow, csvStreamReadApplyCallbacks} from './Readable_Csv'
  5. import { testing } from 'bs-logger';
  6.  
  7. interface SpecificCSVRow {
  8. first: string
  9. second: string
  10. }
  11.  
  12. describe.skip('CSV implementation', () => {
  13. const columns = ['first', 'second', 'third'];
  14. let parser: parse.Parser = parse(`
  15. "1","2",3
  16. "a","b","c"
  17. `, {
  18. trim: true,
  19. skip_empty_lines: true,
  20. columns
  21. })
  22.  
  23. test('Chain several callbacks for CSV reading stream', () => {
  24. let genericCsvCallback: StreamReadCallback<CsvRow> = (row: CsvRow) => {
  25. expect(Object.keys(row)).toEqual(columns)
  26. }
  27. let specificCsvCallback: StreamReadCallback<SpecificCSVRow> = (row: SpecificCSVRow) => {
  28. expect(typeof row.first).toEqual('string')
  29. expect(typeof row.second).toEqual('string')
  30. }
  31.  
  32. let memoryCollection: object[] = [];
  33. let memoryCallback: StreamReadCallback<CsvRow> = (row: object) => memoryCollection.push(row);
  34. const streamReadableCsv: StreamReadable<parse.Parser, object, parse.Parser> = streamReadable;
  35. streamReadableCsv(parser, csvStreamReadApplyCallbacks)(genericCsvCallback)(specificCsvCallback)(memoryCallback)().on('end', () => {
  36. expect(memoryCollection).toEqual([
  37. {first: '1', second: '2', third: '3'},
  38. {first: 'a', second: 'b', third: 'c'}
  39. ]);
  40. });
  41. })
  42. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement