Skip to content

Commit

Permalink
add test for getAppIdAndApiKey
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Aug 24, 2020
1 parent 21faadb commit 7f49f25
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"@wdio/spec-reporter": "5.16.5",
"@wdio/static-server-service": "5.16.5",
"algoliasearch": "4.3.1",
"algoliasearch-v3": "npm:algoliasearch@3",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
"babel-loader": "8.0.6",
Expand Down
22 changes: 22 additions & 0 deletions src/lib/utils/__tests__/getAppIdAndApiKey-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getAppIdAndApiKey } from '../getAppIdAndApiKey';
import algoliasearchV4 from 'algoliasearch';
import algoliasearchV3 from 'algoliasearch-v3';

const APP_ID = 'myAppId';
const API_KEY = 'myApiKey';

describe('getAppIdAndApiKey', () => {
it('gets appId and apiKey from searchClient@v4', () => {
const searchClient = algoliasearchV4(APP_ID, API_KEY);
const [appId, apiKey] = getAppIdAndApiKey(searchClient);
expect(appId).toEqual(APP_ID);
expect(apiKey).toEqual(API_KEY);
});

it('gets appId and apiKey from searchClient@v3', () => {
const searchClient = algoliasearchV3(APP_ID, API_KEY);
const [appId, apiKey] = getAppIdAndApiKey(searchClient);
expect(appId).toEqual(APP_ID);
expect(apiKey).toEqual(API_KEY);
});
});
14 changes: 14 additions & 0 deletions src/lib/utils/getAppIdAndApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function getAppIdAndApiKey(searchClient) {
if (searchClient.transporter) {
// searchClient v4
const { headers, queryParameters } = searchClient.transporter;
const APP_ID = 'x-algolia-application-id';
const API_KEY = 'x-algolia-api-key';
const appId = headers[APP_ID] || queryParameters[APP_ID];
const apiKey = headers[API_KEY] || queryParameters[API_KEY];
return [appId, apiKey];
} else {
// searchClient v3
return [searchClient.applicationID, searchClient.apiKey];
}
}
1 change: 1 addition & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ export {
} from './geo-search';
export { addAbsolutePosition } from './hits-absolute-position';
export { addQueryID } from './hits-query-id';
export { getAppIdAndApiKey } from './getAppIdAndApiKey';
17 changes: 1 addition & 16 deletions src/middleware/insights.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InsightsClient, Middleware } from '../types';
import { getInsightsAnonymousUserToken } from '../helpers';
import { warning, noop } from '../lib/utils';
import { warning, noop, getAppIdAndApiKey } from '../lib/utils';

export type InsightsProps = {
insightsClient: null | InsightsClient;
Expand Down Expand Up @@ -97,18 +97,3 @@ aa('setUserToken', 'your-user-token');
};
};
};

function getAppIdAndApiKey(searchClient) {
if (searchClient.transporter) {
// searchClient v4
const { headers, queryParameters } = searchClient.transporter;
const APP_ID = 'x-algolia-application-id';
const API_KEY = 'x-algolia-api-key';
const appId = headers[APP_ID] || queryParameters[APP_ID];
const apiKey = headers[API_KEY] || queryParameters[API_KEY];
return [appId, apiKey];
} else {
// searchClient v3
return [searchClient.applicationID, searchClient.apiKey];
}
}
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3573,6 +3573,27 @@ algoliasearch-helper@^3.2.2:
dependencies:
events "^1.1.1"

"algoliasearch-v3@npm:algoliasearch@3":
version "3.35.1"
resolved "https://registry.npmjs.org/algoliasearch/-/algoliasearch-3.35.1.tgz#297d15f534a3507cab2f5dfb996019cac7568f0c"
integrity sha512-K4yKVhaHkXfJ/xcUnil04xiSrB8B8yHZoFEhWNpXg23eiCnqvTZw1tn/SqvdsANlYHLJlKl0qi3I/Q2Sqo7LwQ==
dependencies:
agentkeepalive "^2.2.0"
debug "^2.6.9"
envify "^4.0.0"
es6-promise "^4.1.0"
events "^1.1.0"
foreach "^2.0.5"
global "^4.3.2"
inherits "^2.0.1"
isarray "^2.0.1"
load-script "^1.0.0"
object-keys "^1.0.11"
querystring-es3 "^0.2.1"
reduce "^1.0.1"
semver "^5.1.0"
tunnel-agent "^0.6.0"

[email protected]:
version "4.3.1"
resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-4.3.1.tgz#dea6ad87705e0439855cf3e5a4406b74e794b874"
Expand Down

0 comments on commit 7f49f25

Please sign in to comment.