| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | import { config } from '@grafana/runtime'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  | import { UnifiedDashboardAPI } from './UnifiedDashboardAPI'; | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | import { getDashboardAPI, setDashboardAPI } from './dashboard_api'; | 
					
						
							|  |  |  | import { LegacyDashboardAPI } from './legacy'; | 
					
						
							| 
									
										
										
										
											2025-02-20 19:42:59 +08:00
										 |  |  | import { K8sDashboardAPI } from './v1'; | 
					
						
							| 
									
										
										
										
											2025-01-02 19:23:58 +08:00
										 |  |  | import { K8sDashboardV2API } from './v2'; | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('DashboardApi', () => { | 
					
						
							|  |  |  |   it('should use legacy api by default', () => { | 
					
						
							|  |  |  |     expect(getDashboardAPI()).toBeInstanceOf(LegacyDashboardAPI); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should allow overriding clients in test environment', () => { | 
					
						
							|  |  |  |     process.env.NODE_ENV = 'test'; | 
					
						
							|  |  |  |     const mockClient = { legacy: new LegacyDashboardAPI() }; | 
					
						
							|  |  |  |     setDashboardAPI(mockClient); | 
					
						
							|  |  |  |     const api = getDashboardAPI(); | 
					
						
							|  |  |  |     expect(api).toBe(mockClient.legacy); | 
					
						
							|  |  |  |     setDashboardAPI(undefined); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when scenes enabled', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       config.featureToggles.dashboardScene = true; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use legacy api kubernetesDashboards toggle is disabled', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = false; | 
					
						
							|  |  |  |       expect(getDashboardAPI()).toBeInstanceOf(LegacyDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |     it('should use legacy when v1 is passed and kubernetesDashboards toggle is disabled', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = false; | 
					
						
							|  |  |  |       expect(getDashboardAPI('v1')).toBeInstanceOf(LegacyDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use unified api when and kubernetesDashboards toggle is enabled', () => { | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |       config.featureToggles.kubernetesDashboards = true; | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |       expect(getDashboardAPI()).toBeInstanceOf(UnifiedDashboardAPI); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |     it('should return v1 if it is passed in the params', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = true; | 
					
						
							|  |  |  |       expect(getDashboardAPI('v1')).toBeInstanceOf(K8sDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return v2 if it is passed in the params', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = true; | 
					
						
							|  |  |  |       expect(getDashboardAPI('v2')).toBeInstanceOf(K8sDashboardV2API); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw an error if v2 is passed in the params and kubernetesDashboards toggle is disabled', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = false; | 
					
						
							|  |  |  |       expect(() => getDashboardAPI('v2')).toThrow('v2 is not supported if kubernetes dashboards are disabled'); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when scenes disabled', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       config.featureToggles.dashboardScene = false; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use legacy api when kubernetesDashboards toggle is disabled', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = undefined; | 
					
						
							|  |  |  |       expect(getDashboardAPI()).toBeInstanceOf(LegacyDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should use legacy api when kubernetesDashboards toggle is disabled', () => { | 
					
						
							|  |  |  |       config.featureToggles.kubernetesDashboards = false; | 
					
						
							|  |  |  |       expect(getDashboardAPI()).toBeInstanceOf(LegacyDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-20 19:42:59 +08:00
										 |  |  |     it('should use v1 api when kubernetesDashboards toggle is enabled', () => { | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |       config.featureToggles.kubernetesDashboards = true; | 
					
						
							|  |  |  |       expect(getDashboardAPI()).toBeInstanceOf(K8sDashboardAPI); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |     it('should use v1 when v1 is passed in the params', () => { | 
					
						
							|  |  |  |       expect(getDashboardAPI('v1')).toBeInstanceOf(K8sDashboardAPI); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 01:43:32 +08:00
										 |  |  |     it('should use v2 when v2 is passed in the params', () => { | 
					
						
							|  |  |  |       expect(() => getDashboardAPI('v2')).toThrow('v2 is not supported for legacy architecture'); | 
					
						
							| 
									
										
										
										
											2024-12-18 05:17:09 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |