Skip to content

Commit

Permalink
fix(algolia): fallback non-existant highlighted to default value
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 22, 2020
1 parent ce43e83 commit 7783bc6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('parseAlgoliaHitHighlight', () => {
`);
});

test('returns empty if the attribute cannot be highlighted', () => {
test('returns the attribute value if the attribute cannot be highlighted', () => {
expect(
parseAlgoliaHitHighlight({
attribute: 'description',
Expand All @@ -122,6 +122,30 @@ describe('parseAlgoliaHitHighlight', () => {
},
},
})
).toEqual([
{
value: 'Welcome all',
isHighlighted: false,
},
]);
});

test('returns empty string if the attribute does not exist', () => {
expect(
parseAlgoliaHitHighlight({
// @ts-ignore
attribute: 'description',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
},
},
},
})
).toEqual([]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('parseAlgoliaHitReverseHighlight', () => {
`);
});

test('returns empty if the attribute cannot be highlighted', () => {
test('returns the attribute value if the attribute cannot be highlighted', () => {
expect(
parseAlgoliaHitReverseHighlight({
attribute: 'description',
Expand All @@ -139,6 +139,30 @@ describe('parseAlgoliaHitReverseHighlight', () => {
},
},
})
).toEqual([
{
value: 'Welcome all',
isHighlighted: false,
},
]);
});

test('returns empty string if the attribute does not exist', () => {
expect(
parseAlgoliaHitReverseHighlight({
// @ts-ignore
attribute: 'description',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
},
},
},
})
).toEqual([]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('parseAlgoliaHitReverseSnippet', () => {
`);
});

test('returns empty if the attribute cannot be snippeted', () => {
test('returns the attribute value if the attribute cannot be snippeted', () => {
expect(
parseAlgoliaHitReverseSnippet({
attribute: 'description',
Expand All @@ -116,6 +116,30 @@ describe('parseAlgoliaHitReverseSnippet', () => {
},
},
})
).toEqual([
{
value: 'Welcome all',
isHighlighted: false,
},
]);
});

test('returns empty string if the attribute does not exist', () => {
expect(
parseAlgoliaHitReverseSnippet({
// @ts-ignore
attribute: 'description',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
},
},
},
})
).toEqual([]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('parseAlgoliaHitSnippet', () => {
`);
});

test('returns empty if the attribute cannot be snippeted', () => {
test('returns the attribute value if the attribute cannot be snippeted', () => {
expect(
parseAlgoliaHitSnippet({
attribute: 'description',
Expand All @@ -116,6 +116,30 @@ describe('parseAlgoliaHitSnippet', () => {
},
},
})
).toEqual([
{
value: 'Welcome all',
isHighlighted: false,
},
]);
});

test('returns empty string if the attribute does not exist', () => {
expect(
parseAlgoliaHitSnippet({
// @ts-ignore
attribute: 'description',
hit: {
objectID: '1',
title: 'Hello there',
_snippetResult: {
title: {
value:
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
},
},
},
})
).toEqual([]);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function parseAlgoliaHitHighlight<THit extends Hit<{}>>({
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/'
);

highlightedValue = '';
highlightedValue = getAttributeValueByPath(hit, attribute as string) || '';
}

return parseAttribute({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function parseAlgoliaHitSnippet<THit extends Hit<{}>>({
'\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/'
);

highlightedValue = '';
highlightedValue = getAttributeValueByPath(hit, attribute as string) || '';
}

return parseAttribute({
Expand Down

0 comments on commit 7783bc6

Please sign in to comment.