@@ -2,20 +2,31 @@ import { act, renderHook, waitFor } from '@testing-library/react';
2
2
import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
3
3
import { describe , it , vi } from 'vitest' ;
4
4
import {
5
+ useAllVolumes ,
5
6
useAttachVolume ,
6
7
useDeleteVolume ,
7
8
useDetachVolume ,
8
9
useVolume ,
9
10
useVolumes ,
10
11
} from '@/api/hooks/useVolume' ;
11
12
import {
12
- getVolume ,
13
- getAllVolumes ,
14
- deleteVolume ,
15
13
attachVolume ,
14
+ deleteVolume ,
16
15
detachVolume ,
16
+ getAllVolumes ,
17
+ getVolume ,
17
18
TAPIVolume ,
18
19
} from '@/api/data/volume' ;
20
+ import {
21
+ mapVolumeAttach ,
22
+ mapVolumeEncryption ,
23
+ mapVolumePricing ,
24
+ mapVolumeRegion ,
25
+ mapVolumeStatus ,
26
+ mapVolumeType ,
27
+ paginateResults ,
28
+ sortResults ,
29
+ } from '@/api/select/volume' ;
19
30
20
31
vi . mock ( '@/api/data/volume' , async ( importOriginal ) => {
21
32
const actual : any = await importOriginal ( ) ;
@@ -29,6 +40,23 @@ vi.mock('@/api/data/volume', async (importOriginal) => {
29
40
} ;
30
41
} ) ;
31
42
43
+ vi . mock ( '@/api/select/volume' , async ( importOriginal ) => {
44
+ const actual : any = await importOriginal ( ) ;
45
+ return {
46
+ ...actual ,
47
+ mapVolumeAttach : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
48
+ mapVolumeEncryption : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
49
+ mapVolumePricing : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
50
+ mapVolumeRegion : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
51
+ mapVolumeStatus : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
52
+ mapVolumeToAdd : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
53
+ mapVolumeToEdit : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
54
+ mapVolumeType : vi . fn ( ) . mockReturnValue ( vi . fn ( ) ) ,
55
+ paginateResults : vi . fn ( ) ,
56
+ sortResults : vi . fn ( ) ,
57
+ } ;
58
+ } ) ;
59
+
32
60
vi . mock ( '@/api/data/catalog' ) ;
33
61
34
62
const queryClient = new QueryClient ( ) ;
@@ -49,21 +77,8 @@ const onSuccess = vi.fn();
49
77
const onError = vi . fn ( ) ;
50
78
51
79
describe ( 'useVolume' , ( ) => {
52
- it ( 'returns volume data when volumeId is provided' , async ( ) => {
53
- const volumeMock = {
54
- id : '1' ,
55
- name : 'Volume 1' ,
56
- attachedTo : [ ] ,
57
- creationDate : '' ,
58
- description : '' ,
59
- size : 0 ,
60
- status : '' ,
61
- region : 'region1' ,
62
- bootable : false ,
63
- planCode : '' ,
64
- type : 'model1' ,
65
- availabilityZone : 'any' ,
66
- } as TAPIVolume ;
80
+ it ( 'returns mapped volume data when volumeId is provided' , async ( ) => {
81
+ const volumeMock = { id : '1' } as TAPIVolume ;
67
82
68
83
vi . mocked ( getVolume ) . mockResolvedValue ( volumeMock ) ;
69
84
@@ -73,7 +88,12 @@ describe('useVolume', () => {
73
88
74
89
await waitFor ( ( ) => {
75
90
expect ( getVolume ) . toHaveBeenCalledWith ( '123' , '1' ) ;
76
- expect ( result . current . data ) . toEqual ( expect . objectContaining ( volumeMock ) ) ;
91
+ expect ( mapVolumeAttach ) . toHaveBeenCalled ( ) ;
92
+ expect ( mapVolumeEncryption ) . toHaveBeenCalled ( ) ;
93
+ expect ( mapVolumeStatus ) . toHaveBeenCalled ( ) ;
94
+ expect ( mapVolumeRegion ) . toHaveBeenCalled ( ) ;
95
+ expect ( mapVolumePricing ) . toHaveBeenCalled ( ) ;
96
+ expect ( result . current . isPending ) . toBe ( false ) ;
77
97
} ) ;
78
98
} ) ;
79
99
@@ -86,36 +106,10 @@ describe('useVolume', () => {
86
106
} ) ;
87
107
88
108
describe ( 'useVolumes' , ( ) => {
89
- it ( 'returns volumes data when projectId is provided' , async ( ) => {
109
+ it ( 'returns volumes data mapped, paginated and sorted when projectId is provided' , async ( ) => {
90
110
const volumesMock : TAPIVolume [ ] = [
91
- {
92
- id : '1' ,
93
- name : 'Volume 1' ,
94
- attachedTo : [ 'inst1' ] ,
95
- creationDate : '' ,
96
- description : '' ,
97
- size : 0 ,
98
- status : 'available' ,
99
- region : 'region1' ,
100
- bootable : false ,
101
- planCode : '' ,
102
- type : 'model1' ,
103
- availabilityZone : 'any' ,
104
- } ,
105
- {
106
- id : '2' ,
107
- name : 'Volume 2' ,
108
- attachedTo : [ ] ,
109
- creationDate : '' ,
110
- description : '' ,
111
- size : 0 ,
112
- status : 'available' ,
113
- region : 'region2' ,
114
- bootable : false ,
115
- planCode : '' ,
116
- type : 'model1' ,
117
- availabilityZone : 'any' ,
118
- } ,
111
+ { id : '1' } ,
112
+ { id : '2' } ,
119
113
] as TAPIVolume [ ] ;
120
114
121
115
vi . mocked ( getAllVolumes ) . mockResolvedValue ( volumesMock ) ;
@@ -133,48 +127,14 @@ describe('useVolumes', () => {
133
127
134
128
await waitFor ( ( ) => {
135
129
expect ( getAllVolumes ) . toHaveBeenCalledWith ( '123' ) ;
136
- expect ( result . current . data ) . toEqual ( {
137
- pageCount : 1 ,
138
- rows : [
139
- expect . objectContaining ( {
140
- attachedTo : [ 'inst1' ] ,
141
- bootable : false ,
142
- creationDate : '' ,
143
- description : '' ,
144
- id : '1' ,
145
- name : 'Volume 1' ,
146
- planCode : '' ,
147
- region : 'region1' ,
148
- regionName : 'region:manager_components_region_region_micro' ,
149
- size : 0 ,
150
- status : 'available' ,
151
- statusGroup : 'ACTIVE' ,
152
- type : 'model1' ,
153
- availabilityZone : 'any' ,
154
- canAttachInstance : false ,
155
- canDetachInstance : true ,
156
- } ) ,
157
- expect . objectContaining ( {
158
- attachedTo : [ ] ,
159
- bootable : false ,
160
- creationDate : '' ,
161
- description : '' ,
162
- id : '2' ,
163
- name : 'Volume 2' ,
164
- planCode : '' ,
165
- region : 'region2' ,
166
- regionName : 'region:manager_components_region_region_micro' ,
167
- size : 0 ,
168
- status : 'available' ,
169
- statusGroup : 'ACTIVE' ,
170
- type : 'model1' ,
171
- availabilityZone : 'any' ,
172
- canAttachInstance : true ,
173
- canDetachInstance : false ,
174
- } ) ,
175
- ] ,
176
- totalRows : 2 ,
177
- } ) ;
130
+ expect ( mapVolumeStatus ) . toHaveBeenCalled ( ) ;
131
+ expect ( mapVolumeRegion ) . toHaveBeenCalled ( ) ;
132
+ expect ( mapVolumeAttach ) . toHaveBeenCalled ( ) ;
133
+ expect ( mapVolumeEncryption ) . toHaveBeenCalled ( ) ;
134
+ expect ( mapVolumeType ) . toHaveBeenCalled ( ) ;
135
+ expect ( paginateResults ) . toHaveBeenCalled ( ) ;
136
+ expect ( sortResults ) . toHaveBeenCalled ( ) ;
137
+ expect ( result . current . isPending ) . toBe ( false ) ;
178
138
} ) ;
179
139
} ) ;
180
140
@@ -193,6 +153,39 @@ describe('useVolumes', () => {
193
153
} ) ;
194
154
} ) ;
195
155
156
+ describe ( 'useAllVolumes' , ( ) => {
157
+ it ( 'returns volumes data when projectId is provided' , async ( ) => {
158
+ const volumesMock : TAPIVolume [ ] = [
159
+ { id : '1' } ,
160
+ { id : '2' } ,
161
+ ] as TAPIVolume [ ] ;
162
+
163
+ vi . mocked ( getAllVolumes ) . mockResolvedValue ( volumesMock ) ;
164
+
165
+ const { result } = renderHook ( ( ) => useAllVolumes ( '123' ) , {
166
+ wrapper,
167
+ } ) ;
168
+
169
+ await waitFor ( ( ) => {
170
+ expect ( getAllVolumes ) . toHaveBeenCalledWith ( '123' ) ;
171
+ expect ( getAllVolumes ) . toHaveBeenCalledWith ( '123' ) ;
172
+ expect ( mapVolumeStatus ) . toHaveBeenCalled ( ) ;
173
+ expect ( mapVolumeRegion ) . toHaveBeenCalled ( ) ;
174
+ expect ( mapVolumeAttach ) . toHaveBeenCalled ( ) ;
175
+ expect ( mapVolumeEncryption ) . toHaveBeenCalled ( ) ;
176
+ expect ( mapVolumeType ) . toHaveBeenCalled ( ) ;
177
+ expect ( result . current . isPending ) . toBe ( false ) ;
178
+ } ) ;
179
+ } ) ;
180
+
181
+ it ( 'does not fetch data when projectId is not provided' , ( ) => {
182
+ const { result } = renderHook ( ( ) => useAllVolumes ( null ) , { wrapper } ) ;
183
+
184
+ expect ( getAllVolumes ) . not . toHaveBeenCalled ( ) ;
185
+ expect ( result . current . isPending ) . toBe ( true ) ;
186
+ } ) ;
187
+ } ) ;
188
+
196
189
describe ( 'useDeleteVolume' , ( ) => {
197
190
it ( 'deletes volume when volumeId is provided' , async ( ) => {
198
191
vi . mocked ( deleteVolume ) . mockResolvedValue ( null ) ;
0 commit comments