1
- import { render , act , waitFor } from '@testing-library/react'
1
+ import { render , act } from '@testing-library/react'
2
2
import { setupServer } from 'msw/node'
3
3
import { NextAppCsr } from '@siafoundation/design-system'
4
4
import { FilesManagerProvider , useFilesManager , FilesManagerState } from '.'
@@ -9,15 +9,14 @@ import {
9
9
mockMatchMedia ,
10
10
} from '../../mock/mock'
11
11
// eslint-disable-next-line @typescript-eslint/no-var-requires
12
- const { usePathname, useAppRouter } = require ( '@siafoundation/next' )
12
+ const { usePathname, useAppRouter, useParams } = require ( '@siafoundation/next' )
13
13
14
14
// NOTE: Update to use a functional test router after migrating to vitest.
15
15
jest . mock ( '@siafoundation/next' , ( ) => {
16
- const push = jest . fn ( )
17
16
return {
18
17
useAppRouter : jest . fn ( ) . mockReturnValue ( {
19
18
query : { } ,
20
- push,
19
+ push : jest . fn ( ) ,
21
20
} ) ,
22
21
useParams : jest . fn ( ) . mockReturnValue ( { path : [ ] } ) ,
23
22
usePathname : jest . fn ( ) . mockReturnValue ( '/files' ) ,
@@ -33,6 +32,7 @@ beforeAll(() => {
33
32
server . listen ( )
34
33
} )
35
34
beforeEach ( ( ) => {
35
+ jest . clearAllMocks ( )
36
36
localStorage . clear ( )
37
37
} )
38
38
afterEach ( ( ) => server . resetHandlers ( ) )
@@ -41,58 +41,88 @@ afterAll(() => server.close())
41
41
describe ( 'filesManager' , ( ) => {
42
42
it ( 'directory mode navigates to directory with file filter' , async ( ) => {
43
43
useAppRouter . mockClear ( )
44
- usePathname . mockReturnValue ( '/files/default/foo/bar/baz' )
44
+ useParams . mockReturnValue ( {
45
+ bucket : 'default' ,
46
+ path : [ 'foo' , 'bar' , 'baz' ] ,
47
+ } )
48
+ usePathname . mockReturnValue ( '/buckets/default/files/foo/bar/baz' )
45
49
const context = mountProvider ( )
46
- const {
47
- activeExplorerMode,
48
- fileNamePrefixFilter,
49
- navigateToModeSpecificFiltering,
50
- } = context . state
51
- expect ( activeExplorerMode ) . toBe ( 'directory' )
50
+ expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
52
51
act ( ( ) => {
53
- navigateToModeSpecificFiltering ( '/default/photos/cats' )
54
- } )
55
- waitFor ( ( ) => {
56
- const lastPushCallParams = getLastRouterPushCallParams ( )
57
- expect ( lastPushCallParams [ 0 ] ) . toBe ( '/files/default/photos' )
58
- expect ( fileNamePrefixFilter ) . toBe ( 'cats' )
52
+ context . state . navigateToModeSpecificFiltering ( '/default/photos/cats' )
59
53
} )
54
+ const lastPushCallParams = getLastRouterPushCallParams ( )
55
+ expect ( lastPushCallParams [ 0 ] ) . toBe ( '/buckets/default/files/photos' )
56
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( 'cats' )
60
57
} )
61
58
it ( 'toggling to flat explorer mode applies the active directory as a filter' , async ( ) => {
62
59
useAppRouter . mockClear ( )
63
- usePathname . mockReturnValue ( '/files/default/foo/bar/baz' )
60
+ useParams . mockReturnValue ( {
61
+ bucket : 'default' ,
62
+ path : [ 'foo' , 'bar' , 'baz' ] ,
63
+ } )
64
+ usePathname . mockReturnValue ( '/buckets/default/files/foo/bar/baz' )
64
65
const context = mountProvider ( )
65
66
expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
66
67
act ( ( ) => {
67
68
context . state . setExplorerModeFlat ( )
68
69
} )
69
- waitFor ( ( ) => {
70
- expect ( context . state . activeExplorerMode ) . toBe ( 'flat' )
71
- const lastPushCallParams = getLastRouterPushCallParams ( )
72
- expect ( lastPushCallParams [ 0 ] ) . toBe ( '/files/default' )
73
- expect ( context . state . fileNamePrefixFilter ) . toBe ( '/foo/bar/baz' )
74
- } )
70
+ expect ( context . state . activeExplorerMode ) . toBe ( 'flat' )
71
+ const lastPushCallParams = getLastRouterPushCallParams ( )
72
+ expect ( lastPushCallParams [ 0 ] ) . toBe ( '/buckets/default/files/' )
73
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( 'foo/bar/baz/' )
75
74
} )
76
75
it ( 'toggling from flat mode to directory clears the file filter' , async ( ) => {
77
76
useAppRouter . mockClear ( )
78
- usePathname . mockReturnValue ( '/files/foo/bar/baz' )
77
+ useParams . mockReturnValue ( {
78
+ bucket : 'default' ,
79
+ path : [ 'foo' , 'bar' , 'baz' ] ,
80
+ } )
81
+ usePathname . mockReturnValue ( '/buckets/default/files/foo/bar/baz' )
79
82
const context = mountProvider ( )
80
83
act ( ( ) => {
81
84
context . state . setExplorerModeFlat ( )
82
85
} )
83
- waitFor ( ( ) => {
84
- expect ( context . state . activeExplorerMode ) . toBe ( 'flat' )
85
- expect ( context . state . fileNamePrefixFilter ) . toBe ( '/foo/bar/baz' )
86
+ expect ( context . state . activeExplorerMode ) . toBe ( 'flat' )
87
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( 'foo/bar/baz/' )
88
+ act ( ( ) => {
89
+ context . state . setExplorerModeDirectory ( )
90
+ } )
91
+ expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
92
+ const lastPushCallParams = getLastRouterPushCallParams ( )
93
+ expect ( lastPushCallParams [ 0 ] ) . toBe ( '/buckets/default/files/' )
94
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( '' )
95
+ } )
96
+ it ( 'does not routes if mode already active' , async ( ) => {
97
+ useAppRouter . mockClear ( )
98
+ useParams . mockReturnValue ( {
99
+ bucket : 'default' ,
100
+ path : [ ] ,
86
101
} )
102
+ usePathname . mockReturnValue ( '/buckets/default/files' )
103
+ const context = mountProvider ( )
87
104
act ( ( ) => {
88
105
context . state . setExplorerModeDirectory ( )
89
106
} )
90
- waitFor ( ( ) => {
91
- expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
92
- const lastPushCallParams = getLastRouterPushCallParams ( )
93
- expect ( lastPushCallParams [ 0 ] ) . toBe ( '/files/default' )
94
- expect ( context . state . fileNamePrefixFilter ) . toBe ( 'cats' )
107
+ expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
108
+ const pushCount = getRouterPushCallCount ( )
109
+ expect ( pushCount ) . toBe ( 0 )
110
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( '' )
111
+ } )
112
+ it ( 'routes to active mode if viewing uploads' , async ( ) => {
113
+ useAppRouter . mockClear ( )
114
+ useParams . mockReturnValue ( {
115
+ bucket : 'default' ,
116
+ } )
117
+ usePathname . mockReturnValue ( '/buckets/default/uploads' )
118
+ const context = mountProvider ( )
119
+ act ( ( ) => {
120
+ context . state . setExplorerModeDirectory ( )
95
121
} )
122
+ expect ( context . state . activeExplorerMode ) . toBe ( 'directory' )
123
+ const lastPushCallParams = getLastRouterPushCallParams ( )
124
+ expect ( lastPushCallParams [ 0 ] ) . toBe ( '/buckets/default/files/' )
125
+ expect ( context . state . fileNamePrefixFilter ) . toBe ( '' )
96
126
} )
97
127
} )
98
128
@@ -125,6 +155,11 @@ function mountProvider() {
125
155
return context
126
156
}
127
157
158
+ function getRouterPushCallCount ( ) {
159
+ const mockPush = useAppRouter . mock . results . slice ( - 1 ) [ 0 ]
160
+ return mockPush ?. value . push . mock . calls . length
161
+ }
162
+
128
163
function getLastRouterPushCallParams ( ) {
129
164
const mockPush = useAppRouter . mock . results . slice ( - 1 ) [ 0 ]
130
165
const lastPushCallParams = mockPush ?. value . push . mock . calls . slice ( - 1 ) [ 0 ]
0 commit comments