|
| 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 | +}); |
0 commit comments