@@ -12,6 +12,8 @@ interface SetupOptions {
1212 folderId ?: number
1313 libraryFilter ?: string
1414 snippetId ?: number
15+ sort ?: 'name' | 'updatedAt'
16+ order ?: 'ASC' | 'DESC'
1517 tagId ?: number
1618}
1719
@@ -56,7 +58,8 @@ async function setup(options: SetupOptions = {}) {
5658 // чтобы не тянуть electron store в тест.
5759 vi . doMock ( '@/composables/useContentSort' , ( ) => ( {
5860 useContentSort : ( ) => ( {
59- getContentSortQuery : ( ) => ( { } ) ,
61+ getContentSortQuery : ( ) =>
62+ options . sort ? { sort : options . sort , order : options . order } : { } ,
6063 } ) ,
6164 } ) )
6265
@@ -349,3 +352,67 @@ describe('selected snippet full record', () => {
349352 consoleError . mockRestore ( )
350353 } )
351354} )
355+
356+ describe ( 'snippet rename refresh' , ( ) => {
357+ it . each ( [ 'name' , 'updatedAt' ] as const ) (
358+ 'refreshes %s order without reloading the selected record' ,
359+ async ( sort ) => {
360+ const context = await setup ( { snippetId : 1 , sort, order : 'ASC' } )
361+ await context . snippets . refreshSelectedSnippet ( )
362+ await context . snippets . getSnippets ( )
363+ const fullRecordCalls = context . getSnippetsById . mock . calls . length
364+ context . getSnippets . mockResolvedValueOnce ( {
365+ data : [
366+ { id : 2 , name : 'B' , contents : [ ] , tags : [ ] } ,
367+ { id : 1 , name : 'Z' , contents : [ ] , tags : [ ] } ,
368+ ] ,
369+ } )
370+
371+ await context . snippets . updateSnippet ( 1 , { name : 'Z' } )
372+
373+ expect ( context . getSnippets ) . toHaveBeenLastCalledWith (
374+ expect . objectContaining ( { sort, order : 'ASC' } ) ,
375+ )
376+ expect (
377+ context . snippets . displayedSnippets . value ?. map ( item => item . id ) ,
378+ ) . toEqual ( [ 2 , 1 ] )
379+ expect ( context . snippets . selectedSnippet . value ?. name ) . toBe ( 'Z' )
380+ expect ( context . snippets . displayedSnippetContent . value ?. value ) . toBe (
381+ 'Content 1' ,
382+ )
383+ expect ( context . snippets . selectedSnippetRecordStatus . value ) . toBe ( 'ready' )
384+ expect ( context . getSnippetsById ) . toHaveBeenCalledTimes ( fullRecordCalls )
385+ } ,
386+ )
387+
388+ it ( 'removes and restores a search result while retaining the open record' , async ( ) => {
389+ const context = await setup ( { snippetId : 1 } )
390+ context . snippets . searchQuery . value = 'needle'
391+ await context . snippets . search ( )
392+ await context . snippets . refreshSelectedSnippet ( )
393+ const fullRecordCalls = context . getSnippetsById . mock . calls . length
394+ context . getSnippets . mockResolvedValueOnce ( { data : [ ] } )
395+
396+ await context . snippets . updateSnippet ( 1 , { name : 'Renamed' } )
397+
398+ expect ( context . getSnippets ) . toHaveBeenLastCalledWith ( { search : 'needle' } )
399+ expect ( context . snippets . displayedSnippets . value ) . toEqual ( [ ] )
400+ expect ( context . state . snippetId ) . toBe ( 1 )
401+ expect ( context . state . snippetContentIndex ) . toBe ( 0 )
402+ expect ( context . snippets . displayedSnippet . value ?. name ) . toBe ( 'Renamed' )
403+ expect ( context . snippets . displayedSnippetContent . value ?. value ) . toBe (
404+ 'Content 1' ,
405+ )
406+ expect ( context . snippets . selectedSnippetRecordStatus . value ) . toBe ( 'ready' )
407+
408+ context . getSnippets . mockResolvedValueOnce ( {
409+ data : [ { id : 1 , name : 'needle again' , contents : [ ] , tags : [ ] } ] ,
410+ } )
411+ await context . snippets . updateSnippet ( 1 , { name : 'needle again' } )
412+
413+ expect (
414+ context . snippets . displayedSnippets . value ?. map ( item => item . id ) ,
415+ ) . toEqual ( [ 1 ] )
416+ expect ( context . getSnippetsById ) . toHaveBeenCalledTimes ( fullRecordCalls )
417+ } )
418+ } )
0 commit comments