Skip to content

Commit 1a22df3

Browse files
authored
Merge pull request #191 from antidis/antidis/search-bug-fix
Fix bug where the token separators used in the search component and the search indexer were inconsistent
2 parents df76015 + 0f65765 commit 1a22df3

File tree

11 files changed

+44
-11
lines changed

11 files changed

+44
-11
lines changed

addon/components/docs-header/search-box/template.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
type="text"
1313
placeholder='Search'
1414
class='w-24 text-xs p-2 pl-6 rounded focus:bg-grey-lighter outline-none'
15-
data-search-box-input>
15+
data-search-box-input
16+
data-test-search-box-input>
1617
</div>

addon/components/docs-header/search-result/component.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ export default Component.extend({
8181

8282
_highlight(text, start, length) {
8383
return `${text.slice(0, start)}<em class='docs-viewer-search__result-item__text--emphasis'>${text.slice(start, start + length)}</em>${text.slice(start + length)}`;
84-
}
84+
},
85+
86+
'data-test-search-result': true,
8587
});

addon/components/docs-header/search-results/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
onClose=(action 'clearSearch')
77
targetAttachment='bottom left'
88
constraints=(array (hash to='window' attachment='together' pin=true))}}
9-
<ul class="docs-viewer-search__result-list">
9+
<ul class="docs-viewer-search__result-list" data-test-search-result-list>
1010
{{#each (take 5 searchResults) as |result index|}}
1111
<li>
1212
{{docs-header/search-result

addon/services/docs-search.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Service, { inject as service } from '@ember/service';
22
import { getOwner } from '@ember/application';
33
import { computed } from '@ember/object';
44
import lunr from 'lunr';
5+
import config from 'dummy/config/environment';
56

67
const { Index, Query } = lunr;
78

@@ -11,7 +12,7 @@ export default Service.extend({
1112
search(phrase) {
1213
return this.loadSearchIndex()
1314
.then(({ index, documents }) => {
14-
let words = phrase.toLowerCase().split(/\s+/);
15+
let words = phrase.toLowerCase().split(new RegExp(config['ember-cli-addon-docs'].searchTokenSeparator));
1516
let results = index.query((query) => {
1617
// In the future we could boost results based on the field they come from
1718
for (let word of words) {

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = {
4747
projectName: this.parent.pkg.name,
4848
projectTag: this.parent.pkg.version,
4949
projectHref: info && info.browse(),
50-
deployVersion: 'ADDON_DOCS_DEPLOY_VERSION'
50+
deployVersion: 'ADDON_DOCS_DEPLOY_VERSION',
51+
searchTokenSeparator: "\\s+"
5152
}
5253
};
5354

lib/broccoli/search-indexer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = class SearchIndexCompiler extends Writer {
2727
this.field('text');
2828
this.field('keywords');
2929

30+
this.tokenizer.separator = new RegExp(writer.config['ember-cli-addon-docs'].searchTokenSeparator);
31+
3032
for (let doc of writer.buildDocuments()) {
3133
if (doc) {
3234
documents[doc.id] = doc;

tests/acceptance/sandbox/api/components-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ module('Acceptance | API | components', function(hooks) {
4545
assert.equal(indexItems.length, 13, 'correct number of items rendered');
4646
assert.ok(indexItems.includes('_privateField'), 'private field rendered');
4747
});
48+
49+
test('search box works', async function(assert) {
50+
await visit('/sandbox');
51+
52+
assert.equal(modulePage.searchResults.items.length, 0, 'no search results shown');
53+
await modulePage.fillInSearchQuery('sub-subsection');
54+
assert.equal(modulePage.searchResults.items.length, 1, 'one search result shown');
55+
});
4856
});

tests/pages/api/class.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import PageObject, { collection, text } from 'ember-classy-page-object';
1+
import BaseAddonPage from '../base';
2+
import { collection, text } from 'ember-classy-page-object';
23

3-
const ClassPage = PageObject.extend({
4+
const ClassPage = BaseAddonPage.extend({
45
navItems: collection({ scope: '[data-test-id="nav-item"]' }),
56

67
title: text('[data-test-class-name]'),

tests/pages/api/module.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import PageObject, { collection, text } from 'ember-classy-page-object';
1+
import BaseAddonPage from '../base';
2+
import { collection, text } from 'ember-classy-page-object';
23

3-
const ModulePage = PageObject.extend({
4+
const ModulePage = BaseAddonPage.extend({
45
navItems: collection({ scope: '[data-test-id="nav-item"]' }),
56

67
toggles: collection({

tests/pages/base.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import PageObject, { collection, fillable } from 'ember-classy-page-object';
2+
3+
const DefaultPage = PageObject.extend({
4+
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
5+
fillInSearchQuery: fillable('[data-test-search-box-input]'),
6+
// eslint-disable-next-line ember/avoid-leaking-state-in-ember-objects
7+
searchResults: {
8+
scope: '[data-test-search-result-list]',
9+
items: collection({
10+
scope: '[data-test-search-result]'
11+
})
12+
}
13+
});
14+
15+
export default DefaultPage;

0 commit comments

Comments
 (0)