1+ import { QuoteListPage as Component } from "./quote-list.page"
2+ import { userEvent } from "@testing-library/user-event"
3+ import { router } from "@/provider/tanstack-router.provider"
4+ import { renderWithFileRoutes } from "@/test/file-route-utils"
5+ import { beforeEach , describe , expect , it , vi } from "vitest"
6+ import { logDOM , screen , waitFor , waitForElementToBeRemoved } from "@testing-library/react"
7+
8+ // hooks
9+ import { useGetAllQuoteDetails } from "@/api-hook/use-get-all-quote-details.hook"
10+
11+ // db actions
12+ import { deleteQuoteWithLinks } from "@/db/quote_tags.db"
13+ import { toggleQuotePinned } from "@/db/quote.db"
14+
15+ // store
16+ import { useSortStore } from "@/store/use-sort.store"
17+ import type { QuoteDetails } from "@/model/index.model"
18+
19+ vi . mock ( "@/api-hook/use-get-all-quote-details.hook" )
20+ const mockUseGetAllQuoteDetails = vi . mocked ( useGetAllQuoteDetails )
21+ type GetAllQuotesReturn = ReturnType < typeof useGetAllQuoteDetails >
22+
23+ vi . mock ( '@/db/quote_tags.db' )
24+ const mockDeleteQuoteWithLinks = vi . mocked ( deleteQuoteWithLinks )
25+
26+ beforeEach ( ( ) => {
27+ ///reset mock value
28+ mockUseGetAllQuoteDetails . mockReturnValue ( {
29+ data : undefined ,
30+ isLoading : false ,
31+ error : undefined ,
32+ refetch : vi . fn ( )
33+ } )
34+ } )
35+
36+ describe ( Component . name , ( ) => {
37+ describe ( "rendering" , ( ) => {
38+ it ( "renders page with header and search" , async ( ) => {
39+ await router . navigate ( { to : "/" } )
40+ await renderWithFileRoutes ( < Component /> )
41+ const loader = await screen . queryByLabelText ( "loading-list" )
42+ if ( loader ) {
43+ expect ( await loader ) . toBeInTheDocument ( )
44+ await waitForElementToBeRemoved ( loader , { timeout : 8000 } )
45+ }
46+
47+ const list = await screen . queryByLabelText ( "list" )
48+ expect ( await list ) . toBeInTheDocument ( )
49+
50+ expect ( await screen ?. findByLabelText ( "sticky-header" ) ) . toBeInTheDocument ( )
51+ expect ( await screen ?. findByLabelText ( "search-note" ) ) . toBeInTheDocument ( )
52+ expect ( await screen ?. findByLabelText ( "search-icon" ) ) . toBeInTheDocument ( )
53+ expect ( await screen ?. findByLabelText ( "settings-button" ) ) . toBeInTheDocument ( )
54+ } )
55+
56+ it ( "renders loading list" , async ( ) => {
57+ mockUseGetAllQuoteDetails . mockReturnValue ( {
58+ data : undefined ,
59+ isLoading : true ,
60+ error : undefined ,
61+ refetch : vi . fn ( ) ,
62+ } )
63+
64+ await router . navigate ( { to : "/" } )
65+ await renderWithFileRoutes ( < Component /> )
66+
67+ const loaderList = await screen . queryByLabelText ( "loading-list" )
68+ if ( loaderList ) {
69+ expect ( await loaderList ) . toBeInTheDocument ( )
70+ await waitForElementToBeRemoved ( loaderList , { timeout : 8000 } )
71+ }
72+
73+ const loader = await screen . queryByLabelText ( "loading" )
74+ expect ( await loader ) . toBeInTheDocument ( )
75+ logDOM ( loader ! )
76+ } )
77+
78+ it ( "renders quotes when data exists" , async ( ) => {
79+ const testContent = "test quote"
80+ mockUseGetAllQuoteDetails . mockReturnValue ( {
81+ data : [
82+ {
83+ id : 1 ,
84+ text : testContent ,
85+ pinned : false ,
86+ tags : [ { id : 1 , name : "life" } ] ,
87+ } ,
88+ ] ,
89+ isLoading : false ,
90+ error : undefined ,
91+ refetch : vi . fn ( ) ,
92+ } )
93+
94+ await router . navigate ( { to : "/" } )
95+ await renderWithFileRoutes ( < Component /> )
96+
97+ const loaderList = await screen . queryByLabelText ( "loading-list" )
98+ if ( loaderList ) {
99+ expect ( await loaderList ) . toBeInTheDocument ( )
100+ await waitForElementToBeRemoved ( loaderList , { timeout : 8000 } )
101+ }
102+
103+ expect ( await screen . findByText ( testContent ) ) . toBeInTheDocument ( )
104+ } )
105+
106+ it ( "shows error state" , async ( ) => {
107+ const errorMessage = "something went wrong"
108+
109+ mockUseGetAllQuoteDetails . mockReturnValue ( {
110+ data : undefined ,
111+ isLoading : false ,
112+ error : errorMessage ,
113+ refetch : vi . fn ( ) ,
114+ } )
115+
116+ await router . navigate ( { to : "/" } )
117+ await renderWithFileRoutes ( < Component /> )
118+ const loaderList = await screen . queryByLabelText ( "loading-list" )
119+ if ( loaderList ) {
120+ expect ( await loaderList ) . toBeInTheDocument ( )
121+ await waitForElementToBeRemoved ( loaderList , { timeout : 8000 } )
122+ }
123+ expect ( await screen . findByText ( errorMessage , { exact : false } ) ) . toBeInTheDocument ( )
124+ } )
125+
126+ it ( "renders add new button with navigation link" , async ( ) => {
127+ await router . navigate ( { to : "/" } )
128+ await renderWithFileRoutes ( < Component /> )
129+
130+ expect ( await screen . findByLabelText ( "new" ) ) . toBeInTheDocument ( )
131+ } )
132+ } )
133+
134+ describe ( "search filtering" , ( ) => {
135+ it ( "filters quotes by search text" , async ( ) => {
136+ mockUseGetAllQuoteDetails . mockReturnValue ( {
137+ data : [
138+ { id : 1 , text : "hello world" , tags : [ ] } ,
139+ { id : 2 , text : "goodbye world" , tags : [ ] } ,
140+ ] ,
141+ isLoading : false ,
142+ error : undefined ,
143+ refetch : vi . fn ( ) ,
144+ } )
145+
146+ await router . navigate ( { to : "/" } )
147+ await renderWithFileRoutes ( < Component /> )
148+
149+ const searchInput = await screen . findByLabelText ( "search-note" )
150+ await userEvent . type ( searchInput , "hello" )
151+
152+ await waitFor ( ( ) => {
153+ expect ( screen . getByText ( "hello world" ) ) . toBeInTheDocument ( )
154+ expect ( screen . queryByText ( "goodbye world" ) ) . not . toBeInTheDocument ( )
155+ } )
156+ } )
157+ } )
158+
159+ describe ( "tag filtering" , ( ) => {
160+ it ( "filters quotes by tag" , async ( ) => {
161+ const mockedData = {
162+ data : [
163+ {
164+ id : 1 ,
165+ text : "quote one" ,
166+ tags : [ { id : 1 , name : "life" } ] ,
167+ } ,
168+ {
169+ id : 2 ,
170+ text : "quote two" ,
171+ tags : [ { id : 2 , name : "love" } ] ,
172+ } ,
173+ ] ,
174+ isLoading : false ,
175+ error : undefined ,
176+ refetch : vi . fn ( ) ,
177+ }
178+ mockUseGetAllQuoteDetails . mockReturnValue ( mockedData )
179+
180+ await router . navigate ( { to : "/" } )
181+ await renderWithFileRoutes ( < Component /> )
182+ const loaderList = await screen . queryByLabelText ( "loading-list" )
183+ if ( loaderList ) {
184+ expect ( await loaderList ) . toBeInTheDocument ( )
185+ await waitForElementToBeRemoved ( loaderList , { timeout : 8000 } )
186+ }
187+
188+ const firstText = screen . queryByText ( mockedData . data [ 0 ] . text )
189+ const secondText = screen . queryByText ( mockedData . data [ 1 ] . text )
190+
191+ await expect ( firstText ) . toBeInTheDocument ( )
192+ await expect ( secondText ) . toBeInTheDocument ( )
193+
194+ const openFilter = await screen . queryByLabelText ( "open-filter" )
195+ expect ( await openFilter ) . toBeInTheDocument ( )
196+ await userEvent . click ( openFilter ! , { } )
197+
198+ const tagBox = await screen . queryByLabelText ( "filter-by" )
199+ expect ( await tagBox ) . toBeInTheDocument ( )
200+
201+ const tagButton = await screen . queryByLabelText (
202+ "filter-by-" + mockedData . data [ 0 ] . tags [ 0 ] . name
203+ )
204+ await userEvent . click ( tagButton ! )
205+ const list = await screen . queryByLabelText ( "list" )
206+
207+ expect ( firstText ) . toBeInTheDocument ( )
208+ expect ( secondText ) . not . toBeInTheDocument ( )
209+ } )
210+ } )
211+
212+ describe ( "interaction" , ( ) => {
213+ it ( "opens delete dialog and deletes quote" , async ( ) => {
214+ const firstItem = { id : 1 , text : "delete me" , tags : [ { id : 1 , name : "life" } ] }
215+ const mockedData : GetAllQuotesReturn = {
216+ data : [ firstItem ] ,
217+ isLoading : false ,
218+ error : undefined ,
219+ // refetch: vi.fn()
220+ refetch : vi . fn ( async ( ) => {
221+ mockedData . data = [ ]
222+ return { } as Awaited < ReturnType < GetAllQuotesReturn [ "refetch" ] > >
223+ } )
224+ }
225+ mockUseGetAllQuoteDetails . mockReturnValue ( mockedData )
226+
227+ await router . navigate ( { to : "/" } )
228+ await renderWithFileRoutes ( < Component /> )
229+ const loaderList = await screen . queryByLabelText ( "loading-list" )
230+ if ( loaderList ) {
231+ expect ( await loaderList ) . toBeInTheDocument ( )
232+ await waitForElementToBeRemoved ( loaderList , { timeout : 8000 } )
233+ }
234+
235+ const list = await screen . queryByLabelText ( "list" )
236+ const firstText = screen . queryByText ( mockedData ?. data ?. [ 0 ] ?. text ! )
237+ await expect ( firstText ) . toBeInTheDocument ( )
238+
239+ const deleteBtn = await screen . findByLabelText ( "delete-" + firstItem . id )
240+ await userEvent . click ( deleteBtn )
241+ // logDOM(deleteBtn!)
242+
243+ const deletePopup = await screen . findByLabelText ( "delete" )
244+ await expect ( deletePopup ) . toBeInTheDocument ( )
245+
246+ await userEvent . click ( deletePopup , { } )
247+
248+ await expect ( mockDeleteQuoteWithLinks ) . toHaveBeenCalledWith ( firstItem . id )
249+ await expect ( mockedData . refetch ) . toHaveBeenCalled ( )
250+
251+ await expect ( deletePopup ) . not . toBeInTheDocument ( )
252+ await expect ( firstText ) . not . toBeInTheDocument ( )
253+ } )
254+
255+
256+ } )
257+ } )
0 commit comments