-
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(UserAgent): introduce addAlgoliaAgent method (#105)
* feat(UserAgent): introduce addAlgoliaAgent method This introduces a addAlgoliaAgent method that can be also found in the search client. ```ts aa('addAlgoliaAgent', 'a random string') ``` * feat(UserAgent): add space after ;
- Loading branch information
Showing
5 changed files
with
68 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import AlgoliaInsights from "../insights"; | ||
|
||
jest.mock("../../package.json", () => ({ version: "1.0.1" })); | ||
|
||
describe("algoliaAgent", () => { | ||
beforeEach(() => { | ||
AlgoliaInsights.init({ apiKey: "test", appId: "test" }); | ||
}); | ||
|
||
it("should initialize the client with a default algoliaAgent string", () => { | ||
expect(AlgoliaInsights._ua).toEqual( | ||
"Algolia insights for JavaScript (1.0.1)" | ||
); | ||
expect(AlgoliaInsights._uaURIEncoded).toEqual( | ||
"Algolia%20insights%20for%20JavaScript%20(1.0.1)" | ||
); | ||
}); | ||
|
||
it("should allow adding a string to algoliaAgent", () => { | ||
AlgoliaInsights.addAlgoliaAgent("other string"); | ||
expect(AlgoliaInsights._ua).toEqual( | ||
"Algolia insights for JavaScript (1.0.1); other string" | ||
); | ||
expect(AlgoliaInsights._uaURIEncoded).toEqual( | ||
"Algolia%20insights%20for%20JavaScript%20(1.0.1)%3B%20other%20string" | ||
); | ||
}); | ||
|
||
it("should duplicate a string when added twice", () => { | ||
AlgoliaInsights.addAlgoliaAgent("duplicated string"); | ||
AlgoliaInsights.addAlgoliaAgent("duplicated string"); | ||
|
||
expect(AlgoliaInsights._ua).toEqual( | ||
"Algolia insights for JavaScript (1.0.1); duplicated string" | ||
); | ||
expect(AlgoliaInsights._uaURIEncoded).toEqual( | ||
"Algolia%20insights%20for%20JavaScript%20(1.0.1)%3B%20duplicated%20string" | ||
); | ||
}); | ||
}); |
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,10 @@ | ||
import { version } from "../package.json"; | ||
|
||
export const DEFAULT_ALGOLIA_AGENT = `Algolia insights for JavaScript (${version})`; | ||
|
||
export function addAlgoliaAgent(algoliaAgent) { | ||
if (this._ua.indexOf(`; ${algoliaAgent}`) === -1) { | ||
this._ua += `; ${algoliaAgent}`; | ||
this._uaURIEncoded = encodeURIComponent(this._ua); | ||
} | ||
} |
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