forked from algolia/instantsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): set up state management for recommendations (algolia#6100)
- Loading branch information
Showing
11 changed files
with
176 additions
and
3 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
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
37 changes: 37 additions & 0 deletions
37
packages/algoliasearch-helper/src/RecommendParameters/index.js
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,37 @@ | ||
'use strict'; | ||
|
||
/** | ||
* RecommendParameters is the data structure that contains all the information | ||
* usable for getting recommendations from the Algolia API. It doesn't do the | ||
* search itself, nor does it contains logic about the parameters. | ||
* It is an immutable object, therefore it has been created in a way that each | ||
* changes does not change the object itself but returns a copy with the | ||
* modification. | ||
* This object should probably not be instantiated outside of the helper. It | ||
* will be provided when needed. | ||
* @constructor | ||
* @classdesc contains all the parameters for recommendations | ||
* @param {RecommendParametersOptions} opts the options to create the object | ||
*/ | ||
function RecommendParameters(opts) { | ||
opts = opts || {}; | ||
this.params = opts.params || []; | ||
} | ||
|
||
RecommendParameters.prototype = { | ||
constructor: RecommendParameters, | ||
|
||
addParams: function (params) { | ||
return new RecommendParameters({ params: this.params.concat(params) }); | ||
}, | ||
|
||
removeParams: function (id) { | ||
return new RecommendParameters({ | ||
params: this.params.filter(function (param) { | ||
return param.$$id !== id; | ||
}), | ||
}); | ||
}, | ||
}; | ||
|
||
module.exports = RecommendParameters; |
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
10 changes: 10 additions & 0 deletions
10
packages/algoliasearch-helper/test/spec/RecommendParameters/constructorFn.js
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 @@ | ||
'use strict'; | ||
|
||
var RecommendParameters = require('../../../src/RecommendParameters'); | ||
|
||
test('accepts initial parameters', () => { | ||
var params = [{ $$id: '1', objectID: 'objectID', model: 'bought-together' }]; | ||
|
||
var recommendParameters = new RecommendParameters({ params }); | ||
expect(recommendParameters.params).toEqual(params); | ||
}); |
32 changes: 32 additions & 0 deletions
32
packages/algoliasearch-helper/test/spec/RecommendParameters/methods.js
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,32 @@ | ||
'use strict'; | ||
|
||
var RecommendParameters = require('../../../src/RecommendParameters'); | ||
|
||
var params1 = { $$id: '1', objectID: 'objectID1', model: 'bought-together' }; | ||
var params2 = { $$id: '2', facetName: 'brand', model: 'trending-facets' }; | ||
|
||
describe('addParams', () => { | ||
test('appends new parameters to the existing ones', () => { | ||
var recommendParameters = new RecommendParameters(); | ||
|
||
recommendParameters = recommendParameters.addParams(params1); | ||
expect(recommendParameters.params).toHaveLength(1); | ||
expect(recommendParameters.params).toEqual([params1]); | ||
|
||
recommendParameters = recommendParameters.addParams(params2); | ||
expect(recommendParameters.params).toHaveLength(2); | ||
expect(recommendParameters.params).toEqual([params1, params2]); | ||
}); | ||
}); | ||
|
||
describe('removeParams', () => { | ||
test('removes parameters for a specific id', () => { | ||
var recommendParameters = new RecommendParameters({ | ||
params: [params1, params2], | ||
}); | ||
|
||
recommendParameters = recommendParameters.removeParams('1'); | ||
expect(recommendParameters.params).toHaveLength(1); | ||
expect(recommendParameters.params).toEqual([params2]); | ||
}); | ||
}); |
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
21 changes: 21 additions & 0 deletions
21
packages/algoliasearch-helper/test/spec/algoliasearch.helper/recommendState.js
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,21 @@ | ||
'use strict'; | ||
|
||
var algoliasearchHelper = require('../../../index'); | ||
|
||
test('_recommendChange should update the recommend state', () => { | ||
var params1 = { $$id: '1', objectID: 'objectID1', model: 'bought-together' }; | ||
var params2 = { $$id: '2', facetName: 'brand', model: 'trending-facets' }; | ||
|
||
var helper = algoliasearchHelper({}, null, {}); | ||
|
||
helper._recommendChange({ state: helper.recommendState.addParams(params1) }); | ||
expect(helper.recommendState.params).toHaveLength(1); | ||
expect(helper.recommendState.params).toEqual([params1]); | ||
|
||
helper._recommendChange({ state: helper.recommendState.addParams(params2) }); | ||
expect(helper.recommendState.params).toHaveLength(2); | ||
expect(helper.recommendState.params).toEqual([params1, params2]); | ||
}); | ||
|
||
// eslint-disable-next-line jest/no-disabled-tests, jest/expect-expect | ||
test.skip('_recommendChange should trigger a change event', () => {}); |
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 |
---|---|---|
|
@@ -113,6 +113,23 @@ | |
dependencies: | ||
"@algolia/logger-common" "4.22.1" | ||
|
||
"@algolia/[email protected]": | ||
version "4.22.1" | ||
resolved "https://registry.yarnpkg.com/@algolia/recommend/-/recommend-4.22.1.tgz#861344a276748c7dee655329d21291b1670305da" | ||
integrity sha512-M3FCHKNdlDrxFs+uXZN9pWZ9j0YGe0q5GyL+f9w+GG0OXn0W16ryLHtum0sda4yTGM0MVfvAND6HXwoUvKKbVQ== | ||
dependencies: | ||
"@algolia/cache-browser-local-storage" "4.22.1" | ||
"@algolia/cache-common" "4.22.1" | ||
"@algolia/cache-in-memory" "4.22.1" | ||
"@algolia/client-common" "4.22.1" | ||
"@algolia/client-search" "4.22.1" | ||
"@algolia/logger-common" "4.22.1" | ||
"@algolia/logger-console" "4.22.1" | ||
"@algolia/requester-browser-xhr" "4.22.1" | ||
"@algolia/requester-common" "4.22.1" | ||
"@algolia/requester-node-http" "4.22.1" | ||
"@algolia/transporter" "4.22.1" | ||
|
||
"@algolia/[email protected]": | ||
version "4.22.1" | ||
resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.22.1.tgz#f04df6fe9690a071b267c77d26b83a3be9280361" | ||
|