From dc916325333f3cbbf0040f730b5fe841b8c3f8cf Mon Sep 17 00:00:00 2001
From: Grant Bacon <gbacon@tibco.com>
Date: Thu, 30 May 2024 16:24:35 -0700
Subject: [PATCH] ICPlugin->InputControls, add tests for InputControls, fix
 import styles, fix jest config for TS files

---
 packages/input-controls/index.ts              | 66 +------------------
 packages/input-controls/jest.config.ts        |  3 +
 packages/input-controls/src/InputControls.tsx | 65 ++++++++++++++++++
 .../test/src/InputControls.test.ts            | 12 ++++
 packages/input-controls/tsconfig.json         |  3 +-
 packages/test-app/jest.config.ts              | 17 +++++
 packages/test-app/src/App.tsx                 |  4 +-
 packages/test-app/tsconfig.json               |  1 +
 8 files changed, 104 insertions(+), 67 deletions(-)
 create mode 100644 packages/input-controls/src/InputControls.tsx
 create mode 100644 packages/input-controls/test/src/InputControls.test.ts
 create mode 100644 packages/test-app/jest.config.ts

diff --git a/packages/input-controls/index.ts b/packages/input-controls/index.ts
index 6f2d4b37..c17e38bd 100644
--- a/packages/input-controls/index.ts
+++ b/packages/input-controls/index.ts
@@ -1,65 +1,3 @@
-import { FC } from 'react';
+import { InputControls } from "./src/InputControls";
 
-interface BooleanICConfig {
-    style?: string | FC,
-}
-
-export interface InputControlConfig {
-    hostname?: string,
-    username: string,
-    password: string,
-    tenant: string,
-    // customize control look & feel
-    boolean?: BooleanICConfig,
-};
-
-const defaultInputControlConfig: InputControlConfig = {
-    username: 'joeuser',
-    password: 'joeuser',
-    tenant: 'organization_1',
-
-    boolean: { style: 'switch' },
-};
-
-export class ICPlugin {
-    private viz: any;
-    private config: InputControlConfig;
-    protected controlStructure: object = {};
-
-    constructor(vizjs: any, config?: InputControlConfig) {
-        this.viz = vizjs;
-        this.config = config || defaultInputControlConfig;
-    }
-
-    public fillControlStructure = (uri: string, callbackFn?: Function) => {
-        this.viz({
-            auth: {
-                name: this.config.username || "joeuser",
-                password: this.config.password || "joeuser",
-                organization: this.config.tenant || "organization_1",
-            },
-        }, (v: any) => {
-            v.inputControls({
-                resource: uri,
-                success: (data: string) => {
-                    this.controlStructure = {...this.controlStructure, data};
-                    if (callbackFn) {
-                        callbackFn(this.controlStructure);
-                    }
-                },
-                error: (e: object) => {
-                    console.log(e);
-                },
-            });
-        });
-    }
-
-    public getControls = () => {
-        return this.controlStructure;
-    }
-
-    public makeControlsForReport = (resourceUri: string, container: any) => {
-        this.fillControlStructure(resourceUri);
-        container = JSON.stringify(this.controlStructure);
-    };
-}
\ No newline at end of file
+export default InputControls;
\ No newline at end of file
diff --git a/packages/input-controls/jest.config.ts b/packages/input-controls/jest.config.ts
index e0366769..62452429 100644
--- a/packages/input-controls/jest.config.ts
+++ b/packages/input-controls/jest.config.ts
@@ -9,6 +9,9 @@ const conf: Config = {
         'build',
     ],
     testEnvironment: 'jsdom',
+    transform: {
+        '^.+\\.([tj]s|[tj]sx)$': 'babel-jest',
+    },
 };
 
 export default conf;
\ No newline at end of file
diff --git a/packages/input-controls/src/InputControls.tsx b/packages/input-controls/src/InputControls.tsx
new file mode 100644
index 00000000..0d84b91d
--- /dev/null
+++ b/packages/input-controls/src/InputControls.tsx
@@ -0,0 +1,65 @@
+import { FC } from 'react';
+
+interface BooleanICConfig {
+    style?: string | FC,
+}
+
+export interface InputControlConfig {
+    hostname?: string,
+    username: string,
+    password: string,
+    tenant: string,
+    // customize control look & feel
+    boolean?: BooleanICConfig,
+};
+
+const defaultInputControlConfig: InputControlConfig = {
+    username: 'joeuser',
+    password: 'joeuser',
+    tenant: 'organization_1',
+
+    boolean: { style: 'switch' },
+};
+
+export class InputControls {
+    private viz: any;
+    private config: InputControlConfig;
+    protected controlStructure: object = {};
+
+    constructor(vizjs: any, config?: InputControlConfig) {
+        this.viz = vizjs;
+        this.config = config || defaultInputControlConfig;
+    }
+
+    public fillControlStructure = (uri: string, callbackFn?: Function) => {
+        this.viz({
+            auth: {
+                name: this.config.username || "joeuser",
+                password: this.config.password || "joeuser",
+                organization: this.config.tenant || "organization_1",
+            },
+        }, (v: any) => {
+            v.inputControls({
+                resource: uri,
+                success: (data: string) => {
+                    this.controlStructure = {...this.controlStructure, data};
+                    if (callbackFn) {
+                        callbackFn(this.controlStructure);
+                    }
+                },
+                error: (e: object) => {
+                    console.log(e);
+                },
+            });
+        });
+    }
+
+    public getControls = () => {
+        return this.controlStructure;
+    }
+
+    public makeControlsForReport = (resourceUri: string, container: any) => {
+        this.fillControlStructure(resourceUri);
+        container = JSON.stringify(this.controlStructure);
+    };
+}
\ No newline at end of file
diff --git a/packages/input-controls/test/src/InputControls.test.ts b/packages/input-controls/test/src/InputControls.test.ts
new file mode 100644
index 00000000..6ff6639f
--- /dev/null
+++ b/packages/input-controls/test/src/InputControls.test.ts
@@ -0,0 +1,12 @@
+import { InputControls } from "../../src/InputControls"
+
+describe("InputControls plugin", () => {
+
+    it("should allow instantiation", () => {
+        let ic = new InputControls({});
+        expect(ic).toBeDefined();
+        expect(ic.getControls()).toEqual({});
+        expect(ic.config.username).toBe('joeuser');
+    });
+
+})
\ No newline at end of file
diff --git a/packages/input-controls/tsconfig.json b/packages/input-controls/tsconfig.json
index 8e322aac..1f5e02ce 100644
--- a/packages/input-controls/tsconfig.json
+++ b/packages/input-controls/tsconfig.json
@@ -9,7 +9,8 @@
         "skipLibCheck": true,
         "outDir": "dist",
         "moduleResolution": "NodeNext",
+        "module": "NodeNext"
     },
     "include": ["**/*.ts", "**/*.tsx"],
-    "exclude": ["node_modules", "dist"],
+    "exclude": ["node_modules", "dist"]
 }
\ No newline at end of file
diff --git a/packages/test-app/jest.config.ts b/packages/test-app/jest.config.ts
new file mode 100644
index 00000000..62452429
--- /dev/null
+++ b/packages/test-app/jest.config.ts
@@ -0,0 +1,17 @@
+import {Config} from 'jest'
+
+const conf: Config = {
+    verbose: true,
+    watchPathIgnorePatterns: [
+        'node_modules',
+        'dist',
+        'coverage',
+        'build',
+    ],
+    testEnvironment: 'jsdom',
+    transform: {
+        '^.+\\.([tj]s|[tj]sx)$': 'babel-jest',
+    },
+};
+
+export default conf;
\ No newline at end of file
diff --git a/packages/test-app/src/App.tsx b/packages/test-app/src/App.tsx
index 78b34f39..a8021e38 100644
--- a/packages/test-app/src/App.tsx
+++ b/packages/test-app/src/App.tsx
@@ -1,4 +1,4 @@
-import { ICPlugin } from 'input-controls-plugin';
+import InputControls from 'input-controls-plugin';
 import { useEffect, useState } from 'react';
 
 export interface AppConfig {
@@ -8,7 +8,7 @@ export interface AppConfig {
 export default function App(props: AppConfig) {
 
     const [controlStruct, setControlStruct] = useState({});
-    const plugin = new ICPlugin((window as any).visualize);
+    const plugin = new InputControls((window as any).visualize);
     
     useEffect(() => {
         plugin.fillControlStructure('/public/viz/Adhoc/Ad_Hoc_View_All_filters_Report', (controls: object) => {
diff --git a/packages/test-app/tsconfig.json b/packages/test-app/tsconfig.json
index 8e322aac..134459b1 100644
--- a/packages/test-app/tsconfig.json
+++ b/packages/test-app/tsconfig.json
@@ -9,6 +9,7 @@
         "skipLibCheck": true,
         "outDir": "dist",
         "moduleResolution": "NodeNext",
+        "module": "NodeNext"
     },
     "include": ["**/*.ts", "**/*.tsx"],
     "exclude": ["node_modules", "dist"],