UNPKG

nested_json_to_csv

Version:
63 lines (59 loc) 2.1 kB
import { IFactory } from "../interfaces/IFactory"; import { IParser } from "../interfaces/IParser"; import { Csv } from "../models/Csv"; import { ParserFactory } from "../providers/factories/ParserFactory"; import { CsvParserService } from "../providers/services/CsvParserService"; describe("Csv test", () => { test("simple single csv to json", () => { const headers = ["id", "name"]; const body = "31245324,fox,"; const csv: Csv = new Csv(headers, body); let parserFactory: IFactory = new ParserFactory(); const parser: IParser = parserFactory.make("csvParser", csv); expect(parser).toBeInstanceOf(CsvParserService); const json = parser.parse(); const eJson = { name: "fox", id: "31245324", }; expect(json).toEqual(eJson); }); test("simple multiple csv to json", () => { const headers = ["id", "name"]; const body = "31245323,fox,\n31245324,fox\n"; const csv: Csv = new Csv(headers, body); let parserFactory: IFactory = new ParserFactory(); const parser: IParser = parserFactory.make("csvParser", csv); expect(parser).toBeInstanceOf(CsvParserService); const json = parser.parse(); const eJson = { "31245323": { name: "fox", id: "31245323" }, "31245324": { name: "fox", id: "31245324" }, }; expect(json).toEqual(eJson); }); test("nested multiple csv to json", () => { const headers = ["id", "uid", "name", "address.city"]; const body = "user,31245323,fox,nowhere,\nchild,31245324,fox,where,\n"; const csv: Csv = new Csv(headers, body); let parserFactory: IFactory = new ParserFactory(); const parser: IParser = parserFactory.make("csvParser", csv); expect(parser).toBeInstanceOf(CsvParserService); const json = parser.parse(); const eJson = { user: { id: "user", name: "fox", uid: "31245323", address: { city: "nowhere" }, }, child: { id: "child", name: "fox", uid: "31245324", address: { city: "where" }, }, }; expect(json).toEqual(eJson); }); });