Skip to content

Commit

Permalink
feat(algolia): escape values in highlighting utils
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Sep 9, 2020
1 parent 85e4b5e commit 50a9a73
Show file tree
Hide file tree
Showing 2 changed files with 408 additions and 3 deletions.
372 changes: 372 additions & 0 deletions packages/autocomplete-preset-algolia/src/__tests__/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,99 @@ describe('highlight', () => {
]
`);
});

test('allows custom highlightPreTag and highlightPostTag', () => {
expect(
parseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: '<em>He</em>llo t<em>he</em>re',
},
},
},
highlightPreTag: '<em>',
highlightPostTag: '</em>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "He",
},
Object {
"isHighlighted": false,
"value": "llo t",
},
Object {
"isHighlighted": true,
"value": "he",
},
Object {
"isHighlighted": false,
"value": "re",
},
]
`);
});

test('escapes characters', () => {
expect(
parseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "Food",
},
Object {
"isHighlighted": false,
"value": " &amp; &lt;Drinks&gt; &#39;n&#39; &quot;Music&quot;",
},
]
`);
});

test('do not escape ignored characters', () => {
expect(
parseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
ignoreEscape: ["'"],
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "Food",
},
Object {
"isHighlighted": false,
"value": " &amp; &lt;Drinks&gt; 'n' &quot;Music&quot;",
},
]
`);
});
});

describe('parseReverseHighlightedAttribute', () => {
Expand Down Expand Up @@ -98,6 +191,99 @@ describe('highlight', () => {
]
`);
});

test('allows custom highlightPreTag and highlightPostTag', () => {
expect(
parseReverseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: '<em>He</em>llo t<em>he</em>re',
},
},
},
highlightPreTag: '<em>',
highlightPostTag: '</em>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "He",
},
Object {
"isHighlighted": true,
"value": "llo t",
},
Object {
"isHighlighted": false,
"value": "he",
},
Object {
"isHighlighted": true,
"value": "re",
},
]
`);
});

test('escapes characters', () => {
expect(
parseReverseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "Food",
},
Object {
"isHighlighted": true,
"value": " &amp; &lt;Drinks&gt; &#39;n&#39; &quot;Music&quot;",
},
]
`);
});

test('do not escape ignored characters', () => {
expect(
parseReverseHighlightedAttribute({
attribute: 'title',
hit: {
_highlightResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
ignoreEscape: ["'"],
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "Food",
},
Object {
"isHighlighted": true,
"value": " &amp; &lt;Drinks&gt; 'n' &quot;Music&quot;",
},
]
`);
});
});

describe('parseSnippetedAttribute', () => {
Expand Down Expand Up @@ -136,6 +322,99 @@ describe('highlight', () => {
]
`);
});

test('allows custom highlightPreTag and highlightPostTag', () => {
expect(
parseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: '<em>He</em>llo t<em>he</em>re',
},
},
},
highlightPreTag: '<em>',
highlightPostTag: '</em>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "He",
},
Object {
"isHighlighted": false,
"value": "llo t",
},
Object {
"isHighlighted": true,
"value": "he",
},
Object {
"isHighlighted": false,
"value": "re",
},
]
`);
});

test('escapes characters', () => {
expect(
parseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "Food",
},
Object {
"isHighlighted": false,
"value": " &amp; &lt;Drinks&gt; &#39;n&#39; &quot;Music&quot;",
},
]
`);
});

test('do not escape ignored characters', () => {
expect(
parseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
ignoreEscape: ["'"],
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": true,
"value": "Food",
},
Object {
"isHighlighted": false,
"value": " &amp; &lt;Drinks&gt; 'n' &quot;Music&quot;",
},
]
`);
});
});

describe('parseReverseSnippetedAttribute', () => {
Expand Down Expand Up @@ -175,4 +454,97 @@ describe('highlight', () => {
`);
});
});

test('allows custom highlightPreTag and highlightPostTag', () => {
expect(
parseReverseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: '<em>He</em>llo t<em>he</em>re',
},
},
},
highlightPreTag: '<em>',
highlightPostTag: '</em>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "He",
},
Object {
"isHighlighted": true,
"value": "llo t",
},
Object {
"isHighlighted": false,
"value": "he",
},
Object {
"isHighlighted": true,
"value": "re",
},
]
`);
});

test('escapes characters', () => {
expect(
parseReverseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "Food",
},
Object {
"isHighlighted": true,
"value": " &amp; &lt;Drinks&gt; &#39;n&#39; &quot;Music&quot;",
},
]
`);
});

test('do not escape ignored characters', () => {
expect(
parseReverseSnippetedAttribute({
attribute: 'title',
hit: {
_snippetResult: {
title: {
value: `<mark>Food</mark> & <Drinks> 'n' "Music"`,
},
},
},
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
ignoreEscape: ["'"],
})
).toMatchInlineSnapshot(`
Array [
Object {
"isHighlighted": false,
"value": "Food",
},
Object {
"isHighlighted": true,
"value": " &amp; &lt;Drinks&gt; 'n' &quot;Music&quot;",
},
]
`);
});
});
Loading

0 comments on commit 50a9a73

Please sign in to comment.