From d903ca3badec0ddceac1ccbd6920c167253e5142 Mon Sep 17 00:00:00 2001
From: Brian Ingenito <bingenito@users.noreply.github.com>
Date: Tue, 25 Sep 2018 10:47:15 +0000
Subject: [PATCH] Add load method to change current url of window (#191)

---
 src/Default/default.ts               |  7 +++++++
 src/Electron/electron.ts             | 11 +++++++++++
 src/OpenFin/openfin.ts               |  6 ++++++
 src/window.ts                        |  3 +++
 tests/unit/Default/default.spec.ts   | 12 +++++++++++-
 tests/unit/Electron/electron.spec.ts | 16 ++++++++++++++++
 tests/unit/OpenFin/openfin.spec.ts   | 12 ++++++++++++
 7 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/src/Default/default.ts b/src/Default/default.ts
index daf203a8..6d20217d 100644
--- a/src/Default/default.ts
+++ b/src/Default/default.ts
@@ -28,6 +28,13 @@ export class DefaultContainerWindow extends ContainerWindow {
         return this.innerWindow[DefaultContainer.windowNamePropertyKey];
     }
 
+    public load(url: string, options?: any): Promise<void> {
+        return new Promise<void>(resolve => {
+            this.innerWindow.location.replace(url);
+            resolve();
+        });
+    }
+
     public focus(): Promise<void> {
         this.innerWindow.focus();
         return Promise.resolve();
diff --git a/src/Electron/electron.ts b/src/Electron/electron.ts
index 90d9dd58..3b965fd9 100644
--- a/src/Electron/electron.ts
+++ b/src/Electron/electron.ts
@@ -55,6 +55,17 @@ export class ElectronContainerWindow extends ContainerWindow {
         return this.innerWindow.name;
     }
 
+    public load(url: string, options?: any) : Promise<void> {
+        return new Promise<void>(resolve => {
+            if (options) {
+                this.innerWindow.loadURL(url, options);
+            } else {
+                this.innerWindow.loadURL(url);
+            }
+            resolve();
+        });
+    }
+
     public focus(): Promise<void> {
         this.innerWindow.focus();
         return Promise.resolve();
diff --git a/src/OpenFin/openfin.ts b/src/OpenFin/openfin.ts
index 87d9cb3a..80c5d6d6 100644
--- a/src/OpenFin/openfin.ts
+++ b/src/OpenFin/openfin.ts
@@ -42,6 +42,12 @@ export class OpenFinContainerWindow extends ContainerWindow {
         return this.innerWindow.name;
     }
 
+    public load(url: string, options?: any): Promise<void> {
+        return new Promise<void>((resolve, reject) => {
+            this.innerWindow.navigate(url, resolve, reject);
+        });
+    }
+
     public focus(): Promise<void> {
         return new Promise<void>((resolve, reject) => {
             this.innerWindow.focus(resolve, reject);
diff --git a/src/window.ts b/src/window.ts
index af2c0d62..5f2afa9d 100644
--- a/src/window.ts
+++ b/src/window.ts
@@ -86,6 +86,9 @@ export abstract class ContainerWindow extends EventEmitter {
         this.innerWindow = wrap;
     }
 
+    /** Navigate window to a new url */
+    public abstract load(url: string, options?: any): Promise<void>;
+
     /** Gives focus to the window. */
     public abstract focus(): Promise<void>;
 
diff --git a/tests/unit/Default/default.spec.ts b/tests/unit/Default/default.spec.ts
index 63f329d9..b6dd9440 100644
--- a/tests/unit/Default/default.spec.ts
+++ b/tests/unit/Default/default.spec.ts
@@ -16,7 +16,10 @@ class MockWindow {
     public screenY: any = 1;
     public outerWidth: any = 2;
     public outerHeight: any = 3;
-    location: any = { origin: "origin" };
+    location: any = {
+        origin: "origin",
+        replace(url: string) {}
+    };
 }
 
 describe("DefaultContainerWindow", () => {
@@ -36,6 +39,13 @@ describe("DefaultContainerWindow", () => {
         });
     });
 
+    it ("load invokes underlying location.replace", (done) => {
+        spyOn(mockWindow.location, 'replace').and.callThrough();
+        win.load("url").then(() => {
+            expect(mockWindow.location.replace).toHaveBeenCalledWith("url");
+        }).then(done);
+    });
+
     it ("id returns underlying id", () => {
         mockWindow[DefaultContainer.windowUuidPropertyKey] = "UUID";
         expect(win.id).toEqual("UUID");
diff --git a/tests/unit/Electron/electron.spec.ts b/tests/unit/Electron/electron.spec.ts
index 2ecf9dac..39ad6013 100644
--- a/tests/unit/Electron/electron.spec.ts
+++ b/tests/unit/Electron/electron.spec.ts
@@ -38,6 +38,7 @@ class MockWindow extends MockEventEmitter {
         this.name = name;
     }
 
+    public loadURL(url: string, options?: any): void { }
     public focus(): void { }
     public show(): void { }
     public close(): void { }
@@ -113,6 +114,21 @@ describe("ElectronContainerWindow", () => {
     });
 
     describe("Window members", () => {
+        it("load", (done) => {
+            spyOn(innerWin, "loadURL").and.callThrough();
+            win.load("url").then(() => {
+                expect(innerWin.loadURL).toHaveBeenCalledWith("url");
+            }).then(done);
+        });
+
+        it("load with options", (done) => {
+            spyOn(innerWin, "loadURL").and.callThrough();
+            const options = { prop: "value" };
+            win.load("url", options).then(() => {
+                expect(innerWin.loadURL).toHaveBeenCalledWith("url", options);
+            }).then(done);
+        });
+
         it("focus", (done) => {
             spyOn(innerWin, "focus").and.callThrough();
             win.focus().then(() => {
diff --git a/tests/unit/OpenFin/openfin.spec.ts b/tests/unit/OpenFin/openfin.spec.ts
index 03523a54..eeec6c8a 100644
--- a/tests/unit/OpenFin/openfin.spec.ts
+++ b/tests/unit/OpenFin/openfin.spec.ts
@@ -71,6 +71,11 @@ class MockWindow {
 
     getNativeWindow(): any { return this.nativeWindow; }
 
+    navigate(url: string, callback: () => void, error: (reason) => void): any {
+        callback();
+        return {};
+    }
+
     focus(callback: () => void, error: (reason) => void): any {
         callback();
         return {};
@@ -184,6 +189,13 @@ describe("OpenFinContainerWindow", () => {
         expect(nativeWin).toEqual(innerWin.nativeWindow);
     });
 
+    it("load", (done) => {
+        spyOn(innerWin, "navigate").and.callThrough();
+        win.load("url").then(() => {
+            expect(innerWin.navigate).toHaveBeenCalledWith("url", jasmine.any(Function), jasmine.any(Function));
+        }).then(done);
+    });
+
     it("focus", (done) => {
         spyOn(innerWin, "focus").and.callThrough();
         win.focus().then(() => {