Skip to content

Commit 2397249

Browse files
authored
NTP: Display URL in if Omnibar suggestion title is empty (#1825)
* Display URL if suggestion title is empty * Add unit tests for getSuggestionTitle * Fix importing JSX into tests
1 parent a523a46 commit 2397249

File tree

4 files changed

+109
-26
lines changed

4 files changed

+109
-26
lines changed

special-pages/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "node index.mjs",
1111
"build.dev": "npm run build -- --env development",
1212
"lint-fix": "cd ../ && npm run lint-fix",
13-
"test-unit": "node --test \"unit-test/*\" \"pages/history/unit-tests/*\" \"pages/duckplayer/unit-tests/*\" \"pages/new-tab/app/freemium-pir-banner/unit-tests/*\"",
13+
"test-unit": "node --test \"unit-test/*\" \"pages/history/unit-tests/*\" \"pages/duckplayer/unit-tests/*\" \"pages/new-tab/app/freemium-pir-banner/unit-tests/*\" \"pages/new-tab/app/omnibar/unit-tests/*\"",
1414
"test-int": "playwright test --grep-invert '@screenshots'",
1515
"test-int-x": "npm run test-int",
1616
"test-int-snapshots": "playwright test --grep '@screenshots'",

special-pages/pages/new-tab/app/omnibar/components/useSuggestions.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useContext, useEffect, useReducer } from 'preact/hooks';
22
import { eventToTarget } from '../../../../../shared/handlers.js';
33
import { usePlatformName } from '../../settings.provider.js';
4+
import { getSuggestionTitle } from '../utils.js';
45
import { OmnibarContext } from './OmnibarProvider.js';
56

67
/**
@@ -264,31 +265,6 @@ export function useSuggestions({ term, onChangeTerm, onOpenSuggestion, onSubmitS
264265
};
265266
}
266267

267-
/**
268-
* @param {Suggestion} suggestion
269-
* @returns {string}
270-
*/
271-
function getSuggestionTitle(suggestion) {
272-
switch (suggestion.kind) {
273-
case 'bookmark':
274-
return suggestion.title;
275-
case 'historyEntry':
276-
return suggestion.title;
277-
case 'phrase':
278-
return suggestion.phrase;
279-
case 'openTab':
280-
return suggestion.title;
281-
case 'website': {
282-
const url = new URL(suggestion.url);
283-
return url.host + url.pathname + url.search + url.hash;
284-
}
285-
case 'internalPage':
286-
return suggestion.title;
287-
default:
288-
throw new Error('Unknown suggestion kind');
289-
}
290-
}
291-
292268
/**
293269
* @param {string} text
294270
* @param {string} searchTerm
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { test } from 'node:test';
2+
import { equal, throws } from 'node:assert/strict';
3+
import { getSuggestionTitle } from '../utils.js';
4+
5+
/**
6+
* @typedef {import('../../../types/new-tab.js').Suggestion} Suggestion
7+
*/
8+
9+
test.describe('getSuggestionTitle', () => {
10+
test('returns title for bookmark', () => {
11+
/** @type {Suggestion} */
12+
const suggestion = { kind: 'bookmark', title: 'pizzahut.com', url: 'pizzahut.com', isFavorite: true, score: 97 };
13+
equal(getSuggestionTitle(suggestion), 'pizzahut.com');
14+
});
15+
16+
test('returns display URL for bookmark without title', () => {
17+
/** @type {Suggestion} */
18+
const suggestion = { kind: 'bookmark', title: '', url: 'https://dominos.com/order?pizza#special', isFavorite: false, score: 96 };
19+
equal(getSuggestionTitle(suggestion), 'dominos.com/order?pizza#special');
20+
});
21+
22+
test('returns title for historyEntry', () => {
23+
/** @type {Suggestion} */
24+
const suggestion = {
25+
kind: 'historyEntry',
26+
title: 'Best Pizza Places in New York',
27+
url: 'https://example.com/search?q=Best%20Pizza%20Places%20in%20New%20York',
28+
score: 87,
29+
};
30+
equal(getSuggestionTitle(suggestion), 'Best Pizza Places in New York');
31+
});
32+
33+
test('returns display URL for historyEntry without title', () => {
34+
/** @type {Suggestion} */
35+
const suggestion = { kind: 'historyEntry', title: '', url: 'https://example.com/search?q=Pizza%20Dough%20Calculator', score: 85 };
36+
equal(getSuggestionTitle(suggestion), 'example.com/search?q=Pizza%20Dough%20Calculator');
37+
});
38+
39+
test('returns title for internalPage', () => {
40+
/** @type {Suggestion} */
41+
const suggestion = { kind: 'internalPage', title: 'Settings', url: 'duck://settings', score: 1 };
42+
equal(getSuggestionTitle(suggestion), 'Settings');
43+
});
44+
45+
test('returns display URL for internalPage without title', () => {
46+
/** @type {Suggestion} */
47+
const suggestion = { kind: 'internalPage', title: '', url: 'duck://settings', score: 2 };
48+
equal(getSuggestionTitle(suggestion), 'settings');
49+
});
50+
51+
test('returns phrase for phrase kind', () => {
52+
/** @type {Suggestion} */
53+
const suggestion = { kind: 'phrase', phrase: 'pizza delivery' };
54+
equal(getSuggestionTitle(suggestion), 'pizza delivery');
55+
});
56+
57+
test('returns title for openTab', () => {
58+
/** @type {Suggestion} */
59+
const suggestion = { kind: 'openTab', title: 'Pizza Dough Calculator', tabId: 'tab-1', score: 1 };
60+
equal(getSuggestionTitle(suggestion), 'Pizza Dough Calculator');
61+
});
62+
63+
test('returns display URL for website', () => {
64+
/** @type {Suggestion} */
65+
const suggestion = { kind: 'website', url: 'https://pizzaexpress.com/menu#vegetarian' };
66+
equal(getSuggestionTitle(suggestion), 'pizzaexpress.com/menu#vegetarian');
67+
});
68+
69+
test('throws error for unknown kind', () => {
70+
/** @type {any} */
71+
const suggestion = { kind: 'unknown' };
72+
throws(() => getSuggestionTitle(suggestion), { message: 'Unknown suggestion kind' });
73+
});
74+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @typedef {import('../../types/new-tab.js').Suggestion} Suggestion
3+
*/
4+
5+
/**
6+
* @param {Suggestion} suggestion
7+
* @returns {string}
8+
*/
9+
export function getSuggestionTitle(suggestion) {
10+
switch (suggestion.kind) {
11+
case 'bookmark':
12+
case 'historyEntry':
13+
case 'internalPage':
14+
return suggestion.title || getDisplayURL(suggestion.url);
15+
case 'phrase':
16+
return suggestion.phrase;
17+
case 'openTab':
18+
return suggestion.title;
19+
case 'website':
20+
return getDisplayURL(suggestion.url);
21+
default:
22+
throw new Error('Unknown suggestion kind');
23+
}
24+
}
25+
26+
/**
27+
* @param {string} url
28+
* @returns {string}
29+
*/
30+
function getDisplayURL(url) {
31+
const { host, pathname, search, hash } = new URL(url);
32+
return host + pathname + search + hash;
33+
}

0 commit comments

Comments
 (0)