Skip to content

Commit 823831a

Browse files
committed
✅ test: add test for updateHits hook
1 parent a1e7fa7 commit 823831a

File tree

4 files changed

+85
-24
lines changed

4 files changed

+85
-24
lines changed

demo/assets/js/testable.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,74 @@ const test19 = typeahead({
987987
`,
988988
},
989989
});
990+
991+
const test20 = typeahead({
992+
input: document.getElementById('input-twenty'),
993+
source: {
994+
local: colors,
995+
keys: ['name'],
996+
groupKey: 'group',
997+
},
998+
classNames: {
999+
wrapper: 'typeahead-standalone typeahead-test-twenty',
1000+
},
1001+
highlight: true,
1002+
// autoSelect: true,
1003+
onSubmit: (e) => {
1004+
e.preventDefault();
1005+
document.querySelector('#input-twenty-submit-val').innerHTML = e.target.value;
1006+
},
1007+
hooks: {
1008+
updateHits: async (resultSet) => {
1009+
// to test a fetch request
1010+
// const response = await fetch(
1011+
// 'https://raw.githubusercontent.com/digitalfortress-tech/typeahead-standalone/master/docs/assets/json/superheroes.json',
1012+
// {
1013+
// method: 'GET',
1014+
// }
1015+
// );
1016+
// const text = await response.text();
1017+
// const data = text && JSON.parse(text);
1018+
1019+
// const hits = data.results.slice(0, 10);
1020+
1021+
// reverse characters of words
1022+
const hits = resultSet.hits.map((i) => {
1023+
i.name = i.name.split('').reverse().join('');
1024+
return i;
1025+
});
1026+
1027+
return {
1028+
hits,
1029+
count: resultSet.count,
1030+
};
1031+
},
1032+
},
1033+
templates: {
1034+
suggestion: (item, resultSet) => {
1035+
return `<div class="text" style="background-color:${item.hash}">${item.name}</div>`;
1036+
},
1037+
group: (name, resultSet) => {
1038+
const count = resultSet.hits.filter((i) => i.group === name).length;
1039+
return `<div class="custom-group">${name} (count: ${count})</div>`;
1040+
},
1041+
header: (resultSet) => {
1042+
if (!resultSet.query) return 'Top Colors';
1043+
return `Colors Found (Total: ${resultSet.count})`;
1044+
},
1045+
footer: (resultSet) => {
1046+
if (!resultSet.query) return '';
1047+
return `<a href="#">See${
1048+
resultSet.count > resultSet.limit ? ` ${resultSet.count - resultSet.limit}` : ''
1049+
} more...</a>`;
1050+
},
1051+
notFound: (resultSet) => `Oops...Nothing Found for query - ${resultSet.query} 😪 <br>Try another color...`,
1052+
empty: () => {
1053+
return [
1054+
{ name: 'Red', value: 'RD', hash: 'red' },
1055+
{ name: 'Green', value: 'GR', hash: 'green' },
1056+
{ name: 'Blue', value: 'BL', hash: 'blue', group: 'Shades of Blue' },
1057+
];
1058+
},
1059+
},
1060+
});

demo/dev.umd.html

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -182,29 +182,6 @@ <h4>Dummy input to test keyboard nav</h4>
182182
minLength: 1,
183183
limit: 10,
184184
retainFocus: true,
185-
hooks: {
186-
updateHits: async (resultSet) => {
187-
188-
const response = await fetch(
189-
"https://raw.githubusercontent.com/digitalfortress-tech/typeahead-standalone/master/docs/assets/json/superheroes.json",
190-
{
191-
method: 'GET',
192-
}
193-
);
194-
195-
const text = await response.text();
196-
const data = text && JSON.parse(text);
197-
198-
const hits = data.results.slice(0,10)
199-
200-
// const hits = resultSet.hits.filter(hit => hit.popularity && hit.popularity > 50);
201-
202-
return {
203-
hits,
204-
count: hits.length,
205-
};
206-
}
207-
},
208185
templates: {
209186
suggestion: (item) => {
210187
const date = item.release_date

demo/testable.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,19 @@ <h3 class="subtitle">19. Source Keys as deeply nested values</h3>
330330
</form>
331331
</section>
332332

333+
<section class="section-twenty top-section">
334+
<form method="GET">
335+
<h3 class="subtitle">20. Hooks</h3>
336+
<p class="details">updateHits hook</p>
337+
<div>
338+
<label for="input-twenty">Search ...</label>
339+
<input id="input-twenty" name="test-18" type="search" autocomplete="off" placeholder="Search..."
340+
required="true">
341+
<button type="submit">OK</button>
342+
</div>
343+
</form>
344+
</section>
345+
333346
<br />
334347
</main>
335348
<link rel="stylesheet" href="./../dist/basic.css" />

src/common.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export interface typeaheadConfig<T extends Dictionary> {
281281
/**
282282
* The updateHits hook allows you to modify/filter/sort the search results before being rendered
283283
* @param hits The found results
284-
* @returns
284+
* @returns A promise containing nothing/void or with the ResultSet
285285
*/
286286
updateHits: (
287287
resultSet: Pick<ResultSet<T>, 'hits' | 'query' | 'count'>

0 commit comments

Comments
 (0)