Skip to content

Commit

Permalink
feat(get): add _get function (#216)
Browse files Browse the repository at this point in the history
* feat(get): add _get function

* feat(get): add tests
  • Loading branch information
Eunjae Lee authored Jul 23, 2020
1 parent 02b95c3 commit 3173c9b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/__tests__/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import AlgoliaAnalytics from "../insights";

describe("get", () => {
let analyticsInstance;
beforeEach(() => {
analyticsInstance = new AlgoliaAnalytics({ requestFn: () => {} });
});

it("should get values from instance after init", () => {
const callback = jest.fn();
analyticsInstance.init({
appId: "xxx",
apiKey: "***",
region: "us"
});
analyticsInstance._get("_appId", callback);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith("xxx");

analyticsInstance._get("_apiKey", callback);
expect(callback).toHaveBeenCalledTimes(2);
expect(callback).toHaveBeenCalledWith("***");

analyticsInstance._get("_region", callback);
expect(callback).toHaveBeenCalledTimes(3);
expect(callback).toHaveBeenCalledWith("us");

analyticsInstance._get("_hasCredentials", callback);
expect(callback).toHaveBeenCalledTimes(4);
expect(callback).toHaveBeenCalledWith(true);
});
});
13 changes: 13 additions & 0 deletions lib/__tests__/harvest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe("Integration tests", () => {
it("should retrieve a queryID on page load", async () => {
expect(data).toHaveProperty("queryID");
});

it("should generate an anonymous userToken on init and store it in a cookie", async () => {
const userToken = await page.evaluate(
() =>
Expand Down Expand Up @@ -157,6 +158,18 @@ describe("Integration tests", () => {
);
expect(userToken).toEqual("user-id-1");
});

it("should get _hasCredentials from the instance", async () => {
const hasCredentials = await page.evaluate(
() =>
new Promise((resolve, reject) => {
window.aa("_get", "_hasCredentials", hasCredentials => {
resolve(hasCredentials);
});
})
);
expect(hasCredentials).toBe(true);
});
});

describe("click", () => {
Expand Down
5 changes: 5 additions & 0 deletions lib/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GetCallback = (value: any) => void;

export function get(key: string, callback: GetCallback) {
callback(this[key]);
}
5 changes: 5 additions & 0 deletions lib/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ objectAssignPolyfill();
import { makeSendEvent, InsightsEventType, InsightsEvent } from "./_sendEvent";

import { InitParams, init } from "./init";
import { get, GetCallback } from "./get";
import { initSearch, InitSearchParams } from "./_initSearch";
import { addAlgoliaAgent } from "./_algoliaAgent";

Expand Down Expand Up @@ -117,6 +118,8 @@ class AlgoliaAnalytics {
public viewedObjectIDs: (params: InsightsSearchViewObjectIDsEvent) => void;
public viewedFilters: (params: InsightsSearchViewFiltersEvent) => void;

public _get: (key: string, callback: GetCallback) => void;

constructor({ requestFn }: { requestFn: RequestFnType }) {
// Bind private methods to `this` class
this.sendEvent = makeSendEvent(requestFn).bind(this);
Expand Down Expand Up @@ -144,6 +147,8 @@ class AlgoliaAnalytics {

this.viewedObjectIDs = viewedObjectIDs.bind(this);
this.viewedFilters = viewedFilters.bind(this);

this._get = get.bind(this);
}
}

Expand Down

0 comments on commit 3173c9b

Please sign in to comment.