From 6f64d22b3bc58f1ff1a58ce1db5d874de6f3a7ba Mon Sep 17 00:00:00 2001
From: Dick Wolff <dckwlff@gmail.com>
Date: Thu, 1 Feb 2024 10:23:38 +0100
Subject: [PATCH] Remove fs dependency from unit test

---
 src/converters/abstractconverter.ts        |  6 ++--
 src/converters/degiroConverter.ts          |  2 +-
 src/converters/finpensionConverter.ts      |  2 +-
 src/converters/swissquoteComverter.test.ts | 33 ++++------------------
 src/converters/swissquoteConverter.ts      |  2 +-
 src/converters/trading212Converter.ts      |  2 +-
 6 files changed, 13 insertions(+), 34 deletions(-)

diff --git a/src/converters/abstractconverter.ts b/src/converters/abstractconverter.ts
index 03a18a1e..ca6c2675 100644
--- a/src/converters/abstractconverter.ts
+++ b/src/converters/abstractconverter.ts
@@ -26,7 +26,7 @@ export abstract class AbstractConverter {
         
         const contents = fs.readFileSync(inputFile, "utf-8");
 
-        this.processFile(contents,successCallback, errorCallback);
+        this.processFileContents(contents,successCallback, errorCallback);
     }
 
     /**
@@ -38,13 +38,13 @@ export abstract class AbstractConverter {
     abstract isIgnoredRecord(record: any): boolean;    
 
     /**
-     * Process an export file.
+     * Process export file contents.
      * 
      * @param input The file contents to convert.
      * @param successCallback A callback to execute after processing has succeeded.
      * @param errorCallback A callback to execute after processing has failed.
      */
-    abstract processFile(input: string, successCallback: CallableFunction, errorCallback: CallableFunction): void;
+    abstract processFileContents(input: string, successCallback: CallableFunction, errorCallback: CallableFunction): void;
 
     /**
      * Retrieve headers from the input file.
diff --git a/src/converters/degiroConverter.ts b/src/converters/degiroConverter.ts
index cb36f1a3..d18bb87b 100644
--- a/src/converters/degiroConverter.ts
+++ b/src/converters/degiroConverter.ts
@@ -23,7 +23,7 @@ export class DeGiroConverter extends AbstractConverter {
   /**
    * @inheritdoc
    */
-  public processFile(input: string, successCallback: any, errorCallback: any): void {
+  public processFileContents(input: string, successCallback: any, errorCallback: any): void {
 
     // Parse the CSV and convert to Ghostfolio import format.
     parse(input, {
diff --git a/src/converters/finpensionConverter.ts b/src/converters/finpensionConverter.ts
index ae963780..11495c32 100644
--- a/src/converters/finpensionConverter.ts
+++ b/src/converters/finpensionConverter.ts
@@ -20,7 +20,7 @@ export class FinpensionConverter extends AbstractConverter {
     /**
      * @inheritdoc
      */
-    public processFile(input: string, successCallback: any, errorCallback: any): void {
+    public processFileContents(input: string, successCallback: any, errorCallback: any): void {
 
         // Parse the CSV and convert to Ghostfolio import format.
         const parser = parse(input, {
diff --git a/src/converters/swissquoteComverter.test.ts b/src/converters/swissquoteComverter.test.ts
index 58919ea7..e41b812c 100644
--- a/src/converters/swissquoteComverter.test.ts
+++ b/src/converters/swissquoteComverter.test.ts
@@ -1,4 +1,3 @@
-import * as fs from "fs";
 import { SwissquoteConverter } from "./swissquoteConverter";
 import { GhostfolioExport } from "../models/ghostfolioExport";
 
@@ -20,7 +19,7 @@ describe("swissquoteConverter", () => {
     const inputFile = "sample-swissquote-export.csv";
 
     // Act      
-    sut.processFile(inputFile, (actualExport: GhostfolioExport) =>  {
+    sut.readAndProcessFile(inputFile, (actualExport: GhostfolioExport) =>  {
 
       // Assert
       expect(actualExport).toBeTruthy();
@@ -31,22 +30,6 @@ describe("swissquoteConverter", () => {
   });
 
   describe("should throw an error if", () => {
-    beforeAll(() => {
-      jest.spyOn(console, 'log').mockImplementation(jest.fn());
-      jest.spyOn(console, 'error').mockImplementation(jest.fn());
-
-      // Create test input folder before run.
-      if(!fs.existsSync("tmp/testinput")) {
-        fs.mkdirSync("tmp/testinput");
-      }
-    });
-
-    afterAll(() => {
-
-      // Clean test input folder after run.
-      fs.rmSync("tmp/testinput", { recursive: true });
-    })
-
     it("the input file does not exist", (done) => {
 
       // Act
@@ -55,7 +38,7 @@ describe("swissquoteConverter", () => {
       let tempFileName = "tmp/testinput/swissquote-filedoesnotexist.csv";
       
       // Act
-      sut.processFile(tempFileName, () =>  { fail("Should not succeed!"); }, (err: Error) => {
+      sut.readAndProcessFile(tempFileName, () =>  { fail("Should not succeed!"); }, (err: Error) => {
 
         // Assert
         expect(err).toBeTruthy();
@@ -70,13 +53,11 @@ describe("swissquoteConverter", () => {
       const sut = new SwissquoteConverter();
 
       // Create temp file.
-      let tempFileName = "tmp/testinput/swissquote-filedoesisempty.csv";
       let tempFileContent = "";
       tempFileContent += "Date;Order #;Transaction;Symbol;Name;ISIN;Quantity;Unit price;Costs;Accrued Interest;Net Amount;Balance;Currency\n";      
-      fs.writeFileSync(tempFileName, tempFileContent);
       
       // Act
-      sut.processFile(tempFileName, () =>  { fail("Should not succeed!"); }, (err: Error) => {
+      sut.processFileContents(tempFileContent, () =>  { fail("Should not succeed!"); }, (err: Error) => {
 
         // Assert
         expect(err).toBeTruthy();
@@ -84,21 +65,19 @@ describe("swissquoteConverter", () => {
         done();
       });      
     });
-  
+
     it("Yahoo Finance got empty input for query", (done) => {
 
       // Act
       const sut = new SwissquoteConverter();
 
       // Create temp file.
-      let tempFileName = "tmp/testinput/swissquote-yahoofinanceerrortest.csv";
       let tempFileContent = "";
       tempFileContent += "Date;Order #;Transaction;Symbol;Name;ISIN;Quantity;Unit price;Costs;Accrued Interest;Net Amount;Balance;Currency\n";      
       tempFileContent += "10-08-2022 15:30:02;113947121;Buy;;;;200.0;19.85;5.96;0.00;-3975.96;168660.08;USD";
-      fs.writeFileSync(tempFileName, tempFileContent);
-      
+            
       // Act
-      sut.processFile(tempFileName, () =>  { fail("Should not succeed!"); }, (err) => {
+      sut.processFileContents(tempFileContent, () =>  { fail("Should not succeed!"); }, (err) => {
 
         // Assert
         expect(err).toBeTruthy();
diff --git a/src/converters/swissquoteConverter.ts b/src/converters/swissquoteConverter.ts
index bd301a8f..a0070714 100644
--- a/src/converters/swissquoteConverter.ts
+++ b/src/converters/swissquoteConverter.ts
@@ -23,7 +23,7 @@ export class SwissquoteConverter extends AbstractConverter {
     /**
      * @inheritdoc
      */
-    public processFile(input: string, successCallback: any, errorCallback: any): void {
+    public processFileContents(input: string, successCallback: any, errorCallback: any): void {
 
         // Parse the CSV and convert to Ghostfolio import format.
         const parser = parse(input, {
diff --git a/src/converters/trading212Converter.ts b/src/converters/trading212Converter.ts
index de2d81a9..19267d91 100644
--- a/src/converters/trading212Converter.ts
+++ b/src/converters/trading212Converter.ts
@@ -20,7 +20,7 @@ export class Trading212Converter extends AbstractConverter {
     /**
      * @inheritdoc
      */
-    public processFile(input: string, successCallback: any, errorCallback: any): void {
+    public processFileContents(input: string, successCallback: any, errorCallback: any): void {
 
         // Parse the CSV and convert to Ghostfolio import format.
         parse(input, {