From a1b3927f736857915f0756dceeff40c21756c76e Mon Sep 17 00:00:00 2001 From: Delmer Reed Date: Thu, 12 Jul 2018 11:21:39 -0400 Subject: [PATCH] Add test for updating dashboard name link --- .../dashboards/containers/DashboardPage.tsx | 6 +- .../utils/dashboardSwitcherLinks.ts | 2 +- .../utils/dashboardSwitcherLinks.test.ts | 91 ++++++++++++++++--- 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/ui/src/dashboards/containers/DashboardPage.tsx b/ui/src/dashboards/containers/DashboardPage.tsx index 5e8716426..5c2d576ed 100644 --- a/ui/src/dashboards/containers/DashboardPage.tsx +++ b/ui/src/dashboards/containers/DashboardPage.tsx @@ -26,7 +26,7 @@ import {millisecondTimeRange} from 'src/dashboards/utils/time' import {getDeep} from 'src/utils/wrappers' import { updateActiveDashboardLink, - updateDashboadLinkName, + updateDashboardLinkName, } from 'src/dashboards/utils/dashboardSwitcherLinks' // APIs @@ -448,10 +448,10 @@ class DashboardPage extends Component { this.updateLinkName() } - private updateLinkName = () => { + private updateLinkName(): void { this.setState((prevState, props) => ({ ...prevState, - dashboardLinks: updateDashboadLinkName( + dashboardLinks: updateDashboardLinkName( prevState.dashboardLinks, props.dashboard ), diff --git a/ui/src/dashboards/utils/dashboardSwitcherLinks.ts b/ui/src/dashboards/utils/dashboardSwitcherLinks.ts index 7bab002ad..6c13d53d4 100644 --- a/ui/src/dashboards/utils/dashboardSwitcherLinks.ts +++ b/ui/src/dashboards/utils/dashboardSwitcherLinks.ts @@ -36,7 +36,7 @@ export const updateActiveDashboardLink = ( return {...dashboardLinks, active} } -export const updateDashboadLinkName = ( +export const updateDashboardLinkName = ( dashboardLinks: DashboardSwitcherLinks, dashboard: Dashboard ): DashboardSwitcherLinks => { diff --git a/ui/test/dashboards/utils/dashboardSwitcherLinks.test.ts b/ui/test/dashboards/utils/dashboardSwitcherLinks.test.ts index 265778c7e..4d5b3bd7d 100644 --- a/ui/test/dashboards/utils/dashboardSwitcherLinks.test.ts +++ b/ui/test/dashboards/utils/dashboardSwitcherLinks.test.ts @@ -1,6 +1,7 @@ import { linksFromDashboards, updateActiveDashboardLink, + updateDashboardLinkName, } from 'src/dashboards/utils/dashboardSwitcherLinks' import {dashboard, source} from 'test/resources' @@ -34,6 +35,18 @@ describe('dashboards.utils.dashboardSwitcherLinks', () => { }) }) + const link1 = { + key: '9001', + text: 'Low Dash', + to: '/sources/897/dashboards/9001', + } + + const link2 = { + key: '2282', + text: 'Other Dash', + to: '/sources/897/dashboards/2282', + } + describe('updateActiveDashboardLink', () => { const activeDashboard = { ...dashboard, @@ -47,18 +60,6 @@ describe('dashboards.utils.dashboardSwitcherLinks', () => { to: '/sources/897/dashboards/123', } - const link1 = { - key: '9001', - text: 'Low Dash', - to: '/sources/897/dashboards/9001', - } - - const link2 = { - key: '2282', - text: 'Low Dash', - to: '/sources/897/dashboards/2282', - } - const links = [link1, activeLink, link2] it('can set the active link', () => { const loadedLinks = {links, active: null} @@ -79,4 +80,70 @@ describe('dashboards.utils.dashboardSwitcherLinks', () => { expect(actualLinks).toEqual(expectedLinks) }) }) + + describe('updateDashboardLinkName', () => { + const staleDashboard = { + ...dashboard, + id: 3000, + name: 'Stale Dashboard Name', + } + + const staleLink = { + key: '3000', + text: 'Stale Dashboard Name', + to: '/sources/897/dashboards/3000', + } + + const links = [link1, staleLink, link2] + const updatedDashboard = {...staleDashboard, name: 'New Dashboard Name'} + + const dashboardLinks = { + links, + active: link1, + } + + it('can update the name of a provided dashboard', () => { + const actualDashLinks = updateDashboardLinkName( + dashboardLinks, + updatedDashboard + ) + + const expectedDashlinks = { + links: [ + link1, + { + key: '3000', + text: 'New Dashboard Name', + to: '/sources/897/dashboards/3000', + }, + link2, + ], + active: link1, + } + + expect(actualDashLinks).toEqual(expectedDashlinks) + }) + + it('can update name for active link', () => { + const linksWithStaleActive = {...dashboardLinks, active: staleLink} + const actualLinks = updateDashboardLinkName( + linksWithStaleActive, + updatedDashboard + ) + + const renamedLink = { + key: '3000', + text: 'New Dashboard Name', + to: '/sources/897/dashboards/3000', + } + + const expectedDashlinks = { + links: [link1, renamedLink, link2], + active: renamedLink, + } + + expect(actualLinks).toEqual(expectedDashlinks) + expect(actualLinks.active).toBe(actualLinks.links[1]) + }) + }) })