@@ -5,6 +5,14 @@ import { createElement } from "react";
55import { beforeEach , describe , expect , it , vi } from "vitest" ;
66import { DomainSuggestions } from "@/components/domain/domain-suggestions" ;
77
8+ const TEST_SUGGESTIONS = [
9+ "github.com" ,
10+ "reddit.com" ,
11+ "wikipedia.org" ,
12+ "firefox.com" ,
13+ "jarv.is" ,
14+ ] ;
15+
816vi . mock ( "@bprogress/next/app" , ( ) => ( {
917 useRouter : ( ) => ( { push : vi . fn ( ) } ) ,
1018} ) ) ;
@@ -23,9 +31,9 @@ describe("DomainSuggestions", () => {
2331 localStorage . removeItem ( "search-history" ) ;
2432 } ) ;
2533
26- it ( "renders default suggestions when there is no history" , async ( ) => {
27- render ( < DomainSuggestions /> ) ;
28- // Wait for a known default like jarv.is to appear
34+ it ( "renders provided suggestions when there is no history" , async ( ) => {
35+ render ( < DomainSuggestions suggestions = { TEST_SUGGESTIONS } /> ) ;
36+ // Wait for a known suggestion like jarv.is to appear
2937 expect (
3038 await screen . findByRole ( "button" , { name : / j a r v \. i s / i } ) ,
3139 ) . toBeInTheDocument ( ) ;
@@ -35,20 +43,20 @@ describe("DomainSuggestions", () => {
3543 ) . toBeGreaterThan ( 0 ) ;
3644 } ) ;
3745
38- it ( "merges history and defaults without duplicates, capped by max" , async ( ) => {
46+ it ( "merges history and suggestions without duplicates, capped by max" , async ( ) => {
3947 localStorage . setItem (
4048 "search-history" ,
4149 JSON . stringify ( [ "foo.com" , "github.com" , "bar.org" ] ) ,
4250 ) ;
43- render ( < DomainSuggestions max = { 4 } /> ) ;
51+ render ( < DomainSuggestions suggestions = { TEST_SUGGESTIONS } max = { 4 } /> ) ;
4452 // History entries appear
4553 expect (
4654 await screen . findByRole ( "button" , { name : / f o o \. c o m / i } ) ,
4755 ) . toBeInTheDocument ( ) ;
4856 expect (
4957 screen . getByRole ( "button" , { name : / b a r \. o r g / i } ) ,
5058 ) . toBeInTheDocument ( ) ;
51- // github.com appears only once (deduped with defaults )
59+ // github.com appears only once (deduped with suggestions )
5260 expect ( screen . getAllByRole ( "button" , { name : / g i t h u b \. c o m / i } ) . length ) . toBe (
5361 1 ,
5462 ) ;
@@ -57,7 +65,12 @@ describe("DomainSuggestions", () => {
5765 it ( "invokes onSelect when a suggestion is clicked" , async ( ) => {
5866 const onSelect = vi . fn ( ) ;
5967 localStorage . setItem ( "search-history" , JSON . stringify ( [ "example.com" ] ) ) ;
60- render ( < DomainSuggestions onSelectAction = { onSelect } /> ) ;
68+ render (
69+ < DomainSuggestions
70+ suggestions = { TEST_SUGGESTIONS }
71+ onSelectAction = { onSelect }
72+ /> ,
73+ ) ;
6174 await userEvent . click ( screen . getByRole ( "button" , { name : / e x a m p l e .c o m / i } ) ) ;
6275 expect ( onSelect ) . toHaveBeenCalledWith ( "example.com" ) ;
6376 } ) ;
0 commit comments