Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(highlight): highlight should work even if the attribute is missing #1791

Merged
merged 1 commit into from
Jan 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/react-instantsearch/src/core/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ export default function parseAlgoliaHit({
}) {
if (!hit) throw new Error('`hit`, the matching record, must be provided');

const highlightedValue = get(hit._highlightResult, attributeName);
if (!highlightedValue) throw new Error(
`\`attributeName\`=${attributeName} must resolve to an highlighted attribute in the record`);
const highlightObject = get(hit._highlightResult, attributeName);
const highlightedValue = !highlightObject ? '' : highlightObject.value;

return parseHighlightedAttribute({preTag, postTag, highlightedValue: highlightedValue.value});
return parseHighlightedAttribute({preTag, postTag, highlightedValue});
}

/**
Expand Down
15 changes: 6 additions & 9 deletions packages/react-instantsearch/src/core/highlight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import parseAlgoliaHit from './highlight.js';

describe('parseAlgoliaHit()', () => {
it('it does not break when there is a missing attribute', () => {
const attributeName = 'attr';
const out = parseAlgoliaHit({attributeName, hit: {}});
expect(out).toEqual([]);
});

it('creates a single element when there is no tag', () => {
const value = 'foo bar baz';
const attributeName = 'attr';
Expand Down Expand Up @@ -61,15 +67,6 @@ describe('parseAlgoliaHit()', () => {
]);
});

it('throws when the attribute is not highlighted in the hit', () => {
expect(parseAlgoliaHit.bind(null, {
attributeName: 'notHighlightedAttribute',
hit: {notHighlightedAttribute: 'some value'},
})).toThrowError(
'`attributeName`=notHighlightedAttribute must resolve to an highlighted attribute in the record'
);
});

it('throws when hit is `null`', () => {
expect(parseAlgoliaHit.bind(null, {
attributeName: 'unknownattribute',
Expand Down