Skip to content

Commit

Permalink
Prevent crashing when no items provided (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
julievalet authored Mar 19, 2024
1 parent 510b158 commit 7c70c1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export const index = function (items, fields) {

let i = 1;

fields.forEach((field) => {
facets['data'][field] = Object.create(null);
});

items && items.map((item) => {
if (!item['_id']) {
item['_id'] = i;
Expand All @@ -258,15 +262,10 @@ export const index = function (items, fields) {

items && items.map((item) => {
fields.forEach((field) => {
//if (!item || !item[field]) {
if (!item) {
return;
}

if (!facets['data'][field]) {
facets['data'][field] = Object.create(null);
}

if (Array.isArray(item[field])) {
item[field].forEach((v) => {
if (!item[field]) {
Expand Down
21 changes: 20 additions & 1 deletion tests/searchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ describe('search', function () {
done();
});

it('searches no items with filters and query', function test(done) {
const itemsjs = itemsJS([], configuration);

const result = itemsjs.search({
filters: {
tags: ['a'],
category: ['drama'],
},
query: 'comedy',
});

assert.equal(result.data.items.length, 0);
assert.equal(result.data.aggregations.in_cinema.buckets.length, 0);
assert.equal(result.data.aggregations.category.buckets.length, 0);
assert.equal(result.data.aggregations.year.buckets.length, 0);

done();
});

it('searches with two filters', function test(done) {
const itemsjs = itemsJS(items, configuration);

Expand Down Expand Up @@ -244,7 +263,7 @@ describe('search', function () {
} catch (err) {
assert.equal(
err.message,
'"query" and "filter" options are not working once native search is disabled',
'"query" and "filter" options are not working once native search is disabled'
);
}

Expand Down

0 comments on commit 7c70c1e

Please sign in to comment.