From 74e861f9761041be507071fdb3594b46d8c68d1d Mon Sep 17 00:00:00 2001 From: Westin Wrzesinski Date: Tue, 22 Nov 2022 14:27:49 -0600 Subject: [PATCH] fix: tests pass --- .eslintrc.js | 10 +++ package.json | 4 +- src/types.ts | 4 +- src/util.ts | 3 +- src/utils.ts | 125 +++++++++++++++--------------- test/closeWindow.test.ts | 2 +- test/getAllFramesInWindow.test.ts | 12 ++- test/getDomain.test.ts | 5 +- test/getParent.test.ts | 2 +- test/getParents.test.ts | 2 +- test/isBlankDomain.test.ts | 7 +- test/isSameDomain.test.ts | 27 +++++-- test/utils.ts | 5 +- webpack.config.js | 40 ---------- 14 files changed, 117 insertions(+), 131 deletions(-) delete mode 100644 webpack.config.js diff --git a/.eslintrc.js b/.eslintrc.js index 84bcf1c..163f512 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,5 +5,15 @@ module.exports = { rules: { "keyword-spacing": "off", "@typescript-eslint/keyword-spacing": "off", + + // off for initial ts conversion + // Implicit any in catch clause + "@typescript-eslint/no-implicit-any-catch": "off", + // Prefer using an optional chain expression instead, as it's more concise and easier to read + "@typescript-eslint/prefer-optional-chain": "off", + // Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator + "@typescript-eslint/prefer-nullish-coalescing": "off", + // Generic Object Injection Sink + "security/detect-object-injection": "off", }, }; diff --git a/package.json b/package.json index b59ea13..13807ce 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "description": "Utilities for dealing with cross-domain windows.", "main": "dist/cross-domain-utils.js", "module": "dist/index.js", - "types": "dist/types/index.d.ts", + "types": "dist/esm/index.d.ts", "sideEffects": false, "scripts": { "build": "npm run test && npm run babel && npm run webpack && npm run build:types", "build:flow": "find ./dist -type f -not -path './node_modules/*' -name '*.d.ts' -exec sh -c 'flowgen --add-flow-header $1 -o ${1%.*.*}.js.flow' _ '{}' \\;", - "build:tsc": "tsc src/* --outDir ./dist/esm --declaration --emitDeclarationOnly", + "build:tsc": "tsc src/* --outDir ./dist/esm --declaration --emitDeclarationOnly --strict", "build:types": "npm run build:tsc && npm run build:flow", "webpack": "cross-env NODE_ENV=production babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --progress --output-path dist", "babel": "cross-env NODE_ENV=production babel src/ --out-dir ./dist/esm/ --extensions .ts,.tsx", diff --git a/src/types.ts b/src/types.ts index 92ae4e0..7bd05c9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,6 @@ export type SameDomainWindowType = Omit< export type DomainMatcher = | string - | ReadonlyArray - | ReadonlyArray + | readonly string[] + | readonly string[] | RegExp; diff --git a/src/util.ts b/src/util.ts index 99902d1..d982f0a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,6 +2,7 @@ export function isRegex(item: unknown): boolean { return Object.prototype.toString.call(item) === "[object RegExp]"; } -export function noop(...args: ReadonlyArray): void { +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export function noop(...args: readonly unknown[]): void { // pass } diff --git a/src/utils.ts b/src/utils.ts index 1d30ec4..0869cb4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -13,9 +13,9 @@ export function getActualProtocol(win: SameDomainWindowType = window): string { } export function getProtocol(win: SameDomainWindowType = window): string { - // @ts-ignore mockDomain does not exist on window + // @ts-expect-error mockDomain does not exist on window if (win.mockDomain) { - // @ts-ignore mockDomain does not exist on window + // @ts-expect-error mockDomain does not exist on window // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call const protocol = win.mockDomain.split("//")[0]; @@ -42,7 +42,7 @@ export function isMockProtocol(win: SameDomainWindowType = window): boolean { export function getParent( win: CrossDomainWindowType = window -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { if (!win) { return; } @@ -58,7 +58,7 @@ export function getParent( export function getOpener( win: CrossDomainWindowType = window -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { if (!win) { return; } @@ -106,11 +106,10 @@ export function getActualDomain(win: SameDomainWindowType = window): string { } if (protocol === PROTOCOL.ABOUT) { - // @ts-ignore - trying to reassing to something that might be window + // @ts-expect-error - trying to reassing to something that might be window const parent = getParent(win); if (parent && canReadFromWindow(parent)) { - // @ts-ignore return getActualDomain(parent); } @@ -131,12 +130,12 @@ export function getDomain(win: SameDomainWindowType = window): string { if ( domain && - // @ts-ignore - mockDomain + // @ts-expect-error - mockDomain win.mockDomain && - // @ts-ignore - mockDomain - (win.mockDomain as string).indexOf(PROTOCOL.MOCK) === 0 + // @ts-expect-error - mockDomain + (win.mockDomain as string).startsWith(PROTOCOL.MOCK) ) { - // @ts-ignore - mockDomain + // @ts-expect-error - mockDomain return win.mockDomain as string; } @@ -245,7 +244,7 @@ export function assertSameDomain( export function getParents( win: CrossDomainWindowType -): ReadonlyArray { +): readonly CrossDomainWindowType[] { const result = []; try { @@ -274,17 +273,15 @@ export function isAncestorParent( return childParent === parent; } - if (getParents(child).indexOf(parent) !== -1) { + if (getParents(child).includes(parent)) { return true; } return false; } -export function getFrames( - win: CrossDomainWindowType -): Array { - const result: Array = []; +export function getFrames(win: CrossDomainWindowType): CrossDomainWindowType[] { + const result: CrossDomainWindowType[] = []; let frames; try { @@ -342,7 +339,7 @@ export function getFrames( export function getAllChildFrames( win: CrossDomainWindowType -): ReadonlyArray { +): readonly CrossDomainWindowType[] { const result = []; for (const frame of getFrames(win)) { @@ -358,7 +355,7 @@ export function getAllChildFrames( export function getTop( win: CrossDomainWindowType = window -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { try { if (win.top) { return win.top; @@ -404,25 +401,26 @@ export function getTop( export function getNextOpener( win: CrossDomainWindowType = window -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { return getOpener(getTop(win) || win); } export function getUltimateTop( win: CrossDomainWindowType = window -): CrossDomainWindowType | null { +): CrossDomainWindowType | undefined { const opener = getNextOpener(win); if (opener) { return getUltimateTop(opener); } + // @ts-expect-error where does top come from here? return top; } export function getAllFramesInWindow( win: CrossDomainWindowType -): ReadonlyArray { +): readonly CrossDomainWindowType[] { const top = getTop(win); if (!top) { @@ -432,7 +430,7 @@ export function getAllFramesInWindow( let result = [...getAllChildFrames(top), top]; // Win may be in shadow dom - if (result.indexOf(win) === -1) { + if (!result.includes(win)) { result = [...result, win, ...getAllChildFrames(win)]; } @@ -441,7 +439,7 @@ export function getAllFramesInWindow( export function getAllWindows( win: CrossDomainWindowType = window -): ReadonlyArray { +): readonly CrossDomainWindowType[] { const frames = getAllFramesInWindow(win); const opener = getNextOpener(win); @@ -471,11 +469,11 @@ export function isFrameWindowClosed(frame: HTMLIFrameElement): boolean { let parent = frame; while (parent.parentNode && parent.parentNode !== parent) { - // @ts-ignore + // @ts-expect-error parentNode is missing from HTMLIFrameElement parent = parent.parentNode; } - // @ts-ignore - host does not exist on type frame + // @ts-expect-error - host does not exist on type frame // eslint-disable-next-line @typescript-eslint/no-unsafe-argument if (!parent.host || !doc.documentElement.contains(parent.host)) { return true; @@ -485,7 +483,7 @@ export function isFrameWindowClosed(frame: HTMLIFrameElement): boolean { return false; } -function safeIndexOf(collection: ReadonlyArray, item: T): number { +function safeIndexOf(collection: readonly T[], item: T): number { for (let i = 0; i < collection.length; i++) { try { if (collection[i] === item) { @@ -499,8 +497,8 @@ function safeIndexOf(collection: ReadonlyArray, item: T): number { return -1; } -const iframeWindows: Array = []; -const iframeFrames: Array = []; +const iframeWindows: CrossDomainWindowType[] = []; +const iframeFrames: HTMLIFrameElement[] = []; export function isWindowClosed( win: CrossDomainWindowType, @@ -537,7 +535,7 @@ export function isWindowClosed( if (allowMock && isSameDomain(win)) { try { - // @ts-ignore + // @ts-expect-error win.mockclosed is our own added var on window if (win.mockclosed) { return true; } @@ -608,17 +606,17 @@ export function linkFrameWindow(frame: HTMLIFrameElement): void { } export function getUserAgent( - win: SameDomainWindowType | null | undefined + win: SameDomainWindowType | undefined | undefined ): string { win = win || window; - // @ts-ignore + // @ts-expect-error win.mockUserAgent is our own added var return (win.navigator.mockUserAgent as string) || win.navigator.userAgent; } export function getFrameByName( win: CrossDomainWindowType, name: string -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { const winFrames = getFrames(win); for (const childFrame of winFrames) { @@ -626,7 +624,7 @@ export function getFrameByName( if ( isSameDomain(childFrame) && childFrame.name === name && - winFrames.indexOf(childFrame) !== -1 + winFrames.includes(childFrame) ) { return childFrame; } @@ -636,10 +634,10 @@ export function getFrameByName( } try { - // @ts-ignore + // @ts-expect-error win.frames is implicit any so no indexing // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - if (winFrames.indexOf(win.frames[name]) !== -1) { - // @ts-ignore + if (winFrames.includes(win.frames[name])) { + // @ts-expect-error win.frames is implicit any so no indexing return win.frames[name] as CrossDomainWindowType; } } catch (err: unknown) { @@ -647,10 +645,10 @@ export function getFrameByName( } try { - // @ts-ignore + // @ts-expect-error win can't be indexed by name which is string // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - if (winFrames.indexOf(win[name]) !== -1) { - // @ts-ignore + if (winFrames.includes(win[name])) { + // @ts-expect-error win can't be indexed by name which is string return win[name] as CrossDomainWindowType; } } catch (err: unknown) { @@ -661,7 +659,7 @@ export function getFrameByName( export function findChildFrameByName( win: CrossDomainWindowType, name: string -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { const frame = getFrameByName(win, name); if (frame) { @@ -680,7 +678,7 @@ export function findChildFrameByName( export function findFrameByName( win: CrossDomainWindowType, name: string -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { const frame = getFrameByName(win, name); if (frame) { @@ -719,7 +717,7 @@ export function isOpener( export function getAncestor( win: CrossDomainWindowType = window -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { win = win || window; const opener = getOpener(win); @@ -736,12 +734,12 @@ export function getAncestor( export function getAncestors( win: CrossDomainWindowType -): ReadonlyArray { +): readonly CrossDomainWindowType[] { const results = []; let ancestor = win; while (ancestor) { - // @ts-ignore + // @ts-expect-error type Window | undefined is not assignable to type Window ancestor = getAncestor(ancestor); if (ancestor) { @@ -796,8 +794,8 @@ export function isFullpage(win: CrossDomainWindowType = window): boolean { } function anyMatch( - collection1: ReadonlyArray, - collection2: ReadonlyArray + collection1: readonly unknown[], + collection2: readonly unknown[] ): boolean { for (const item1 of collection1) { for (const item2 of collection2) { @@ -817,7 +815,7 @@ export function getDistanceFromTop( let parent = win; while (parent) { - // @ts-ignore - trying to reassing to something that might be window + // @ts-expect-error - trying to reassing to something that might be window parent = getParent(parent); if (parent) { @@ -831,7 +829,7 @@ export function getDistanceFromTop( export function getNthParent( win: CrossDomainWindowType, n = 1 -): CrossDomainWindowType | undefined | null { +): CrossDomainWindowType | undefined | undefined { let parent = win; for (let i = 0; i < n; i++) { @@ -839,7 +837,7 @@ export function getNthParent( return; } - // @ts-ignore - trying to reassign to something that might be window + // @ts-expect-error - trying to reassign to something that might be window parent = getParent(parent); } @@ -849,7 +847,7 @@ export function getNthParent( export function getNthParentFromTop( win: CrossDomainWindowType, n = 1 -): CrossDomainWindowType | null | undefined { +): CrossDomainWindowType | undefined | undefined { return getNthParent(win, getDistanceFromTop(win) - n); } @@ -920,7 +918,7 @@ export function matchDomain( return false; } - // @ts-ignore - earlier already shortcutted the string case + // @ts-expect-error - earlier already shortcutted the string case // eslint-disable-next-line @typescript-eslint/no-unsafe-call return Boolean(origin.match(pattern)); } @@ -955,7 +953,7 @@ export function stringifyDomainPattern(pattern: DomainMatcher): string { export function getDomainFromUrl(url: string): string { let domain; - if (url.match(/^(https?|mock|file):\/\//)) { + if (/^(https?|mock|file):\/\//.exec(url)) { domain = url; } else { return getDomain(); @@ -967,7 +965,7 @@ export function getDomainFromUrl(url: string): string { export function onCloseWindow( win: CrossDomainWindowType, - callback: (...args: Array) => void, + callback: (...args: unknown[]) => void, delay = 1000, maxtime = Infinity ): { cancel: () => void } { @@ -1035,7 +1033,7 @@ export function isWindow(obj: unknown): boolean { } try { - // @ts-ignore not complete sure that obj has self. needs guard + // @ts-expect-error not complete sure that obj has self. needs guard if (obj && obj.self === obj) { return true; } @@ -1046,7 +1044,7 @@ export function isWindow(obj: unknown): boolean { } try { - // @ts-ignore not complete sure that obj has parent. needs guard + // @ts-expect-error not complete sure that obj has parent. needs guard if (obj && obj.parent === obj) { return true; } @@ -1057,7 +1055,7 @@ export function isWindow(obj: unknown): boolean { } try { - // @ts-ignore not complete sure that obj has top. needs guard + // @ts-expect-error not complete sure that obj has top. needs guard if (obj && obj.top === obj) { return true; } @@ -1068,7 +1066,7 @@ export function isWindow(obj: unknown): boolean { } try { - // @ts-ignore this equality check is a self compare + // @ts-expect-error this equality check is a self compare // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression, no-self-compare if (noop(obj === obj) === "__unlikely_value__") { return false; @@ -1080,7 +1078,7 @@ export function isWindow(obj: unknown): boolean { try { if ( obj && - // @ts-ignore more shenanigans + // @ts-expect-error more shenanigans obj.__cross_domain_utils_window_check__ === "__unlikely_value__" ) { return false; @@ -1090,7 +1088,7 @@ export function isWindow(obj: unknown): boolean { } try { - // @ts-ignore needs guard + // @ts-expect-error needs guard if ("postMessage" in obj && "self" in obj && "location" in obj) { return true; } @@ -1116,7 +1114,7 @@ export function isCurrentDomain(domain: string): boolean { } export function isMockDomain(domain: string): boolean { - return domain.indexOf(PROTOCOL.MOCK) === 0; + return domain.startsWith(PROTOCOL.MOCK); } export function normalizeMockUrl(url: string): string { @@ -1124,7 +1122,7 @@ export function normalizeMockUrl(url: string): string { return url; } - // @ts-ignore - global + // @ts-expect-error - global if (!__TEST__) { throw new Error(`Mock urls not supported out of test mode`); } @@ -1134,12 +1132,13 @@ export function normalizeMockUrl(url: string): string { export function getFrameForWindow( win: CrossDomainWindowType -): HTMLElement | Element | null | undefined { +): HTMLElement | Element | undefined { if (isSameDomain(win)) { + // @ts-expect-error Type 'Element | null' is not assignable to type 'Element | HTMLElement | undefined' return assertSameDomain(win).frameElement; } - // @ts-ignore not an array type, but is iterable so fine + // @ts-expect-error not an array type, but is iterable so fine for (const frame of document.querySelectorAll("iframe")) { // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access if (frame && frame.contentWindow && frame.contentWindow === win) { diff --git a/test/closeWindow.test.ts b/test/closeWindow.test.ts index 53bcd0a..9ff546a 100644 --- a/test/closeWindow.test.ts +++ b/test/closeWindow.test.ts @@ -12,7 +12,7 @@ test("closeWindow will call window.close", () => { }, }); - // @ts-ignore + // @ts-expect-error Argument of type 'Window | SameDomainWindowType' is not assignable to parameter of type 'Window' closeWindow(win); assert(fnCalled, `Expected window.close to be called`); diff --git a/test/getAllFramesInWindow.test.ts b/test/getAllFramesInWindow.test.ts index f8eadf8..d58508c 100644 --- a/test/getAllFramesInWindow.test.ts +++ b/test/getAllFramesInWindow.test.ts @@ -33,7 +33,7 @@ test("getAllFramesInWindow should get all of the frames", () => { z.parent = z; const allFrames = [a, b, x, y, z]; - // @ts-ignore x is not type of window + // @ts-expect-error x is not type of window const foundFrames = getAllFramesInWindow(x); assert( @@ -42,24 +42,22 @@ test("getAllFramesInWindow should get all of the frames", () => { ); for (const frame of allFrames) { - // @ts-ignore frame is not type of window + // @ts-expect-error frame is not type of window + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions assert(foundFrames.includes(frame), `Did not find frame ${frame.name}`); } }); test("should get a mock frame defined in window.frames", () => { const frames = window.frames; - const mockFrame = {}; - // @ts-ignore - window.frames = [mockFrame]; + const mockFrame = {} as unknown as Window; + window.frames = [mockFrame] as unknown as Window; const foundFrames = getAllFramesInWindow(window); assert( - // @ts-ignore foundFrames.includes(mockFrame), `getAllFramesInWindow expected to find mock frame in window.frames` ); - // @ts-ignore window.frames = frames; }); diff --git a/test/getDomain.test.ts b/test/getDomain.test.ts index 079f051..144eec7 100644 --- a/test/getDomain.test.ts +++ b/test/getDomain.test.ts @@ -5,8 +5,9 @@ import { getDomain } from "../src"; import { getSameDomainWindow } from "./utils"; test("getDomain should get the domain for the current window", () => { - // @ts-ignore - window.location = new URL("https://www.paypal.com/sdk/js"); + window.location = new URL( + "https://www.paypal.com/sdk/js" + ) as unknown as Location; const domain = getDomain(); const expectedDomain = `${window.location.protocol}//${window.location.host}`; diff --git a/test/getParent.test.ts b/test/getParent.test.ts index 6bddf0b..e4ad0d0 100644 --- a/test/getParent.test.ts +++ b/test/getParent.test.ts @@ -15,7 +15,7 @@ test("getParent should get the parent window if there is one", () => { test("getParent should not get the parent window if the parent is the same window", () => { const win = getCrossDomainWindow({}); - // @ts-ignore + // @ts-expect-error window.parent is readonly win.parent = win; const parent = getParent(win); diff --git a/test/getParents.test.ts b/test/getParents.test.ts index f4582a2..6badc96 100644 --- a/test/getParents.test.ts +++ b/test/getParents.test.ts @@ -13,7 +13,7 @@ test("getParents should get all of a windows parents", () => { }, }); - // @ts-ignore + // @ts-expect-error window.parent is readonly win.parent.parent.parent.parent = win.parent.parent.parent; const parents = getParents(win); diff --git a/test/isBlankDomain.test.ts b/test/isBlankDomain.test.ts index f67562a..0507d15 100644 --- a/test/isBlankDomain.test.ts +++ b/test/isBlankDomain.test.ts @@ -13,7 +13,8 @@ test("isBlankDomain returns true if window.href is falsy", () => { }, }) ); - // @ts-ignore + + // @ts-expect-error Error Argument of type '(win: Window) => boolean' is not assignable to parameter of type '(value: Window | SameDomainWindowType const results = windows.map(isBlankDomain); const expectedResult = true; assert( @@ -29,7 +30,7 @@ test("isBlankDomain returns true if window.href about:blank", () => { }, }); const expectedResult = true; - // @ts-ignore + // @ts-expect-error Error Argument of type '(win: Window) => boolean' is not assignable to parameter of type '(value: Window | SameDomainWindowType const result = isBlankDomain(win); assert( result === expectedResult, @@ -44,7 +45,7 @@ test("isBlankDomain should return false if window.href is truthy but not about:b }, }); const expectedResult = false; - // @ts-ignore + // @ts-expect-error Error Argument of type '(win: Window) => boolean' is not assignable to parameter of type '(value: Window | SameDomainWindowType const result = isBlankDomain(win); assert( result === expectedResult, diff --git a/test/isSameDomain.test.ts b/test/isSameDomain.test.ts index 84a41ad..73d51a0 100644 --- a/test/isSameDomain.test.ts +++ b/test/isSameDomain.test.ts @@ -5,8 +5,9 @@ import { isSameDomain } from "../src"; import { getSameDomainWindow } from "./utils"; test("isSameDomain should give a positive result for isSameDomain", () => { - // @ts-ignore - window.location = new URL("http://www.paypal.com/sdk/js"); + window.location = new URL( + "http://www.paypal.com/sdk/js" + ) as unknown as Location; const win = getSameDomainWindow({ location: { @@ -37,6 +38,7 @@ test("isSameDomain should give a negative result for isSameDomain with a differe return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -59,6 +61,7 @@ test("isSameDomain should give a negative result for isSameDomain with a differe return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -75,11 +78,13 @@ test("isSameDomain should give a negative result for isSameDomain with a differe host: "foobar.com:12345", }, }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -99,11 +104,13 @@ test("isSameDomain should give a negative result for isSameDomain when an error host: window.location.host, }, }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -123,11 +130,13 @@ test("isSameDomain should give a negative result for isSameDomain when an error }, }, }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -139,6 +148,7 @@ test("isSameDomain should give a negative result for isSameDomain when an error test("isSameDomain should give a negative result for isSameDomain when location is non-enumerable", () => { const win = getSameDomainWindow({}); + Object.defineProperty(win, "location", { value: { protocol: window.location.protocol, @@ -146,11 +156,13 @@ test("isSameDomain should give a negative result for isSameDomain when location }, enumerable: false, }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; @@ -161,10 +173,8 @@ test("isSameDomain should give a negative result for isSameDomain when location }); test("isSameDomain should give a positive result for isSameDomain when mockDomain matches", () => { - // @ts-ignore + // @ts-expect-error win.mockDomain is our own var window.mockDomain = "mock://foobar.com:12345"; - // @ts-ignore - window.location = new URL("mock://foobar.com:12345"); const win = getSameDomainWindow({ location: { @@ -173,11 +183,13 @@ test("isSameDomain should give a positive result for isSameDomain when mockDomai }, mockDomain: "mock://foobar.com:12345", }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = true; @@ -188,8 +200,9 @@ test("isSameDomain should give a positive result for isSameDomain when mockDomai }); test("isSameDomain should give a negative result for isSameDomain when mockDomain does not match", () => { - // @ts-ignore + // @ts-expect-error win.mockDomain is our own var window.mockDomain = "mock://fizzbuzz.com:345"; + const win = getSameDomainWindow({ location: { protocol: window.location.protocol, @@ -197,11 +210,13 @@ test("isSameDomain should give a negative result for isSameDomain when mockDomai }, mockDomain: "mock://foobar.com:12345", }); + Object.defineProperty(win.location, "href", { get(): string { return `${win.location.protocol}//${win.location.host}`; }, }); + const result = isSameDomain(win); const expectedResult = false; diff --git a/test/utils.ts b/test/utils.ts index ae95432..8f68697 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -3,15 +3,16 @@ import type { CrossDomainWindowType, SameDomainWindowType } from "../src/types"; export function getCrossDomainWindow( options: Record ): CrossDomainWindowType { - // @ts-ignore not a true window type + // @ts-expect-error not a true window type return { ...options, }; } + export function getSameDomainWindow( options: Record ): CrossDomainWindowType | SameDomainWindowType | Window { - // @ts-ignore not a true window type + // @ts-expect-error not a true window type return { ...options, }; diff --git a/webpack.config.js b/webpack.config.js deleted file mode 100644 index 71293d6..0000000 --- a/webpack.config.js +++ /dev/null @@ -1,40 +0,0 @@ -/* @flow */ -/* eslint import/no-nodejs-modules: off */ - -import type { WebpackConfig } from "@krakenjs/webpack-config-grumbler/index.flow"; -import { getWebpackConfig } from "@krakenjs/webpack-config-grumbler"; - -let FILE_NAME = "cross-domain-utils"; -let MODULE_NAME = "crossDomainUtils"; - -export let WEBPACK_CONFIG: WebpackConfig = getWebpackConfig({ - filename: `${FILE_NAME}.js`, - modulename: MODULE_NAME, - minify: false, - vars: { - __MIN__: false, - __TEST__: false, - }, -}); - -export let WEBPACK_CONFIG_MIN: WebpackConfig = getWebpackConfig({ - filename: `${FILE_NAME}.min.js`, - modulename: MODULE_NAME, - minify: true, - vars: { - __MIN__: true, - __TEST__: false, - }, -}); - -export let WEBPACK_CONFIG_TEST: WebpackConfig = getWebpackConfig({ - modulename: MODULE_NAME, - options: { - devtool: "inline-source-map", - }, - vars: { - __TEST__: true, - }, -}); - -export default [WEBPACK_CONFIG, WEBPACK_CONFIG_MIN];