From a4566eaf32b4f1be34e4e611d7a8e901dda135bd Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 6 Sep 2022 16:43:59 +0100 Subject: [PATCH] add unit tests for buildBreadcrumbs (#54785) --- .../core/components/Breadcrumbs/utils.test.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 public/app/core/components/Breadcrumbs/utils.test.ts diff --git a/public/app/core/components/Breadcrumbs/utils.test.ts b/public/app/core/components/Breadcrumbs/utils.test.ts new file mode 100644 index 00000000000..f2f22b45f9e --- /dev/null +++ b/public/app/core/components/Breadcrumbs/utils.test.ts @@ -0,0 +1,86 @@ +import { NavModelItem } from '@grafana/data'; + +import { buildBreadcrumbs } from './utils'; + +describe('breadcrumb utils', () => { + describe('buildBreadcrumbs', () => { + it('includes the home breadcrumb at the root', () => { + const sectionNav: NavModelItem = { + text: 'My section', + url: '/my-section', + }; + const result = buildBreadcrumbs(sectionNav); + expect(result[0]).toEqual({ icon: 'home-alt', href: '/', text: 'Home' }); + }); + + it('includes breadcrumbs for the section nav', () => { + const sectionNav: NavModelItem = { + text: 'My section', + url: '/my-section', + }; + expect(buildBreadcrumbs(sectionNav)).toEqual([ + { icon: 'home-alt', href: '/', text: 'Home' }, + { 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(sectionNav, pageNav)).toEqual([ + { icon: 'home-alt', href: '/', text: 'Home' }, + { 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(sectionNav)).toEqual([ + { icon: 'home-alt', href: '/', text: 'Home' }, + { 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(sectionNav, pageNav)).toEqual([ + { icon: 'home-alt', href: '/', text: 'Home' }, + { 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' }, + ]); + }); + }); +});