nested_json_to_csv
Version:
60 lines (59 loc) • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Csv_1 = require("../models/Csv");
const ParserFactory_1 = require("../providers/factories/ParserFactory");
const CsvParserService_1 = require("../providers/services/CsvParserService");
describe("Csv test", () => {
test("simple single csv to json", () => {
const headers = ["id", "name"];
const body = "31245324,fox,";
const csv = new Csv_1.Csv(headers, body);
let parserFactory = new ParserFactory_1.ParserFactory();
const parser = parserFactory.make("csvParser", csv);
expect(parser).toBeInstanceOf(CsvParserService_1.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 = new Csv_1.Csv(headers, body);
let parserFactory = new ParserFactory_1.ParserFactory();
const parser = parserFactory.make("csvParser", csv);
expect(parser).toBeInstanceOf(CsvParserService_1.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 = new Csv_1.Csv(headers, body);
let parserFactory = new ParserFactory_1.ParserFactory();
const parser = parserFactory.make("csvParser", csv);
expect(parser).toBeInstanceOf(CsvParserService_1.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);
});
});