UNPKG

nested_json_to_csv

Version:
46 lines (45 loc) 2.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Csv_1 = require("../models/Csv"); const ParserFactory_1 = require("../providers/factories/ParserFactory"); const FileSystemService_1 = require("../providers/services/FileSystemService"); const fs_1 = __importDefault(require("fs")); describe("FileIO test", () => { test("Create a csv file", (done) => { const json = { user: { name: "fox", uid: "31245324", address: { city: "nowhere" } }, child: { name: "fox", uid: "31245324", address: { city: "where" } }, }; let parserFactory = new ParserFactory_1.ParserFactory(); const parser = parserFactory.make("jsonParser", json, { hasId: true, single: false, }); const headers = ["uid", "name", "address.city"]; const csv = parser.parse(headers); const data = csv.make(); const fileSystemService = new FileSystemService_1.FileSystemService("data.csv", "./Data"); fileSystemService.write(data).then(() => { const exists = fs_1.default.existsSync(fileSystemService.getFullPath()); expect(exists).toEqual(true); done(); }); }); test("Create a json file", (done) => { 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); const json = parser.parse(); const fileSystemService = new FileSystemService_1.FileSystemService("data.json", "./Data"); fileSystemService.write(JSON.stringify(json)).then(() => { const exists = fs_1.default.existsSync(fileSystemService.getFullPath()); expect(exists).toEqual(true); done(); }); }); });