@@ -39,35 +39,27 @@ import UserEditor from '../../src/lib/components/v2/admin/UserEditor.svelte';
3939describe ( 'UserEditor' , ( ) => {
4040 beforeEach ( ( ) => {
4141 /** @type {import('vitest').Mock } */ ( fetch ) . mockClear ( ) ;
42+ mockResourcesAndProfiles ( ) ;
4243 } ) ;
4344
4445 const selectedUser = mockUser ( ) ;
4546
46- /**
47- * @type {() => Promise<Response> }
48- */
49- const mockSaveUser = vi . fn ( ) ;
50-
5147 it ( 'Update settings - success' , async ( ) => {
52- const mockRequest = mockSettingsUpdate ( {
53- ok : true ,
54- status : 200 ,
55- json : ( ) =>
56- new Promise ( ( resolve ) =>
57- resolve ( {
58- slurm_accounts : [ ] ,
59- project_dir : '/path/to/project/dir'
60- } )
61- )
62- } ) ;
63-
6448 const user = userEvent . setup ( ) ;
6549
50+ const mockSaveUser = vi . fn ( async ( u ) => {
51+ return /** @type {Response } */ ( {
52+ ok : true ,
53+ status : 200 ,
54+ json : ( ) => new Promise ( ( resolve ) => resolve ( u ) )
55+ } ) ;
56+ } ) ;
57+
6658 render ( UserEditor , {
6759 props : {
6860 runnerBackend : 'slurm_sudo' ,
69- user : selectedUser ,
70- settings : {
61+ user : {
62+ ... selectedUser ,
7163 slurm_accounts : [ ] ,
7264 project_dir : null
7365 } ,
@@ -79,42 +71,41 @@ describe('UserEditor', () => {
7971 await user . click ( screen . getByRole ( 'button' , { name : 'Save' } ) ) ;
8072 await screen . findByText ( 'User successfully updated' ) ;
8173
82- expect ( mockRequest ) . toHaveBeenCalledWith (
83- expect . anything ( ) ,
74+ expect ( mockSaveUser ) . toHaveBeenCalledWith (
8475 expect . objectContaining ( {
85- body : JSON . stringify ( {
86- slurm_accounts : [ ] ,
87- project_dir : '/path/to/project/dir'
88- } )
76+ slurm_accounts : [ ] ,
77+ project_dir : '/path/to/project/dir'
8978 } )
9079 ) ;
9180 } ) ;
9281
9382 it ( 'Update settings - validation error' , async ( ) => {
94- const mockRequest = mockSettingsUpdate ( {
95- ok : false ,
96- status : 422 ,
97- json : ( ) =>
98- new Promise ( ( resolve ) =>
99- resolve ( {
100- detail : [
101- {
102- loc : [ 'body' , 'project_dir' ] ,
103- msg : 'mocked_error' ,
104- type : 'value_error'
105- }
106- ]
107- } )
108- )
109- } ) ;
110-
11183 const user = userEvent . setup ( ) ;
11284
85+ const mockSaveUser = vi . fn ( async ( ) => {
86+ return /** @type {Response } */ ( {
87+ ok : false ,
88+ status : 422 ,
89+ json : ( ) =>
90+ new Promise ( ( resolve ) =>
91+ resolve ( {
92+ detail : [
93+ {
94+ loc : [ 'body' , 'project_dir' ] ,
95+ msg : 'mocked_error' ,
96+ type : 'value_error'
97+ }
98+ ]
99+ } )
100+ )
101+ } ) ;
102+ } ) ;
103+
113104 render ( UserEditor , {
114105 props : {
115106 runnerBackend : 'slurm_sudo' ,
116- user : selectedUser ,
117- settings : {
107+ user : {
108+ ... selectedUser ,
118109 slurm_accounts : [ ] ,
119110 project_dir : null
120111 } ,
@@ -126,37 +117,30 @@ describe('UserEditor', () => {
126117 await user . click ( screen . getByRole ( 'button' , { name : 'Save' } ) ) ;
127118 await screen . findByText ( 'mocked_error' ) ;
128119
129- expect ( mockRequest ) . toHaveBeenCalledWith (
130- expect . anything ( ) ,
120+ expect ( mockSaveUser ) . toHaveBeenCalledWith (
131121 expect . objectContaining ( {
132- body : JSON . stringify ( {
133- slurm_accounts : [ ] ,
134- project_dir : 'xxx'
135- } )
122+ slurm_accounts : [ ] ,
123+ project_dir : 'xxx'
136124 } )
137125 ) ;
138126 } ) ;
139127
140128 it ( 'Update settings - add slurm accounts' , async ( ) => {
141- const mockRequest = mockSettingsUpdate ( {
142- ok : true ,
143- status : 200 ,
144- json : ( ) =>
145- new Promise ( ( resolve ) =>
146- resolve ( {
147- slurm_accounts : [ 'foo' , 'bar' ] ,
148- project_dir : '/path/to/project/dir'
149- } )
150- )
129+ const mockSaveUser = vi . fn ( async ( u ) => {
130+ return /** @type {Response } */ ( {
131+ ok : true ,
132+ status : 200 ,
133+ json : ( ) => new Promise ( ( resolve ) => resolve ( u ) )
134+ } ) ;
151135 } ) ;
152136
153137 const user = userEvent . setup ( ) ;
154138
155139 render ( UserEditor , {
156140 props : {
157141 runnerBackend : 'slurm_sudo' ,
158- user : selectedUser ,
159- settings : {
142+ user : {
143+ ... selectedUser ,
160144 slurm_accounts : [ ] ,
161145 project_dir : '/path/to/project/dir'
162146 } ,
@@ -174,37 +158,30 @@ describe('UserEditor', () => {
174158 await user . click ( screen . getByRole ( 'button' , { name : 'Save' } ) ) ;
175159 await screen . findByText ( 'User successfully updated' ) ;
176160
177- expect ( mockRequest ) . toHaveBeenCalledWith (
178- expect . anything ( ) ,
161+ expect ( mockSaveUser ) . toHaveBeenCalledWith (
179162 expect . objectContaining ( {
180- body : JSON . stringify ( {
181- slurm_accounts : [ 'foo' , 'bar' ] ,
182- project_dir : '/path/to/project/dir'
183- } )
163+ slurm_accounts : [ 'foo' , 'bar' ] ,
164+ project_dir : '/path/to/project/dir'
184165 } )
185166 ) ;
186167 } ) ;
187168
188169 it ( 'Update settings - remove slurm account' , async ( ) => {
189- const mockRequest = mockSettingsUpdate ( {
190- ok : true ,
191- status : 200 ,
192- json : ( ) =>
193- new Promise ( ( resolve ) =>
194- resolve ( {
195- slurm_accounts : [ 'foo' , 'bar' ] ,
196- project_dir : '/path/to/project/dir'
197- } )
198- )
170+ const mockSaveUser = vi . fn ( async ( u ) => {
171+ return /** @type {Response } */ ( {
172+ ok : true ,
173+ status : 200 ,
174+ json : ( ) => new Promise ( ( resolve ) => resolve ( u ) )
175+ } ) ;
199176 } ) ;
200177
201178 const user = userEvent . setup ( ) ;
202179
203180 render ( UserEditor , {
204181 props : {
205182 runnerBackend : 'slurm_sudo' ,
206- user : selectedUser ,
207- settings : {
183+ user : {
184+ ... selectedUser ,
208185 slurm_accounts : [ 'foo' , 'bar' ] ,
209186 project_dir : '/path/to/project/dir'
210187 } ,
@@ -219,19 +196,16 @@ describe('UserEditor', () => {
219196 await user . click ( screen . getByRole ( 'button' , { name : 'Save' } ) ) ;
220197 await screen . findByText ( 'User successfully updated' ) ;
221198
222- expect ( mockRequest ) . toHaveBeenCalledWith (
223- expect . anything ( ) ,
199+ expect ( mockSaveUser ) . toHaveBeenCalledWith (
224200 expect . objectContaining ( {
225- body : JSON . stringify ( {
226- slurm_accounts : [ 'foo' ] ,
227- project_dir : '/path/to/project/dir'
228- } )
201+ slurm_accounts : [ 'foo' ] ,
202+ project_dir : '/path/to/project/dir'
229203 } )
230204 ) ;
231205 } ) ;
232206} ) ;
233207
234- function mockSettingsUpdate ( updateSettings ) {
208+ function mockResourcesAndProfiles ( ) {
235209 return /** @type {import('vitest').Mock } */ ( fetch )
236210 . mockResolvedValueOnce ( {
237211 ok : true ,
@@ -250,6 +224,5 @@ function mockSettingsUpdate(updateSettings) {
250224 status : 200 ,
251225 // mock profiles
252226 json : ( ) => new Promise ( ( resolve ) => resolve ( [ { id : 1 } ] ) )
253- } )
254- . mockResolvedValue ( updateSettings ) ;
227+ } ) ;
255228}
0 commit comments