@@ -25,6 +25,18 @@ vi.mock("~/helpers/teams/TeamListAdapter", () => ({
2525 } ,
2626} ) )
2727
28+ // Mock TeamCollectionsService to prevent i18n dependency issues
29+ vi . mock ( "../team-collection.service" , ( ) => ( {
30+ TeamCollectionsService : class MockTeamCollectionsService {
31+ static readonly ID = "TEAM_COLLECTIONS_SERVICE"
32+
33+ changeTeamID = vi . fn ( )
34+ clearCollections = vi . fn ( )
35+
36+ onServiceInit = vi . fn ( )
37+ } ,
38+ } ) )
39+
2840describe ( "WorkspaceService" , ( ) => {
2941 const platformMock = {
3042 auth : {
@@ -239,4 +251,226 @@ describe("WorkspaceService", () => {
239251 expect ( listAdapterMock . fetchList ) . not . toHaveBeenCalled ( )
240252 } )
241253 } )
254+
255+ describe ( "Team Collection Service Synchronization" , ( ) => {
256+ it ( "should call changeTeamID when workspace changes to a team workspace" , async ( ) => {
257+ const container = new TestContainer ( )
258+ const service = container . bind ( WorkspaceService )
259+
260+ // Access the team collection service mock
261+ const teamCollectionServiceMock = ( service as any ) . teamCollectionService
262+
263+ // Change to team workspace
264+ service . changeWorkspace ( {
265+ type : "team" ,
266+ teamID : "team-123" ,
267+ teamName : "Test Team" ,
268+ role : null ,
269+ } )
270+
271+ await nextTick ( )
272+
273+ expect ( teamCollectionServiceMock . changeTeamID ) . toHaveBeenCalledWith (
274+ "team-123"
275+ )
276+ } )
277+
278+ it ( "should call clearCollections when workspace changes to personal workspace" , async ( ) => {
279+ const container = new TestContainer ( )
280+ const service = container . bind ( WorkspaceService )
281+
282+ // Start with a team workspace
283+ service . changeWorkspace ( {
284+ type : "team" ,
285+ teamID : "team-123" ,
286+ teamName : "Test Team" ,
287+ role : null ,
288+ } )
289+
290+ await nextTick ( )
291+
292+ const teamCollectionServiceMock = ( service as any ) . teamCollectionService
293+ teamCollectionServiceMock . clearCollections . mockClear ( )
294+
295+ // Change to personal workspace
296+ service . changeWorkspace ( {
297+ type : "personal" ,
298+ } )
299+
300+ await nextTick ( )
301+
302+ expect ( teamCollectionServiceMock . clearCollections ) . toHaveBeenCalled ( )
303+ } )
304+
305+ it ( "should call clearCollections when workspace changes to team workspace without teamID" , async ( ) => {
306+ const container = new TestContainer ( )
307+ const service = container . bind ( WorkspaceService )
308+
309+ const teamCollectionServiceMock = ( service as any ) . teamCollectionService
310+
311+ // Change to team workspace without teamID
312+ service . changeWorkspace ( {
313+ type : "team" ,
314+ teamID : "" ,
315+ teamName : "Test Team" ,
316+ role : null ,
317+ } )
318+
319+ await nextTick ( )
320+
321+ expect ( teamCollectionServiceMock . clearCollections ) . toHaveBeenCalled ( )
322+ } )
323+
324+ it ( "should not sync when workspaces are effectively the same" , async ( ) => {
325+ const container = new TestContainer ( )
326+ const service = container . bind ( WorkspaceService )
327+
328+ // Start with a team workspace
329+ service . changeWorkspace ( {
330+ type : "team" ,
331+ teamID : "team-123" ,
332+ teamName : "Test Team" ,
333+ role : null ,
334+ } )
335+
336+ await nextTick ( )
337+
338+ const teamCollectionServiceMock = ( service as any ) . teamCollectionService
339+ teamCollectionServiceMock . changeTeamID . mockClear ( )
340+
341+ // Change to same team workspace (different name, same ID)
342+ service . changeWorkspace ( {
343+ type : "team" ,
344+ teamID : "team-123" ,
345+ teamName : "Updated Team Name" ,
346+ role : null ,
347+ } )
348+
349+ await nextTick ( )
350+
351+ // Should not call changeTeamID again since it's the same team
352+ expect ( teamCollectionServiceMock . changeTeamID ) . not . toHaveBeenCalled ( )
353+ } )
354+
355+ it ( "should handle errors during team collection service sync gracefully" , async ( ) => {
356+ const container = new TestContainer ( )
357+ const service = container . bind ( WorkspaceService )
358+
359+ const teamCollectionServiceMock = ( service as any ) . teamCollectionService
360+ teamCollectionServiceMock . changeTeamID . mockImplementation ( ( ) => {
361+ throw new Error ( "Sync failed" )
362+ } )
363+
364+ const consoleSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
365+
366+ // Change to team workspace (should not throw)
367+ expect ( ( ) => {
368+ service . changeWorkspace ( {
369+ type : "team" ,
370+ teamID : "team-123" ,
371+ teamName : "Test Team" ,
372+ role : null ,
373+ } )
374+ } ) . not . toThrow ( )
375+
376+ await nextTick ( )
377+
378+ expect ( consoleSpy ) . toHaveBeenCalledWith (
379+ "Failed to sync team collections:" ,
380+ expect . any ( Error )
381+ )
382+
383+ consoleSpy . mockRestore ( )
384+ } )
385+ } )
386+
387+ describe ( "areWorkspacesEqual" , ( ) => {
388+ let service : WorkspaceService
389+
390+ beforeEach ( ( ) => {
391+ const container = new TestContainer ( )
392+ service = container . bind ( WorkspaceService )
393+ } )
394+
395+ it ( "should return false when newWorkspace is undefined" , ( ) => {
396+ const result = ( service as any ) . areWorkspacesEqual ( undefined , {
397+ type : "personal" ,
398+ } )
399+ expect ( result ) . toBe ( false )
400+ } )
401+
402+ it ( "should return false when oldWorkspace is undefined" , ( ) => {
403+ const result = ( service as any ) . areWorkspacesEqual (
404+ { type : "personal" } ,
405+ undefined
406+ )
407+ expect ( result ) . toBe ( false )
408+ } )
409+
410+ it ( "should return true when both workspaces are personal" , ( ) => {
411+ const result = ( service as any ) . areWorkspacesEqual (
412+ { type : "personal" } ,
413+ { type : "personal" }
414+ )
415+ expect ( result ) . toBe ( true )
416+ } )
417+
418+ it ( "should return true when both workspaces are team workspaces with same teamID" , ( ) => {
419+ const workspace1 = {
420+ type : "team" ,
421+ teamID : "team-123" ,
422+ teamName : "Team A" ,
423+ role : null ,
424+ }
425+ const workspace2 = {
426+ type : "team" ,
427+ teamID : "team-123" ,
428+ teamName : "Team A Updated" ,
429+ role : null ,
430+ }
431+
432+ const result = ( service as any ) . areWorkspacesEqual ( workspace1 , workspace2 )
433+ expect ( result ) . toBe ( true )
434+ } )
435+
436+ it ( "should return false when team workspaces have different teamIDs" , ( ) => {
437+ const workspace1 = {
438+ type : "team" ,
439+ teamID : "team-123" ,
440+ teamName : "Team A" ,
441+ role : null ,
442+ }
443+ const workspace2 = {
444+ type : "team" ,
445+ teamID : "team-456" ,
446+ teamName : "Team B" ,
447+ role : null ,
448+ }
449+
450+ const result = ( service as any ) . areWorkspacesEqual ( workspace1 , workspace2 )
451+ expect ( result ) . toBe ( false )
452+ } )
453+
454+ it ( "should return false when one is personal and other is team workspace" , ( ) => {
455+ const personalWorkspace = { type : "personal" }
456+ const teamWorkspace = {
457+ type : "team" ,
458+ teamID : "team-123" ,
459+ teamName : "Team A" ,
460+ role : null ,
461+ }
462+
463+ const result1 = ( service as any ) . areWorkspacesEqual (
464+ personalWorkspace ,
465+ teamWorkspace
466+ )
467+ const result2 = ( service as any ) . areWorkspacesEqual (
468+ teamWorkspace ,
469+ personalWorkspace
470+ )
471+
472+ expect ( result1 ) . toBe ( false )
473+ expect ( result2 ) . toBe ( false )
474+ } )
475+ } )
242476} )
0 commit comments