Skip to content

Commit dbadcdb

Browse files
author
vvo
committed
fix(core): recursively merge arrays in searchParameters
fixes #80
1 parent 8b8965d commit dbadcdb

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

lib/InstantSearch.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var algoliasearchHelper = require('algoliasearch-helper');
33

44
var forEach = require('lodash/collection/forEach');
55
var merge = require('lodash/object/merge');
6+
var union = require('lodash/array/union');
67

78
class InstantSearch {
89
constructor(applicationID, searchKey, indexName, searchParameters) {
@@ -19,7 +20,16 @@ class InstantSearch {
1920
// Get the helper configuration from the widget
2021
if (widgetDefinition.getConfiguration) {
2122
var partialConfiguration = widgetDefinition.getConfiguration(this.searchParameters);
22-
this.searchParameters = merge({}, this.searchParameters, partialConfiguration);
23+
this.searchParameters = merge(
24+
{},
25+
this.searchParameters,
26+
partialConfiguration,
27+
(a, b) => {
28+
if (Array.isArray(a)) {
29+
return union(a, b);
30+
}
31+
}
32+
);
2333
}
2434
// Add the widget to the list of widget
2535
this.widgets.push(widgetDefinition);

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"lint": "./scripts/lint.sh",
99
"build": "./scripts/build.sh",
1010
"dev": "./scripts/dev.sh",
11-
"test": "babel-node test/*.js && npm run lint",
12-
"test:watch": "nodemon -i node_modules/ -i coverage/ -i .git/ -i dist/ -q --exec 'babel-node test/*.js | tap-spec'",
13-
"test:coverage": "babel-node node_modules/.bin/isparta cover test/*.js",
11+
"test": "./scripts/test.sh && npm run lint",
12+
"test:watch": "nodemon -i node_modules/ -i coverage/ -i .git/ -i dist/ -q --exec './scripts/test.sh | tap-spec'",
13+
"test:coverage": "babel-node node_modules/.bin/isparta cover ./scripts/test.sh",
1414
"doctoc": "doctoc --maxlevel 3 README.md"
1515
},
1616
"browserify": {
@@ -25,6 +25,7 @@
2525
"babel": "5.8.23",
2626
"babel-eslint": "4.1.1",
2727
"babel-loader": "5.3.2",
28+
"babel-tape-runner": "1.2.0",
2829
"css-loader": "0.16.0",
2930
"doctoc": "0.15.0",
3031
"eslint": "1.3.1",

scripts/test.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e # exit when error
4+
5+
printf "\nTest\n"
6+
7+
babel-tape-runner test/**/*.js

test/InstantSearch.js test/InstantSearch/lifecycle.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var test = require('tape');
22

3-
test('InstantSearch', function(t) {
3+
test('InstantSearch: lifecycle', function(t) {
44
var EventEmitter = require('events').EventEmitter;
55

66
var proxyquire = require('proxyquire');
@@ -18,7 +18,7 @@ test('InstantSearch', function(t) {
1818

1919
var appId = 'appId';
2020
var apiKey = 'apiKey';
21-
var indexName = 'test';
21+
var indexName = 'lifeycle';
2222

2323
var searchParameters = {some: 'configuration', another: {config: 'parameter'}};
2424

@@ -28,7 +28,7 @@ test('InstantSearch', function(t) {
2828
render: sinon.spy()
2929
};
3030

31-
var InstantSearch = proxyquire('../lib/InstantSearch', {
31+
var InstantSearch = proxyquire('../../lib/InstantSearch', {
3232
algoliasearch: algoliasearch,
3333
'algoliasearch-helper': algoliasearchHelper
3434
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var test = require('tape');
2+
3+
test('InstantSearch: recursively merge arrays in searchParameters', function(t) {
4+
var EventEmitter = require('events').EventEmitter;
5+
6+
var proxyquire = require('proxyquire');
7+
var sinon = require('sinon');
8+
9+
// simple mocks
10+
var helper = new EventEmitter();
11+
helper.search = noop;
12+
13+
// hijack `algoliasearch` and `algoliasearchHelper` modules
14+
var algoliasearch = noop;
15+
var algoliasearchHelper = sinon.stub().returns(helper);
16+
17+
var searchParameters = {
18+
some: ['value', 'another value'],
19+
cool: 3,
20+
another: {nested: ['array', 'of', 'values']}
21+
};
22+
23+
var firstWidget = {
24+
getConfiguration: sinon.stub().returns({
25+
first: ['values'],
26+
some: ['first'],
27+
another: {yes: true}
28+
})
29+
};
30+
31+
var secondWidget = {
32+
getConfiguration: sinon.stub().returns({
33+
first: ['new value'],
34+
cool: 4,
35+
another: {nested: ['thing'], yes: false},
36+
some: ['second']
37+
})
38+
};
39+
40+
var InstantSearch = proxyquire('../../lib/InstantSearch', {
41+
algoliasearch: algoliasearch,
42+
'algoliasearch-helper': algoliasearchHelper
43+
});
44+
45+
var search = new InstantSearch('appId', 'apiKey', 'recursively', searchParameters);
46+
47+
search.addWidget(firstWidget);
48+
search.addWidget(secondWidget);
49+
search.start();
50+
51+
var expected = {
52+
first: ['values', 'new value'],
53+
some: ['value', 'another value', 'first', 'second'],
54+
cool: 4,
55+
another: {nested: ['array', 'of', 'values', 'thing'], yes: false}
56+
};
57+
58+
t.deepEqual(
59+
algoliasearchHelper.args[0][2],
60+
expected
61+
);
62+
63+
t.end();
64+
});
65+
66+
function noop() {}

0 commit comments

Comments
 (0)