Skip to content

Commit

Permalink
prevent adding highlights containing no text
Browse files Browse the repository at this point in the history
  • Loading branch information
samisayegh committed Nov 13, 2020
1 parent 38d8e42 commit 476754d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
4 changes: 4 additions & 0 deletions src/ui/Quickview/QuickviewDocumentWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class QuickviewDocumentWords {
const quickviewWord = new QuickviewDocumentWord(this.result);
quickviewWord.doCompleteInitialScanForKeywordInDocument(element);

if (!quickviewWord.text) {
return;
}

const alreadyScannedKeyword = this.words[quickviewWord.indexIdentifier];

if (!alreadyScannedKeyword) {
Expand Down
67 changes: 27 additions & 40 deletions unitTests/ui/QuickviewDocumentWordsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,45 @@ export function QuickviewDocumentWordsTest() {
});

it('should detect multiple keywords, and ignore random elements', () => {
fakeBody.append(
$$('div', {
id: 'CoveoHighlight:1.1.1'
}).el
);

fakeBody.append(
$$('div', {
id: 'randomStuff'
}).el
);

fakeBody.append(
$$('div', {
id: 'CoveoHighlight:2.1.1'
}).el
);

fakeBody.append(
$$('div', {
id: 'randomStuff2'
}).el
);
fakeBody.append($$('div', { id: 'CoveoHighlight:1.1.1' }, 'A').el);

fakeBody.append($$('div', { id: 'randomStuff' }).el);

fakeBody.append($$('div', { id: 'CoveoHighlight:2.1.1' }, 'B').el);

fakeBody.append($$('div', { id: 'randomStuff2' }).el);

const words = new QuickviewDocumentWords(fakeIframe, fakeResult);

expect(words.words['1'].indexIdentifier).toBe('1');
expect(words.words['1'].text).toBe('A');

expect(words.words['2'].indexIdentifier).toBe('2');
expect(words.words['2'].text).toBe('B');

expect(Object.keys(words.words).length).toBe(2);
});

it('should detect multiple keywords in the same group', () => {
fakeBody.append(
$$('div', {
id: 'CoveoHighlight:1.1.1'
}).el
);

fakeBody.append(
$$('div', {
id: 'CoveoHighlight:1.2.1'
}).el
);

fakeBody.append(
$$('div', {
id: 'CoveoHighlight:1.3.1'
}).el
);
fakeBody.append($$('div', { id: 'CoveoHighlight:1.1.1' }, 'A').el);

fakeBody.append($$('div', { id: 'CoveoHighlight:1.2.1' }, 'A').el);

fakeBody.append($$('div', { id: 'CoveoHighlight:1.3.1' }, 'A').el);

const words = new QuickviewDocumentWords(fakeIframe, fakeResult);
expect(words.words['1'].indexIdentifier).toBe('1');
expect(words.words['1'].elements.length).toBe(3);
});

it('should ignore highlights whose text resolves to an empty string', () => {
const span = $$('span', { id: 'CoveoHighlight:1.1.1' }, $$('coveotaggedword', { id: 'CoveoHighlight:2.1.1' }, 'A'));

fakeBody.append(span.el);
const words = new QuickviewDocumentWords(fakeIframe, fakeResult);

expect(words.words['1'].text).toBe('A');
expect(words.words['2']).toBe(undefined);
});
});
}

0 comments on commit 476754d

Please sign in to comment.