@@ -3,6 +3,7 @@ import { fireEvent, screen, waitFor, act } from '@testing-library/react'
33import { ErrorBoundary } from 'react-error-boundary'
44import '@testing-library/jest-dom'
55import { useQuery , QueryClient } from '@tanstack/react-query'
6+ import { sortFns } from '../devtools'
67import {
78 getByTextContent ,
89 renderWithClient ,
@@ -552,6 +553,104 @@ describe('ReactQueryDevtools', () => {
552553 expect ( queries [ 2 ] ?. textContent ) . toEqual ( query3Hash )
553554 } )
554555
556+ it ( 'should initialize filtering and sorting values with defaults when they are not stored in localstorage' , ( ) => {
557+ localStorage . removeItem ( 'reactQueryDevtoolsSortDesc' )
558+ localStorage . removeItem ( 'reactQueryDevtoolsSortFn' )
559+ localStorage . removeItem ( 'reactQueryDevtoolsFilter' )
560+
561+ const { queryClient } = createQueryClient ( )
562+
563+ function Page ( ) {
564+ const { data = 'default' } = useQuery ( [ 'check' ] , async ( ) => {
565+ await sleep ( 10 )
566+ return 'test'
567+ } )
568+
569+ return (
570+ < div >
571+ < h1 > { data } </ h1 >
572+ </ div >
573+ )
574+ }
575+
576+ renderWithClient ( queryClient , < Page /> , {
577+ initialIsOpen : true ,
578+ } )
579+
580+ const filterInput : HTMLInputElement =
581+ screen . getByLabelText ( / F i l t e r b y q u e r y h a s h / i)
582+ expect ( filterInput . value ) . toEqual ( '' )
583+
584+ const sortCombobox : HTMLSelectElement =
585+ screen . getByLabelText ( / S o r t q u e r i e s / i)
586+ expect ( sortCombobox . value ) . toEqual ( Object . keys ( sortFns ) [ 0 ] )
587+
588+ expect ( screen . getByRole ( 'button' , { name : / A s c / i } ) ) . toBeInTheDocument ( )
589+
590+ const detailsPanel = screen . queryByText ( / Q u e r y D e t a i l s / i)
591+ expect ( detailsPanel ) . not . toBeInTheDocument ( )
592+ } )
593+
594+ it ( 'should initialize sorting values with ones stored in localstorage' , async ( ) => {
595+ localStorage . setItem ( 'reactQueryDevtoolsSortDesc' , 'true' )
596+ localStorage . setItem (
597+ 'reactQueryDevtoolsSortFn' ,
598+ JSON . stringify ( Object . keys ( sortFns ) [ 1 ] ) ,
599+ )
600+
601+ const { queryClient } = createQueryClient ( )
602+
603+ function Page ( ) {
604+ const { data = 'default' } = useQuery ( [ 'check' ] , async ( ) => {
605+ await sleep ( 10 )
606+ return 'test'
607+ } )
608+
609+ return (
610+ < div >
611+ < h1 > { data } </ h1 >
612+ </ div >
613+ )
614+ }
615+
616+ renderWithClient ( queryClient , < Page /> , {
617+ initialIsOpen : true ,
618+ } )
619+
620+ const sortCombobox : HTMLSelectElement =
621+ screen . getByLabelText ( / S o r t q u e r i e s / i)
622+ expect ( sortCombobox . value ) . toEqual ( Object . keys ( sortFns ) [ 1 ] )
623+
624+ expect ( screen . getByRole ( 'button' , { name : / D e s c / i } ) ) . toBeInTheDocument ( )
625+ } )
626+
627+ it ( 'should initialize filter value with one stored in localstorage' , ( ) => {
628+ localStorage . setItem ( 'reactQueryDevtoolsFilter' , JSON . stringify ( 'posts' ) )
629+
630+ const { queryClient } = createQueryClient ( )
631+
632+ function Page ( ) {
633+ const { data = 'default' } = useQuery ( [ 'check' ] , async ( ) => {
634+ await sleep ( 10 )
635+ return 'test'
636+ } )
637+
638+ return (
639+ < div >
640+ < h1 > { data } </ h1 >
641+ </ div >
642+ )
643+ }
644+
645+ renderWithClient ( queryClient , < Page /> , {
646+ initialIsOpen : true ,
647+ } )
648+
649+ const filterInput : HTMLInputElement =
650+ screen . getByLabelText ( / F i l t e r b y q u e r y h a s h / i)
651+ expect ( filterInput . value ) . toEqual ( 'posts' )
652+ } )
653+
555654 it ( 'style should have a nonce' , async ( ) => {
556655 const { queryClient } = createQueryClient ( )
557656
0 commit comments