-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
373d341
commit e5d7bec
Showing
5 changed files
with
220 additions
and
97 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,69 @@ | ||
import createStateManager from '../createStateManager.js'; | ||
|
||
describe('stateManager', () => { | ||
describe('get()', () => { | ||
it('should get userData from algolia', async () => { | ||
const mock = { | ||
getSettings: jest.fn(() => { | ||
return { | ||
userData: 'foobar', | ||
}; | ||
}), | ||
}; | ||
const stateManager = createStateManager(mock); | ||
const userData = await stateManager.get(); | ||
|
||
expect(mock.getSettings).toHaveBeenCalled(); | ||
expect(userData).toBe('foobar'); | ||
}); | ||
}); | ||
|
||
describe('set()', () => { | ||
it('should set userData to algolia', async () => { | ||
const mock = { | ||
setSettings: jest.fn(), | ||
}; | ||
const stateManager = createStateManager(mock); | ||
await stateManager.set('state'); | ||
|
||
expect(mock.setSettings).toHaveBeenCalledWith({ | ||
userData: 'state', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('reset()', () => { | ||
it('should reset userData', async () => { | ||
const mock = { | ||
setSettings: jest.fn(), | ||
}; | ||
const stateManager = createStateManager(mock); | ||
await stateManager.reset(); | ||
|
||
expect(mock.setSettings).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('save()', () => { | ||
it('should save userData to algolia', async () => { | ||
const mock = { | ||
getSettings: jest.fn(() => { | ||
return { | ||
userData: { bar: 'foo' }, | ||
}; | ||
}), | ||
setSettings: jest.fn(), | ||
}; | ||
const stateManager = createStateManager(mock); | ||
await stateManager.save({ foo: 'bar' }); | ||
|
||
expect(mock.getSettings).toHaveBeenCalled(); | ||
expect(mock.setSettings).toHaveBeenCalledWith({ | ||
userData: { | ||
bar: 'foo', | ||
foo: 'bar', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
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,63 @@ | ||
import algoliasearch from 'algoliasearch'; | ||
|
||
function createClient(appId, apiKey, indexName) { | ||
const client = algoliasearch(appId, apiKey); | ||
return { | ||
index: client.initIndex(indexName), | ||
client, | ||
}; | ||
} | ||
|
||
/** | ||
* Prepare algolia for indexing | ||
* @param {object} config | ||
*/ | ||
async function prepare(config) { | ||
if (!config.apiKey) { | ||
throw new Error( | ||
'npm-search: Please provide the `apiKey` env variable and restart' | ||
); | ||
} | ||
|
||
// Get main index and boostrap algolia client | ||
const { index: mainIndex, client } = createClient( | ||
config.appId, | ||
config.apiKey, | ||
config.indexName | ||
); | ||
const { index: bootstrapIndex } = createClient( | ||
config.appId, | ||
config.apiKey, | ||
config.bootstrapIndexName | ||
); | ||
|
||
// Ensure indices exists by calling an empty setSettings() | ||
await mainIndex.setSettings({}); | ||
await bootstrapIndex.setSettings({}); | ||
|
||
return { | ||
client, | ||
mainIndex, | ||
bootstrapIndex, | ||
}; | ||
} | ||
|
||
/** | ||
* | ||
* @param {AlgoliasearchIndex} index | ||
* @param {object} config | ||
*/ | ||
async function putDefaultSettings(index, config) { | ||
await index.setSettings(config.indexSettings); | ||
|
||
await index.batchSynonyms(config.indexSynonyms, { | ||
replaceExistingSynonyms: true, | ||
}); | ||
const { taskID } = await index.batchRules(config.indexRules, { | ||
replaceExistingRules: true, | ||
}); | ||
|
||
await index.waitTask(taskID); | ||
} | ||
|
||
export { prepare, putDefaultSettings }; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.