Skip to content

Commit

Permalink
error with getUrlWithTextHighlight (#948)
Browse files Browse the repository at this point in the history
direct answer may return a snippet with no matchSubstring, add a check for that before constructing url with text highlighting

TEST=manual&auto
- test a query that return a Direct answer without any matched substrings, see that no error occur and url is still working
- add jest test
  • Loading branch information
yen-tt authored Sep 13, 2021
1 parent 8c86fc8 commit 13a5928
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion static/js/formatters-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ export function getYoutubeUrl(videos = []) {
* @returns a URL with text fragment URI component attached
*/
export function getUrlWithTextHighlight(snippet, baseUrl) {
if (!isChrome()) {
if (!isChrome() || !snippet || snippet.matchedSubstrings.length === 0) {
return baseUrl;
}
//Find the surrounding sentence of the snippet
Expand Down
9 changes: 9 additions & 0 deletions tests/static/js/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ describe('Formatters', () => {
const isChrome = jest.spyOn(useragent, 'isChrome');
isChrome.mockReturnValue(true);

it('Behaves correctly when there is no matchedSubstring', () => {
const snippet = {
value: 'this is a sentence, for testing purposes.',
matchedSubstrings: []
};
const actual = Formatters.getUrlWithTextHighlight(snippet, link);
expect(actual).toEqual(link);
});

it('Behaves correctly when there is a matchedSubstring with a surrounding sentence', () => {
const snippet = {
value: 'this is a sentence, for testing purposes.',
Expand Down

0 comments on commit 13a5928

Please sign in to comment.