From 37cfc685b53630342966c6630fbef63cc563fe50 Mon Sep 17 00:00:00 2001 From: Iris Scholten Date: Tue, 31 Jul 2018 17:50:13 -0700 Subject: [PATCH] Add tests for utils --- .../utils/importDashboardMappings.ts | 6 +- .../utils/importDashboardMappings.test.ts | 200 ++++++++++++++++++ 2 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 ui/test/dashboards/utils/importDashboardMappings.test.ts diff --git a/ui/src/dashboards/utils/importDashboardMappings.ts b/ui/src/dashboards/utils/importDashboardMappings.ts index 97d9a03e9..fc2f7000f 100644 --- a/ui/src/dashboards/utils/importDashboardMappings.ts +++ b/ui/src/dashboards/utils/importDashboardMappings.ts @@ -82,7 +82,7 @@ export const mapCells = ( const sourceLink = getDeep(query, 'source', '') if (!sourceLink) { - return mapQueriesInCells(sourceMappings, c, DYNAMIC_SOURCE) + return mapQueriesInCell(sourceMappings, c, DYNAMIC_SOURCE) } let importedSourceID = _.findKey( @@ -97,7 +97,7 @@ export const mapCells = ( importedSourceID = sourceLinkSID } if (importedSourceID) { - return mapQueriesInCells(sourceMappings, c, importedSourceID) + return mapQueriesInCell(sourceMappings, c, importedSourceID) } return c @@ -106,7 +106,7 @@ export const mapCells = ( return mappedCells } -export const mapQueriesInCells = (sourceMappings, cell, sourceID) => { +export const mapQueriesInCell = (sourceMappings, cell, sourceID) => { const mappedSourceLink = sourceMappings[sourceID].link let queries = getDeep(cell, 'queries', []) if (queries.length) { diff --git a/ui/test/dashboards/utils/importDashboardMappings.test.ts b/ui/test/dashboards/utils/importDashboardMappings.test.ts new file mode 100644 index 000000000..ebffd0f74 --- /dev/null +++ b/ui/test/dashboards/utils/importDashboardMappings.test.ts @@ -0,0 +1,200 @@ +// utils +import { + getSourceInfo, + mapQueriesInCell, + getSourceIDFromLink, + createSourceMappings, +} from 'src/dashboards/utils/importDashboardMappings' + +// fixtures +import {source, cell, query} from 'test/fixtures' + +// constants +import {DYNAMIC_SOURCE, DYNAMIC_SOURCE_INFO} from 'src/dashboards/constants' + +describe('Dashboards.Utils.importDashboardMappings', () => { + describe('createSourceMappings', () => { + describe('if there are no cells', () => { + it('returns object with empty sourcesCells and sourceMappings', () => { + const cells = [] + const importedSources = {} + const expected = {sourcesCells: {}, sourceMappings: {}} + const actual = createSourceMappings(source, cells, importedSources) + + expect(actual).toEqual(expected) + }) + }) + + it('maps cells to their sources from the import', () => { + const sourceLink1 = '/chronograf/v1/sources/1' + const sourceLink2 = '/chronograf/v1/sources/2' + const cellName1 = 'Cell 1' + const cellName2 = 'Cell 2' + const cellName3 = 'Cell 3' + const cellID1 = '1' + const cellID2 = '2' + const cellID3 = '3' + const queryWithSource1 = {...query, source: sourceLink1} + const cellWithSource1 = { + ...cell, + name: cellName1, + queries: [queryWithSource1], + i: cellID1, + } + const queryWithSource2 = {...query, source: sourceLink2} + const cellWithSource2 = { + ...cell, + name: cellName2, + queries: [queryWithSource2], + i: cellID2, + } + const cellNoSource = {...cell, name: cellName3, i: cellID3} + const cells = [cellWithSource1, cellWithSource2, cellNoSource] + const importedSources = { + 1: {name: 'Source 1', link: sourceLink1}, + 2: {name: 'Source 2', link: sourceLink2}, + } + + const expected = { + 1: [{id: cellID1, name: cellName1}], + 2: [{id: cellID2, name: cellName2}], + [DYNAMIC_SOURCE]: [{id: cellID3, name: cellName3}], + } + + const {sourcesCells} = createSourceMappings( + source, + cells, + importedSources + ) + expect(sourcesCells).toEqual(expected) + }) + + it('maps imported sources to the current source by default', () => { + const currentSource = {...source, id: 10, name: 'MY SOURCE'} + const sourceLink1 = '/chronograf/v1/sources/1' + const sourceLink2 = '/chronograf/v1/sources/2' + const cellName1 = 'Cell 1' + const cellName2 = 'Cell 2' + const cellID1 = '1' + const cellID2 = '2' + const queryWithSource1 = {...query, source: sourceLink1} + const cellWithSource1 = { + ...cell, + name: cellName1, + queries: [queryWithSource1], + i: cellID1, + } + const queryWithSource2 = {...query, source: sourceLink2} + const cellWithSource2 = { + ...cell, + name: cellName2, + queries: [queryWithSource2], + i: cellID2, + } + const cells = [cellWithSource1, cellWithSource2] + const importedSources = { + 1: {name: 'Source 1', link: sourceLink1}, + 2: {name: 'Source 2', link: sourceLink2}, + } + const sourceInfo = { + name: currentSource.name, + id: currentSource.id, + link: currentSource.links.self, + } + const expected = { + 1: sourceInfo, + 2: sourceInfo, + } + + const {sourceMappings} = createSourceMappings( + currentSource, + cells, + importedSources + ) + expect(sourceMappings).toEqual(expected) + }) + + it('maps Dynamic source to the dynamic source option by default', () => { + const currentSource = {...source, id: 10, name: 'MY SOURCE'} + const sourceLink1 = '' + const sourceLink2 = '' + const cellName1 = 'Cell 1' + const cellName2 = 'Cell 2' + const cellID1 = '1' + const cellID2 = '2' + const queryWithSource1 = {...query, source: sourceLink1} + const cellWithSource1 = { + ...cell, + name: cellName1, + queries: [queryWithSource1], + i: cellID1, + } + const queryWithSource2 = {...query, source: sourceLink2} + const cellWithSource2 = { + ...cell, + name: cellName2, + queries: [queryWithSource2], + i: cellID2, + } + const cells = [cellWithSource1, cellWithSource2] + const importedSources = {} + + const expected = { + [DYNAMIC_SOURCE]: DYNAMIC_SOURCE_INFO, + } + + const {sourceMappings} = createSourceMappings( + currentSource, + cells, + importedSources + ) + expect(sourceMappings).toEqual(expected) + }) + }) + + describe('mapQueriesInCell', () => { + it('returns cell with source from sourceMappings', () => { + const sourceLink1 = '/chronograf/v1/sources/1' + const sourceID = 1 + const sourceMappings = { + [sourceID]: DYNAMIC_SOURCE_INFO, + } + const queryWithSource1 = {...query, source: sourceLink1} + const cellWithSource1 = { + ...cell, + queries: [queryWithSource1], + } + + // cell has no source + const expected = cell + const actual = mapQueriesInCell(sourceMappings, cellWithSource1, sourceID) + expect(actual).toEqual(expected) + }) + }) + + describe('getSourceIDFromLink', () => { + it('returns the id after sources in a link', () => { + const sourceLink = '/chronograf/v1/sources/10' + const expected = '10' + const actual = getSourceIDFromLink(sourceLink) + expect(actual).toBe(expected) + }) + }) + + describe('getSourceInfo', () => { + it('returns source info from a source', () => { + const name = 'name' + const id = 'id' + const link = '/chronograf/v1/sources/10' + + const links = {...source.links, self: link} + const s = {...source, name, links, id} + + const expected = {name, id, link} + + const actual = getSourceInfo(s) + + expect(actual).toEqual(expected) + }) + }) +})