grafana/public/app/core/components/Breadcrumbs/utils.test.ts
Ashley Harrison 8984507291
Navigation: show breadcrumbs correctly when on the home page (#55759)
* show breadcrumbs correctly when on the home page

* adjust breadcrumb unit tests

* update betterer

* fix backend tests

* update getSectionRoot to look at the home nav id

* remove redundant setting of home dashboard

* construct a home navmodelitem in the backend

* fix cases when the feature toggle is off

* fix unit test

* fix more unit tests

* refactor how buildBreadcrumbs works

* use HOME_NAV_ID

* move homeNav useSelector into NavToolbar

* remove unnecesary cloneDeep

* don't need locationUtil here

* restore using getUrlForPartial in DashboardPage

* special case for the editview query param

* remove commented out code

* add comment to clarify splice behaviour

* slightly cleaner syntax
2022-10-03 15:05:19 +01:00

149 lines
4.4 KiB
TypeScript

import { NavModelItem } from '@grafana/data';
import { buildBreadcrumbs } from './utils';
const mockHomeNav: NavModelItem = {
text: 'Home',
url: '/home',
id: 'home',
};
describe('breadcrumb utils', () => {
describe('buildBreadcrumbs', () => {
it('includes breadcrumbs for the section nav', () => {
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav)).toEqual([{ text: 'My section', href: '/my-section' }]);
});
it('includes breadcrumbs for the page nav', () => {
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
};
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav, pageNav)).toEqual([
{ text: 'My section', href: '/my-section' },
{ text: 'My page', href: '/my-page' },
]);
});
it('includes breadcrumbs for any parents in the section nav', () => {
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
parentItem: {
text: 'My parent section',
url: '/my-parent-section',
},
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav)).toEqual([
{ text: 'My parent section', href: '/my-parent-section' },
{ text: 'My section', href: '/my-section' },
]);
});
it('includes breadcrumbs for any parents in the section nav or page nav', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'My parent page',
url: '/my-parent-page',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
parentItem: {
text: 'My parent section',
url: '/my-parent-section',
},
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav, pageNav)).toEqual([
{ text: 'My parent section', href: '/my-parent-section' },
{ text: 'My section', href: '/my-section' },
{ text: 'My parent page', href: '/my-parent-page' },
{ text: 'My page', href: '/my-page' },
]);
});
it('shortcircuits if the home nav is found early', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'My parent page',
url: '/home',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
parentItem: {
text: 'My parent section',
url: '/my-parent-section',
},
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav, pageNav)).toEqual([
{ text: 'Home', href: '/home' },
{ text: 'My page', href: '/my-page' },
]);
});
it('matches the home nav ignoring query parameters', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'My parent page',
url: '/home?orgId=1',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
parentItem: {
text: 'My parent section',
url: '/my-parent-section',
},
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav, pageNav)).toEqual([
{ text: 'Home', href: '/home?orgId=1' },
{ text: 'My page', href: '/my-page' },
]);
});
it('does not match the home nav if the editview param is different', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'My parent page',
url: '/home?orgId=1&editview=settings',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
parentItem: {
text: 'My parent section',
url: '/my-parent-section',
},
};
expect(buildBreadcrumbs(mockHomeNav, sectionNav, pageNav)).toEqual([
{ text: 'My parent section', href: '/my-parent-section' },
{ text: 'My section', href: '/my-section' },
{ text: 'My parent page', href: '/home?orgId=1&editview=settings' },
{ text: 'My page', href: '/my-page' },
]);
});
});
});