1
2 import { beforeEach, describe, expect, test, vi } from "vitest"
3 import { cleanup, render } from "@testing-library/react"
4
5 // mock components
6 vi.mock("@/componets/Card", () => ({
7 Card: (props: { title: string }) => <div data-testid="card">{props.title}</div>,
8 }))
9
10 import { useMedusaSuggestions } from "../index"
11
12 const TestComponent = ({ keywords }: { keywords: string | string[] }) => {
13 const suggestion = useMedusaSuggestions({ keywords })
14 return suggestion ? (
15 <div data-testid="suggestion">{suggestion.title}</div>
16 ) : null
17 }
18
19 beforeEach(() => {
20 vi.clearAllMocks()
21 cleanup()
22 })
23
24 describe("keywords", () => {
25 test("should handle empty keyword string", () => {
26 const { container } = render(<TestComponent keywords="" />)
27
28 expect(container.firstChild).toBeNull()
29 })
30
31 test("should handle empty keyword array", () => {
32 const { container } = render(<TestComponent keywords={[]} />)
33
34 expect(container.firstChild).toBeNull()
35 })
36
37 test("should return null for no matches", () => {
38 const { container } = render(<TestComponent keywords="nonexistentkeyword" />)
39
40 expect(container.firstChild).toBeNull()
41 })
42
43 test("should return suggestion for matching keyword string", () => {
44 const { getByTestId } = render(<TestComponent keywords="railway" />)
45
46 const suggestion = getByTestId("suggestion")
47
48 expect(suggestion).toBeInTheDocument()
49 expect(suggestion).toHaveTextContent("Deploy to Cloud")
50 })
51
52 test("should return suggestion for matching keyword in array", () => {
53 const { getByTestId } = render(
54 <TestComponent keywords={["nonexistent", "railway", "anothernonexistent"]} />
55 )
56
57 const suggestion = getByTestId("suggestion")
58
59 expect(suggestion).toBeInTheDocument()
60 expect(suggestion).toHaveTextContent("Deploy to Cloud")
61 })
62
63 test("should be case insensitive and ignore punctuation", () => {
64 const { getByTestId } = render(<TestComponent keywords="Railway!" />)
65
66 const suggestion = getByTestId("suggestion")
67
68 expect(suggestion).toBeInTheDocument()
69 expect(suggestion).toHaveTextContent("Deploy to Cloud")
70 })
71 })