@@ -23,6 +23,7 @@ describe('DesksStateService', () => {
2323 let active_building : BehaviorSubject < any > ;
2424 let active_region : BehaviorSubject < any > ;
2525 let current_building : any ;
26+ let settings_map : Record < string , any > ;
2627 const organisation_service : any = {
2728 active_levels : of ( [ ] ) ,
2829 initialised : of ( true ) ,
@@ -46,15 +47,15 @@ describe('DesksStateService', () => {
4647 providers : [
4748 MockProvider ( MatDialog , { open : jest . fn ( ) } ) ,
4849 MockProvider ( SettingsService , {
49- get : ( ( name : string ) =>
50- name === 'app.use_region' ? false : undefined ) as any ,
50+ get : ( ( name : string ) => settings_map [ name ] ) as any ,
5151 } as any ) ,
5252 MockProvider ( OrganisationService , organisation_service ) ,
5353 ] ,
5454 } ) ;
5555
5656 beforeEach ( ( ) => {
5757 current_building = { id : 'bld-1' } ;
58+ settings_map = { 'app.use_region' : false } ;
5859 active_building = new BehaviorSubject ( current_building ) ;
5960 active_region = new BehaviorSubject ( { id : 'region-1' } ) ;
6061 organisation_service . active_building = active_building ;
@@ -68,6 +69,9 @@ describe('DesksStateService', () => {
6869 jest . spyOn ( ts_client_mod , 'updateMetadata' ) . mockReturnValue (
6970 of ( { } ) as any ,
7071 ) ;
72+ jest . spyOn ( ts_client_mod , 'showMetadata' ) . mockReturnValue (
73+ of ( { details : [ ] } ) as any ,
74+ ) ;
7175 ( component_mod as any ) . openConfirmModal = jest . fn ( async ( ) => ( {
7276 reason : 'done' ,
7377 loading : jest . fn ( ) ,
@@ -79,6 +83,7 @@ describe('DesksStateService', () => {
7983 ( common_mod as any ) . notifySuccess = jest . fn ( ) ;
8084 ( common_mod as any ) . notifyError = jest . fn ( ) ;
8185 ( common_mod as any ) . unique = jest . fn ( ( list ) => list ) ;
86+ jest . clearAllMocks ( ) ;
8287 spectator = createService ( ) ;
8388 } ) ;
8489
@@ -180,6 +185,51 @@ describe('DesksStateService', () => {
180185 expect ( booking_mod . saveBooking ) . toHaveBeenCalled ( ) ;
181186 } ) ;
182187
188+ it ( 'should block assignments when the desk limit is reached' , async ( ) => {
189+ settings_map [ 'app.desks.max_assigned_count' ] = 1 ;
190+ ( ts_client_mod . showMetadata as jest . Mock ) . mockReturnValue (
191+ of ( {
192+ details : [
193+ {
194+ id : 'desk-existing' ,
195+ assigned_to : 'staff@example.com' ,
196+ assigned_name : 'Staff Name' ,
197+ } ,
198+ ] ,
199+ } ) as any ,
200+ ) ;
201+ const dialog_ref = {
202+ afterClosed : ( ) =>
203+ of ( {
204+ reason : 'done' ,
205+ metadata : {
206+ id : 'desk-new' ,
207+ name : 'Desk New' ,
208+ assigned_to : 'staff@example.com' ,
209+ assigned_name : 'Staff Name' ,
210+ } ,
211+ } ) ,
212+ componentInstance : {
213+ event : new EventEmitter < any > ( ) ,
214+ loading : { set : jest . fn ( ) } ,
215+ } ,
216+ close : jest . fn ( ) ,
217+ } ;
218+ ( spectator . inject ( MatDialog ) . open as any ) . mockReturnValue ( dialog_ref ) ;
219+ spectator . service . setFilters ( { zones : [ 'level-1' ] } ) ;
220+
221+ await spectator . service . editDesk ( { id : 'desk-new-2' } as any ) . catch ( ( ) => undefined ) ;
222+
223+ expect ( common_mod . notifyError ) . toHaveBeenCalledWith (
224+ 'Users can only have 1 assigned desk at a time.' ,
225+ ) ;
226+ expect ( ts_client_mod . updateMetadata ) . not . toHaveBeenCalled ( ) ;
227+ expect ( booking_mod . saveBooking ) . not . toHaveBeenCalled ( ) ;
228+ expect ( dialog_ref . componentInstance . loading . set ) . toHaveBeenCalledWith (
229+ false ,
230+ ) ;
231+ } ) ;
232+
183233 it . todo ( 'should handle loading desk bookings' ) ;
184234 it . todo ( 'should handle loading desk list' ) ;
185235 it . todo ( 'should handle filtering of desk bookings' ) ;
0 commit comments