Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load method to change current url of window #191

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
12 changes: 11 additions & 1 deletion tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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");
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Expand Down Expand Up @@ -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(() => {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down Expand Up @@ -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(() => {
Expand Down