Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(store): add user agent to client
Browse files Browse the repository at this point in the history
Closes: #12
  • Loading branch information
rayrutjes committed Apr 13, 2017
1 parent 8447a6c commit 5b1e469
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
54 changes: 36 additions & 18 deletions packages/instantsearch-store/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,42 @@ test('HIGHLIGHT_POST_TAG should be "__/ais-highlight__"', () => {
});

test('can assert that a facet type is valid', () => {
expect( () => { assertValidFacetType(FACET_AND) } ).not.toThrow()
expect( () => { assertValidFacetType(FACET_OR) } ).not.toThrow()
expect( () => { assertValidFacetType(FACET_TREE) } ).not.toThrow()
expect( () => { assertValidFacetType('unknown') } ).toThrow()
expect(() => {
assertValidFacetType(FACET_AND);
}).not.toThrow();
expect(() => {
assertValidFacetType(FACET_OR);
}).not.toThrow();
expect(() => {
assertValidFacetType(FACET_TREE);
}).not.toThrow();
expect(() => {
assertValidFacetType('unknown');
}).toThrow();
});

test('can create a Store instance from algolia credentials', () => {
const store = createFromAlgoliaCredentials('app_id', 'api_key');
expect(store).toBeInstanceOf(Store)
expect(store.algoliaApiKey).toBe('api_key')
expect(store.algoliaAppId).toBe('app_id')
})
expect(store).toBeInstanceOf(Store);
expect(store.algoliaApiKey).toBe('api_key');
expect(store.algoliaAppId).toBe('app_id');
});

test('can create a Store instance from an algolia client', () => {
const client = algoliaClient('app_id', 'api_key');
const store = createFromAlgoliaClient(client);
expect(store).toBeInstanceOf(Store)
expect(store.algoliaClient).toBe(client)
})
expect(store).toBeInstanceOf(Store);
expect(store.algoliaClient).toBe(client);
});

describe('Store', () => {

test('should be constructed with a helper instance', () => {
const client = algoliaClient('app_id', 'api_key');
const helper = algoliaHelper(client);
const store = new Store(helper);

expect(store.algoliaHelper).toBe(helper);
})
});

test('should always use custom highlighting tags', () => {
const client = algoliaClient('app_id', 'api_key');
Expand All @@ -71,15 +78,15 @@ describe('Store', () => {

expect(store.highlightPreTag).toEqual(HIGHLIGHT_PRE_TAG);
expect(store.highlightPostTag).toEqual(HIGHLIGHT_POST_TAG);
})
});

test('can retrieve index name', () => {
const client = algoliaClient('app_id', 'api_key');
const helper = algoliaHelper(client, 'my_index');
const store = new Store(helper);

expect(store.indexName).toEqual('my_index');
})
});

test('can set index name', () => {
const client = algoliaClient('app_id', 'api_key');
Expand All @@ -89,7 +96,7 @@ describe('Store', () => {
store.indexName = 'custom_index_name';

expect(store.indexName).toEqual('custom_index_name');
})
});

test('can retrieve the current page', () => {
const client = algoliaClient('app_id', 'api_key');
Expand All @@ -98,7 +105,7 @@ describe('Store', () => {
const store = new Store(helper);

expect(store.page).toEqual(2);
})
});

test('can change the current page', () => {
const client = algoliaClient('app_id', 'api_key');
Expand All @@ -109,6 +116,17 @@ describe('Store', () => {

expect(helper.getPage()).toEqual(1);
expect(store.page).toEqual(2);
})
});

test('should add "vue-instantsearch" User Agent to the client with the current version', () => {
const addAlgoliaAgent = jest.fn();
const client = {
addAlgoliaAgent
};

const helper = algoliaHelper(client);
const store = new Store(helper);
const version = require('../../package.json').version;
expect(addAlgoliaAgent).toBeCalledWith(`vue-instantsearch ${version}`);
});
});
6 changes: 4 additions & 2 deletions packages/instantsearch-store/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import algolia from 'algoliasearch'
import algoliaHelper from 'algoliasearch-helper'
import { version } from '../package.json'



export const FACET_AND = 'and'
export const FACET_OR = 'or'
Expand Down Expand Up @@ -63,8 +66,7 @@ export class Store {

this._helper.on('change', onHelperChange.bind(this))

// Todo: fetch the version somehow.
this._helper.getClient().addAlgoliaAgent('Store (x.x.x)')
this._helper.getClient().addAlgoliaAgent(`vue-instantsearch ${version}`)
}

get highlightPreTag() {
Expand Down

0 comments on commit 5b1e469

Please sign in to comment.