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

feat(reverseHighlight/reverseSnippet): Implements reverseHighlight and reverseSnippet #4592

Merged
merged 22 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7baa09d
feat(highlight): accept array for attribute
Haroenv Nov 23, 2020
1a66d30
allow number in type
Haroenv Nov 24, 2020
31f6c64
Implement reverseHighlight helper
shortcuts Nov 24, 2020
c741b44
Implement reverseHighlight helper
shortcuts Nov 24, 2020
94fd93d
Implement reverseSnippet, rebase from `feat/highlight-array`, clearne…
shortcuts Nov 24, 2020
c479f7f
Implement reverseSnippet, rebase from `feat/highlight-array`, clearne…
shortcuts Nov 24, 2020
a8b8e74
Moved `getReverseHighlight` function to utils
shortcuts Nov 24, 2020
d6a59b1
Add tests for `getReversedHighlight` and `unescape`
shortcuts Nov 24, 2020
1ce4538
Add `@MAJOR` comments from feat/highlight-array
shortcuts Nov 26, 2020
fc9b985
Merge branch 'master' into feat/reverseHighlight
shortcuts Nov 26, 2020
ca9ab81
Resolve conflicts on newest files
shortcuts Nov 26, 2020
f842b61
Remove `getReversedHighlight`, add `getHighlightedParts` and `reverse…
shortcuts Dec 3, 2020
1b53d3f
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 3, 2020
00f664a
Remove tmp classes to prevent duplicate with `instantsearch-specs`, r…
shortcuts Dec 8, 2020
5294e82
Merge branch 'master' of github.com:algolia/instantsearch.js into fea…
shortcuts Dec 8, 2020
1e9f3aa
Add tests for `reverseSnippet` and `reverseHighlight` based on the si…
shortcuts Dec 10, 2020
3167129
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 10, 2020
0a47dd2
Increase bundle size limit from 65.25KB to 65.5KB
shortcuts Dec 11, 2020
51d39fd
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 21, 2020
05d4a33
Inlined function export and test snapshots, moved `isAlphanumeric` re…
shortcuts Dec 21, 2020
2c851c5
fix regex naming from `isAlphanumeric` to `hasAlphanumeric`
shortcuts Dec 22, 2020
a88b614
Merge branch 'master' into feat/reverseHighlight
shortcuts Dec 22, 2020
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"bundlesize": [
{
"path": "./dist/instantsearch.production.min.js",
"maxSize": "65.25 kB"
"maxSize": "65.5 kB"
},
{
"path": "./dist/instantsearch.development.js",
Expand Down
203 changes: 203 additions & 0 deletions src/helpers/__tests__/reverseHighlight-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import reverseHighlight from '../reverseHighlight';

const NONE = 'none' as const;
const FULL = 'full' as const;

/* eslint-disable @typescript-eslint/camelcase */
const hit = {
name: 'Amazon - Fire TV Stick with Alexa Voice Remote - Black',
description:
'Enjoy smart access to videos, games and apps with this Amazon Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core processor, 1GB internal memory and 8GB of storage, this portable Amazon Fire TV stick works fast for buffer-free streaming.',
brand: 'Amazon',
categories: ['TV & Home Theater', 'Streaming Media Players'],
hierarchicalCategories: {
lvl0: 'TV & Home Theater',
lvl1: 'TV & Home Theater > Streaming Media Players',
},
type: 'Streaming - (media plyr)',
price: 39.99,
price_range: '1 - 50',
image: 'https://cdn-demo.algolia.com/bestbuy-0118/5477500_sb.jpg',
url: 'https://api.bestbuy.com/click/-/5477500/pdp',
free_shipping: false,
rating: 4,
popularity: 21469,
objectID: '5477500',
_highlightResult: {
name: {
value:
'<mark>Amazon</mark> - Fire TV Stick with Alexa Voice Remote - Black',
matchLevel: FULL,
fullyHighlighted: false,
matchedWords: ['amazon'],
},
description: {
value:
'Enjoy smart access to videos, games and apps with this <mark>Amazon</mark> Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core <mark>processor</mark>, <mark>1GB</mark> internal memory and 8GB of storage, this portable <mark>Amazon</mark> Fire TV stick works fast for buffer-free streaming.',
matchLevel: FULL,
fullyHighlighted: false,
matchedWords: ['amazon', 'processor', '1GB'],
},
brand: {
value: '<mark>Amazon</mark>',
matchLevel: FULL,
fullyHighlighted: true,
matchedWords: ['amazon'],
},
categories: [
{
value: 'TV & Home Theater',
matchLevel: NONE,
matchedWords: [],
},
{
value: 'Streaming Media Players',
matchLevel: NONE,
matchedWords: [],
},
],
type: {
value: '<mark>Streaming</mark> - (<mark>media</mark> plyr)',
matchLevel: FULL,
fullyHighlighted: false,
matchedWords: ['streaming', 'media'],
},
typeMissingSibling: {
value: 'Streaming - (<mark>media</mark> plyr)',
matchLevel: FULL,
fullyHighlighted: false,
matchedWords: ['media'],
},
typeFullMatch: {
value: '<mark>Streaming</mark> - (<mark>media</mark> <mark>plyr</mark>)',
matchLevel: FULL,
fullyHighlighted: false,
matchedWords: ['streaming', 'media', 'plyr'],
},
meta: {
name: {
value: 'Nested <mark>Amazon</mark> name',
matchLevel: NONE,
matchedWords: ['Amazon'],
},
},
},
};
/* eslint-enable @typescript-eslint/camelcase */

describe('reverseHighlight', () => {
test('with default tag name', () => {
expect(
reverseHighlight({
attribute: 'name',
hit,
})
).toMatchInlineSnapshot(
`"Amazon<mark class=\\"ais-ReverseHighlight-highlighted\\"> - Fire TV Stick with Alexa Voice Remote - Black</mark>"`
);
});

test('with full match', () => {
expect(
reverseHighlight({
attribute: 'typeFullMatch',
hit,
})
).toMatchInlineSnapshot(`"Streaming - (media plyr)"`);
});

test('with custom tag name', () => {
expect(
reverseHighlight({
attribute: 'description',
highlightedTagName: 'em',
hit,
})
).toMatchInlineSnapshot(
`"<em class=\\"ais-ReverseHighlight-highlighted\\">Enjoy smart access to videos, games and apps with this </em>Amazon<em class=\\"ais-ReverseHighlight-highlighted\\"> Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core </em>processor, 1GB<em class=\\"ais-ReverseHighlight-highlighted\\"> internal memory and 8GB of storage, this portable </em>Amazon<em class=\\"ais-ReverseHighlight-highlighted\\"> Fire TV stick works fast for buffer-free streaming.</em>"`
);
});

test('with custom highlighted class name', () => {
expect(
reverseHighlight({
attribute: 'description',
cssClasses: { highlighted: '__highlighted class' },
hit,
})
).toMatchInlineSnapshot(
`"<mark class=\\"ais-ReverseHighlight-highlighted __highlighted class\\">Enjoy smart access to videos, games and apps with this </mark>Amazon<mark class=\\"ais-ReverseHighlight-highlighted __highlighted class\\"> Fire TV stick. Its Alexa voice remote lets you deliver hands-free commands when you want to watch television or engage with other applications. With a quad-core </mark>processor, 1GB<mark class=\\"ais-ReverseHighlight-highlighted __highlighted class\\"> internal memory and 8GB of storage, this portable </mark>Amazon<mark class=\\"ais-ReverseHighlight-highlighted __highlighted class\\"> Fire TV stick works fast for buffer-free streaming.</mark>"`
);
});

test('with unknown attribute returns an empty string', () => {
expect(
reverseHighlight({
attribute: 'wrong-attribute',
hit,
})
).toMatchInlineSnapshot(`""`);
});

test('with nested attribute', () => {
expect(
reverseHighlight({
attribute: 'meta.name',
hit,
})
).toMatchInlineSnapshot(
`"<mark class=\\"ais-ReverseHighlight-highlighted\\">Nested </mark>Amazon<mark class=\\"ais-ReverseHighlight-highlighted\\"> name</mark>"`
);
});

test('with nested attribute as array', () => {
expect(
reverseHighlight({
attribute: ['meta', 'name'],
hit,
})
).toMatchInlineSnapshot(
`"<mark class=\\"ais-ReverseHighlight-highlighted\\">Nested </mark>Amazon<mark class=\\"ais-ReverseHighlight-highlighted\\"> name</mark>"`
);
});

test('with array attribute', () => {
expect(
reverseHighlight({
attribute: 'categories.1',
hit,
})
).toMatchInlineSnapshot(`"Streaming Media Players"`);
});

test('with array attribute as array', () => {
expect(
reverseHighlight({
attribute: ['categories', '1'],
hit,
})
).toMatchInlineSnapshot(`"Streaming Media Players"`);
});

test('with non-alphanumeric character with alphanumeric siblings matching highlight', () => {
expect(
reverseHighlight({
attribute: 'type',
hit,
})
).toMatchInlineSnapshot(
`"Streaming - (media<mark class=\\"ais-ReverseHighlight-highlighted\\"> plyr)</mark>"`
);
});

test('with non-alphanumeric character with different alphanumeric siblings highlight', () => {
expect(
reverseHighlight({
attribute: 'typeMissingSibling',
hit,
})
).toMatchInlineSnapshot(
`"<mark class=\\"ais-ReverseHighlight-highlighted\\">Streaming - (</mark>media<mark class=\\"ais-ReverseHighlight-highlighted\\"> plyr)</mark>"`
);
});
});
Loading