-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): expose window.AlgoliaAnalyticsObject if not yet set (#390)
This allows us to detect a search-insights instance, regardless of whether it's using umd or cjs entry points. FX-2264
- Loading branch information
Showing
6 changed files
with
72 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createInsightsClient } from "../_createInsightsClient"; | ||
|
||
describe("createInsightsClient", () => { | ||
beforeEach(() => { | ||
window.AlgoliaAnalyticsObject = undefined; | ||
}); | ||
|
||
it("should return a function", () => { | ||
expect(typeof createInsightsClient(() => {})).toBe("function"); | ||
}); | ||
|
||
it("registers itself to window", () => { | ||
const aa = createInsightsClient(() => {}); | ||
|
||
expect(typeof window.AlgoliaAnalyticsObject).toBe("string"); | ||
expect(window[window.AlgoliaAnalyticsObject!]).toBe(aa); | ||
}); | ||
|
||
it("doesn't register itself to window if key is already taken", () => { | ||
window.AlgoliaAnalyticsObject = "existingKey"; | ||
const aa = createInsightsClient(() => {}); | ||
|
||
expect(window.AlgoliaAnalyticsObject).toBe("existingKey"); | ||
expect(window[window.AlgoliaAnalyticsObject!]).not.toBe(aa); | ||
}); | ||
|
||
it("doesn't register if no window", () => { | ||
delete (global as any).window; | ||
|
||
expect(typeof createInsightsClient(() => {})).toBe("function"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import AlgoliaAnalytics from "./insights"; | ||
import { getFunctionalInterface } from "./_getFunctionalInterface"; | ||
import { RequestFnType } from "./utils/request"; | ||
import AlgoliaAnalytics from './insights'; | ||
import { getFunctionalInterface } from './_getFunctionalInterface'; | ||
import { RequestFnType } from './utils/request'; | ||
import { createUUID } from './utils/uuid'; | ||
|
||
export function createInsightsClient(requestFn: RequestFnType) { | ||
return getFunctionalInterface(new AlgoliaAnalytics({ requestFn })); | ||
const aa = getFunctionalInterface(new AlgoliaAnalytics({ requestFn })); | ||
|
||
if (typeof window === 'object') { | ||
if (!window.AlgoliaAnalyticsObject) { | ||
let pointer: string; | ||
do { | ||
pointer = createUUID(); | ||
} while (window[pointer] !== undefined); | ||
window.AlgoliaAnalyticsObject = pointer; | ||
window[window.AlgoliaAnalyticsObject] = aa; | ||
} | ||
} | ||
|
||
return aa; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters