| 
									
										
										
										
											2022-11-07 22:19:31 +08:00
										 |  |  | import { NavModelItem } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2022-10-07 22:33:36 +08:00
										 |  |  | import { HOME_NAV_ID } from 'app/core/reducers/navModel'; | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { buildPluginSectionNav } from './utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('buildPluginSectionNav', () => { | 
					
						
							|  |  |  |   const pluginNav = { main: { text: 'Plugin nav' }, node: { text: 'Plugin nav' } }; | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |   const app1: NavModelItem = { | 
					
						
							|  |  |  |     text: 'App1', | 
					
						
							|  |  |  |     id: 'plugin-page-app1', | 
					
						
							| 
									
										
										
										
											2022-10-05 17:46:27 +08:00
										 |  |  |     url: '/a/plugin1', | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  |     children: [ | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |         text: 'page1', | 
					
						
							|  |  |  |         url: '/a/plugin1/page1', | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: 'page2', | 
					
						
							|  |  |  |         url: '/a/plugin1/page2', | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  |       }, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const appsSection = { | 
					
						
							|  |  |  |     text: 'apps', | 
					
						
							|  |  |  |     id: 'apps', | 
					
						
							|  |  |  |     children: [app1], | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-10 18:47:04 +08:00
										 |  |  |   const home = { | 
					
						
							|  |  |  |     id: HOME_NAV_ID, | 
					
						
							|  |  |  |     text: 'Home', | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |   const adminSection: NavModelItem = { | 
					
						
							|  |  |  |     text: 'Admin', | 
					
						
							|  |  |  |     id: 'admin', | 
					
						
							|  |  |  |     children: [], | 
					
						
							| 
									
										
										
										
											2022-10-10 18:47:04 +08:00
										 |  |  |     parentItem: home, | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const standalonePluginPage = { | 
					
						
							|  |  |  |     id: 'standalone-plugin-page-/a/app2/config', | 
					
						
							|  |  |  |     text: 'Standalone page', | 
					
						
							|  |  |  |     parentItem: adminSection, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   adminSection.children = [standalonePluginPage]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   app1.parentItem = appsSection; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 16:43:11 +08:00
										 |  |  |   it('Should return return section nav', () => { | 
					
						
							| 
									
										
										
										
											2022-11-07 22:19:31 +08:00
										 |  |  |     const result = buildPluginSectionNav(appsSection, pluginNav, '/a/plugin1/page1'); | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  |     expect(result?.main.text).toBe('apps'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('Should set active page', () => { | 
					
						
							| 
									
										
										
										
											2022-11-07 22:19:31 +08:00
										 |  |  |     const result = buildPluginSectionNav(appsSection, null, '/a/plugin1/page2'); | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  |     expect(result?.main.children![0].children![1].active).toBe(true); | 
					
						
							|  |  |  |     expect(result?.node.text).toBe('page2'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 22:10:09 +08:00
										 |  |  |   it('Should only set the most specific match as active (not the parents)', () => { | 
					
						
							|  |  |  |     const result = buildPluginSectionNav(appsSection, null, '/a/plugin1/page2'); | 
					
						
							|  |  |  |     expect(result?.main.children![0].children![1].active).toBe(true); | 
					
						
							|  |  |  |     expect(result?.main.children![0].active).not.toBe(true); // Parent should not be active
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 17:46:27 +08:00
										 |  |  |   it('Should set app section to active', () => { | 
					
						
							| 
									
										
										
										
											2022-11-07 22:19:31 +08:00
										 |  |  |     const result = buildPluginSectionNav(appsSection, null, '/a/plugin1'); | 
					
						
							| 
									
										
										
										
											2022-10-05 17:46:27 +08:00
										 |  |  |     expect(result?.main.children![0].active).toBe(true); | 
					
						
							|  |  |  |     expect(result?.node.text).toBe('App1'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |   it('Should handle standalone page', () => { | 
					
						
							| 
									
										
										
										
											2022-11-07 22:19:31 +08:00
										 |  |  |     const result = buildPluginSectionNav(adminSection, pluginNav, '/a/app2/config'); | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  |     expect(result?.main.text).toBe('Admin'); | 
					
						
							|  |  |  |     expect(result?.node.text).toBe('Standalone page'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-09-05 20:56:08 +08:00
										 |  |  | }); |