From 5789f41b7cb0ef8288665fc171af6e5e5cdde0bc Mon Sep 17 00:00:00 2001 From: Iris Scholten Date: Fri, 23 Mar 2018 18:39:45 -0700 Subject: [PATCH] update getters in TableOptions.test --- ui/src/dashboards/components/TableOptions.tsx | 4 +- .../components/TableOptions.test.tsx | 98 ++++++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/ui/src/dashboards/components/TableOptions.tsx b/ui/src/dashboards/components/TableOptions.tsx index 39c1dd6567..f6433e9862 100644 --- a/ui/src/dashboards/components/TableOptions.tsx +++ b/ui/src/dashboards/components/TableOptions.tsx @@ -53,7 +53,7 @@ export class TableOptions extends PureComponent { return fieldNames || [] } - get timeColumn() { + get timeField() { return ( this.fieldNames.find(f => f.internalName === 'time') || TIME_FIELD_DEFAULT ) @@ -63,7 +63,7 @@ export class TableOptions extends PureComponent { const {dataLabels} = this.props return _.isEmpty(dataLabels) - ? [this.timeColumn] + ? [this.timeField] : dataLabels.map(label => { const existing = this.fieldNames.find(f => f.internalName === label) return ( diff --git a/ui/test/dashboards/components/TableOptions.test.tsx b/ui/test/dashboards/components/TableOptions.test.tsx index 6dda3556e6..96c7985748 100644 --- a/ui/test/dashboards/components/TableOptions.test.tsx +++ b/ui/test/dashboards/components/TableOptions.test.tsx @@ -9,6 +9,7 @@ import {TableOptions} from 'src/dashboards/components/TableOptions' import FancyScrollbar from 'src/shared/components/FancyScrollbar' import ThresholdsList from 'src/shared/components/ThresholdsList' import ThresholdsListTypeToggle from 'src/shared/components/ThresholdsListTypeToggle' +import {TIME_FIELD_DEFAULT} from 'src/shared/constants/tableGraph' const defaultProps = { dataLabels: [], @@ -32,12 +33,105 @@ const setup = (override = {}) => { const wrapper = shallow() - return {wrapper, props} + const instance = wrapper.instance() as TableOptions + + return {wrapper, instance, props} } +const dataLabels = ['time', 'foo', 'bar'] describe('Dashboards.Components.TableOptions', () => { describe('getters', () => { - describe('computedColumnNames', () => {}) + describe('fieldNames', () => { + describe('if fieldNames are passed in tableOptions as props', () => { + it('returns fieldNames', () => { + const fieldNames = [ + {internalName: 'time', displayName: 'TIME', visible: true}, + {internalName: 'foo', displayName: 'BAR', visible: false}, + ] + const {instance} = setup({tableOptions: {fieldNames}}) + + expect(instance.fieldNames).toBe(fieldNames) + }) + }) + + describe('if fieldNames are not passed in tableOptions as props', () => { + it('returns empty array', () => { + const {instance} = setup() + + expect(instance.fieldNames).toEqual([]) + }) + }) + }) + + describe('timeField', () => { + describe('if time field in fieldNames', () => { + it('returns time field', () => { + const timeField = { + internalName: 'time', + displayName: 'TIME', + visible: true, + } + const fieldNames = [ + timeField, + {internalName: 'foo', displayName: 'BAR', visible: false}, + ] + const {instance} = setup({tableOptions: {fieldNames}}) + + expect(instance.timeField).toBe(timeField) + }) + }) + + describe('if time field not in fieldNames', () => { + it('returns default time field', () => { + const fieldNames = [ + {internalName: 'foo', displayName: 'BAR', visible: false}, + ] + const {instance} = setup({tableOptions: {fieldNames}}) + + expect(instance.timeField).toBe(TIME_FIELD_DEFAULT) + }) + }) + }) + + describe('computedFieldNames', () => { + describe('if dataLabels are not passed in', () => { + it('returns an array of the time column', () => { + const {instance} = setup() + + expect(instance.computedFieldNames).toEqual([TIME_FIELD_DEFAULT]) + }) + }) + + describe('if dataLabels are passed in', () => { + describe('if dataLabel has a matching fieldName', () => { + it('returns array with the matching fieldName', () => { + const fieldNames = [ + {internalName: 'foo', displayName: 'bar', visible: true}, + ] + const dataLabels = ['foo'] + const {instance} = setup({tableOptions: {fieldNames}, dataLabels}) + + expect(instance.computedFieldNames).toEqual(fieldNames) + }) + }) + + describe('if dataLabel does not have a matching fieldName', () => { + it('returns array with a new fieldName created for it', () => { + const fieldNames = [ + {internalName: 'time', displayName: 'bar', visible: true}, + ] + const unmatchedLabel = 'foo' + const dataLabels = ['time', unmatchedLabel] + const {instance} = setup({tableOptions: {fieldNames}, dataLabels}) + + expect(instance.computedFieldNames).toEqual([ + ...fieldNames, + {internalName: unmatchedLabel, displayName: '', visible: true}, + ]) + }) + }) + }) + }) }) describe('rendering', () => {