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

test(utils): Add tests for getSnippettedValue #12

Merged
merged 1 commit into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 1 addition & 15 deletions src/lib/DocSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DocSearch {
utils.getHighlightedValue(hit, 'lvl5'),
utils.getHighlightedValue(hit, 'lvl6')
]).join(' › ');
let text = this.getSnippettedValue(hit, 'content');
let text = utils.getSnippettedValue(hit, 'content');

return {
isCategoryHeader: hit.isCategoryHeader,
Expand All @@ -121,20 +121,6 @@ class DocSearch {
});
}

getSnippettedValue(object, key) {
if (!object._snippetResult || !object._snippetResult[key].value) {
return object[key];
}
let snippet = object._snippetResult[key].value;
if (snippet[0] !== snippet[0].toUpperCase()) {
snippet = `…${snippet}`;
}
if (['.', '!', '?'].indexOf(snippet[snippet.length - 1]) === -1) {
snippet = `${snippet}…`;
}
return snippet;
}

getSuggestionTemplate() {
const template = Hogan.compile(templates.suggestion);
return (suggestion) => {
Expand Down
38 changes: 37 additions & 1 deletion src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ let utils = {
* }
* },
* text: 'foo'
* }, 'foo');
* }, 'text');
* =>
* '<mark>foo</mark>'
* @param {object} object Hit object returned by the Algolia API
Expand All @@ -194,6 +194,42 @@ let utils = {
return object[property];
}
return object._highlightResult[property].value;
},
/*
* Returns the snippeted value of the specified key in the specified object.
* If no highlighted value is available, will return the key value directly.
* Will add starting and ending ellipsis (…) if we detect that a sentence is
* incomplete
* eg.
* getSnippetedValue({
* _snippetResult: {
* text: {
* value: '<mark>This is an unfinished sentence</mark>'
* }
* },
* text: 'This is an unfinished sentence'
* }, 'text');
* =>
* '<mark>This is an unefinished sentenced</mark>…'
* @param {object} object Hit object returned by the Algolia API
* @param {string} property Object key to look for
* @return {string}
**/
getSnippetedValue(object, property) {
if (!object._snippetResult
|| !object._snippetResult[property]
|| !object._snippetResult[property].value) {
return object[property];
}
let snippet = object._snippetResult[property].value;

if (snippet[0] !== snippet[0].toUpperCase()) {
snippet = `…${snippet}`;
}
if (['.', '!', '?'].indexOf(snippet[snippet.length - 1]) === -1) {
snippet = `${snippet}…`;
}
return snippet;
}


Expand Down
67 changes: 67 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,71 @@ describe('utils', () => {
expect(actual).toEqual('foo');
});
});

describe('getSnippetedValue', () => {
it('should return the key value if no snippet returned', () => {
// Given
let input = {
text: 'Foo'
};

// When
let actual = utils.getSnippetedValue(input, 'text');

// Then
expect(actual).toEqual('Foo');
});
it('should return the key value if no snippet for this key', () => {
// Given
let input = {
_snippetResult: {
content: {
value: '<mark>Bar</mark>'
}
},
text: 'Foo',
content: 'Bar'
};

// When
let actual = utils.getSnippetedValue(input, 'text');

// Then
expect(actual).toEqual('Foo');
});
it('should add ellipsis at the start if snippet does not start with a capital letter', () => {
// Given
let input = {
_snippetResult: {
text: {
value: 'this is the <mark>end</mark> of a sentence.'
}
},
text: 'this is the end of a sentence.'
};

// When
let actual = utils.getSnippetedValue(input, 'text');

// Then
expect(actual).toEqual('…this is the <mark>end</mark> of a sentence.');
});
it('should add ellipsis at the end if snippet does not end with a terminal point', () => {
// Given
let input = {
_snippetResult: {
text: {
value: 'This is an <mark>finished</mark> sentence'
}
},
text: 'This is an <mark>finished</mark> sentence'
};

// When
let actual = utils.getSnippetedValue(input, 'text');

// Then
expect(actual).toEqual('This is an <mark>finished</mark> sentence…');
});
});
});