Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

fix(ua): change the User-Agent to use the new specs lib (version) #481

Merged
merged 14 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/__tests__/create-ssr-algolia-client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createSSRSearchClient } from '../create-ssr-algolia-client';
import * as algoliasearchProxy from 'algoliasearch/index';
import { VERSION } from '../version';
import { VERSION as AngularVersion } from '@angular/core';

jest.mock('algoliasearch/index');

describe('Create SSR', () => {
it('passes the User-Agent', () => {
const addAlgoliaAgent = jest.fn();
algoliasearchProxy.mockImplementation(() => {
return {
addAlgoliaAgent,
};
});

const ssrClient = createSSRSearchClient({
appId: 'test',
apiKey: 'test',
httpClient: null,
HttpHeaders: null,
makeStateKey: null,
transferState: null,
});
expect(addAlgoliaAgent).toHaveBeenCalledTimes(3);
expect(addAlgoliaAgent).toHaveBeenCalledWith(
`angular (${AngularVersion.full})`
);
expect(addAlgoliaAgent).toHaveBeenCalledWith(
`angular-instantsearch (${VERSION})`
);
expect(addAlgoliaAgent).toHaveBeenCalledWith(
`angular-instantsearch-server (${VERSION})`
);
});
});
6 changes: 4 additions & 2 deletions src/create-ssr-algolia-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as algoliasearchProxy from 'algoliasearch/index';
import * as encodeProxy from 'querystring-es3/encode';

import { VERSION as AngularVersion } from '@angular/core';
import { VERSION } from './version';

// AOT + Rollup workaround
Expand Down Expand Up @@ -39,7 +39,9 @@ export function createSSRSearchClient({
makeStateKey,
}) {
const client = algoliasearch(appId, apiKey, {});
client.addAlgoliaAgent(`angular-instantsearch ${VERSION}`);
client.addAlgoliaAgent(`angular (${AngularVersion.full})`);
client.addAlgoliaAgent(`angular-instantsearch (${VERSION})`);
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved
client.addAlgoliaAgent(`angular-instantsearch-server (${VERSION})`);

client._request = (rawUrl, opts) => {
let headers = new HttpHeaders();
Expand Down
46 changes: 46 additions & 0 deletions src/instantsearch/__tests__/instantsearch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NgAisInstantSearchModule } from '../instantsearch.module';
import { Component, VERSION as AngularVersion } from '@angular/core';
import { VERSION } from '../../version';
import { TestBed } from '@angular/core/testing';
jest.mock('instantsearch.js/es', () => ({
default: () => {
return { on: jest.fn(), start: jest.fn() };
},
}));

jest.mock('../../../src/base-widget');

describe('InstantSearch', () => {
it('should add user agent stuff when the client is provided to the config', () => {
samouss marked this conversation as resolved.
Show resolved Hide resolved
const addAlgoliaAgent = jest.fn();
@Component({
template: `<ais-instantsearch [config]="config"> </ais-instantsearch>`,
})
class TestContainer {
public config = {
appId: 'theAppId',
apiKey: 'theApiKey',
indexName: 'theIndexName',
searchClient: { addAlgoliaAgent },
};
}

TestBed.configureCompiler({ preserveWhitespaces: true } as any)
.configureTestingModule({
imports: [NgAisInstantSearchModule.forRoot()],
declarations: [TestContainer],
})
.compileComponents();

const fixture = TestBed.createComponent(TestContainer);
fixture.detectChanges();

expect(addAlgoliaAgent).toHaveBeenCalledTimes(2);
expect(addAlgoliaAgent).toHaveBeenCalledWith(
`angular (${AngularVersion.full})`
);
expect(addAlgoliaAgent).toHaveBeenCalledWith(
`angular-instantsearch (${VERSION})`
);
});
samouss marked this conversation as resolved.
Show resolved Hide resolved
});
15 changes: 8 additions & 7 deletions src/instantsearch/instantsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EventEmitter,
Inject,
PLATFORM_ID,
VERSION as AngularVersion,
} from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

Expand Down Expand Up @@ -146,6 +147,7 @@ export type SearchForFacetValuesResponse = {
};

export type SearchClient = {
addAlgoliaAgent?: (agent: string) => void;
search: (requests: SearchRequest[]) => Promise<{ results: SearchResponse[] }>;
searchForFacetValues?: (
requests: SearchForFacetValuesRequest[]
Expand Down Expand Up @@ -260,15 +262,14 @@ export class NgAisInstantSearch implements AfterViewInit, OnInit, OnDestroy {
}

// custom algolia client agent
if (!config.searchClient && !config.createAlgoliaClient) {
const client = algoliasearch(config.appId, config.apiKey);
client.addAlgoliaAgent(`angular-instantsearch ${VERSION}`);
config.searchClient =
samouss marked this conversation as resolved.
Show resolved Hide resolved
config.searchClient || algoliasearch(config.appId, config.apiKey);

config.searchClient = client;
config.appId = undefined;
config.apiKey = undefined;
if (typeof config.searchClient.addAlgoliaAgent === 'function') {
// add user agents
config.searchClient.addAlgoliaAgent(`angular (${AngularVersion.full})`);
config.searchClient.addAlgoliaAgent(`angular-instantsearch (${VERSION})`);
}

this.instantSearchInstance = instantsearch(config);
this.instantSearchInstance.on('render', this.onRender);
}
Expand Down