Fix getDataExplorerWindow (#285)

This commit is contained in:
Steve Faulkner 2020-10-16 16:01:41 -05:00 committed by GitHub
parent 9a5d46b6e0
commit 23714831bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 50 deletions

View File

@ -1,49 +1,39 @@
import { getDataExplorerWindow } from "./WindowUtils"; import { getDataExplorerWindow } from "./WindowUtils";
const createWindow = (dataExplorerPlatform: unknown, parent: Window): Window => { interface MockWindow {
// TODO: Need to `any` here since we're creating a mock window object parent?: MockWindow;
// eslint-disable-next-line @typescript-eslint/no-explicit-any top?: MockWindow;
const mockWindow: any = {}; }
if (dataExplorerPlatform !== undefined) {
mockWindow.dataExplorerPlatform = dataExplorerPlatform;
}
if (parent) {
mockWindow.parent = parent;
}
return mockWindow;
};
describe("WindowUtils", () => { describe("WindowUtils", () => {
describe("getDataExplorerWindow", () => { describe("getDataExplorerWindow", () => {
it("should return current window if current window has dataExplorerPlatform property", () => { it("should return undefined if current window is at the top", () => {
const currentWindow = createWindow(0, undefined); const mockWindow: MockWindow = {};
mockWindow.parent = mockWindow;
expect(getDataExplorerWindow(currentWindow)).toEqual(currentWindow); expect(getDataExplorerWindow(mockWindow as Window)).toEqual(undefined);
}); });
it("should return current window's parent if current window's parent has dataExplorerPlatform property", () => { it("should return current window if parent is top", () => {
const parentWindow = createWindow(0, undefined); const dataExplorerWindow: MockWindow = {};
const currentWindow = createWindow(undefined, parentWindow); const portalWindow: MockWindow = {};
dataExplorerWindow.parent = portalWindow;
dataExplorerWindow.top = portalWindow;
expect(getDataExplorerWindow(currentWindow)).toEqual(parentWindow); expect(getDataExplorerWindow(dataExplorerWindow as Window)).toEqual(dataExplorerWindow);
}); });
it("should return undefined if none of the windows in the hierarchy have dataExplorerPlatform property and window's parent is reference to itself", () => { it("should return closest window to top if in nested windows", () => {
const parentWindow = createWindow(undefined, undefined); const terminalWindow: MockWindow = {};
const dataExplorerWindow: MockWindow = {};
const portalWindow: MockWindow = {};
dataExplorerWindow.top = portalWindow;
dataExplorerWindow.parent = portalWindow;
terminalWindow.top = portalWindow;
terminalWindow.parent = dataExplorerWindow;
// TODO: Need to `any` here since parent is a readonly property expect(getDataExplorerWindow(terminalWindow as Window)).toEqual(dataExplorerWindow);
// eslint-disable-next-line @typescript-eslint/no-explicit-any expect(getDataExplorerWindow(dataExplorerWindow as Window)).toEqual(dataExplorerWindow);
(parentWindow as any).parent = parentWindow; // If a window does not have a parent, its parent property is a reference to itself.
const currentWindow = createWindow(undefined, parentWindow);
expect(getDataExplorerWindow(currentWindow)).toBeUndefined();
});
it("should return undefined if none of the windows in the hierarchy have dataExplorerPlatform property and window's parent is not defined", () => {
const parentWindow = createWindow(undefined, undefined);
const currentWindow = createWindow(undefined, parentWindow);
expect(getDataExplorerWindow(currentWindow)).toBeUndefined();
}); });
}); });
}); });

View File

@ -1,23 +1,18 @@
export const getDataExplorerWindow = (currentWindow: Window): Window | undefined => { export const getDataExplorerWindow = (currentWindow: Window): Window | undefined => {
// Start with the current window and traverse up the parent hierarchy to find a window // Data explorer is always loaded in an iframe, so traverse the parents until we hit the top and return the first child window.
// with `dataExplorerPlatform` property
let dataExplorerWindow: Window | undefined = currentWindow;
try { try {
// TODO: Need to `any` here since the window imports Explorer which can't be in strict mode yet while (currentWindow) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any if (currentWindow.parent === currentWindow) {
while (dataExplorerWindow && (dataExplorerWindow as any).dataExplorerPlatform === undefined) { return undefined;
// If a window does not have a parent, its parent property is a reference to itself.
if (dataExplorerWindow.parent === dataExplorerWindow) {
dataExplorerWindow = undefined;
} else {
dataExplorerWindow = dataExplorerWindow.parent;
} }
if (currentWindow.parent === currentWindow.top) {
return currentWindow;
}
currentWindow = currentWindow.parent;
} }
} catch (error) { } catch (error) {
// This can happen if we come across parent from a different origin // Hitting a cross domain error means we are in the portal and the current window is data explorer
dataExplorerWindow = undefined; return currentWindow;
} }
return undefined;
return dataExplorerWindow;
}; };