diff --git a/src/utils/__tests__/cardUtilityFunctions.test.jsx b/src/utils/__tests__/cardUtilityFunctions.test.jsx index bcce79b037..6b95215691 100644 --- a/src/utils/__tests__/cardUtilityFunctions.test.jsx +++ b/src/utils/__tests__/cardUtilityFunctions.test.jsx @@ -983,6 +983,50 @@ describe('cardUtilityFunctions', () => { 'unit', ]); }); + it('getCardVariables does not blow up on null or react symbols', () => { + const timeSeriesCardWithVariables = { + id: 'air_flow_mean', + size: 'LARGE', + title: 'Air flow {deviceId} mean vs max', + type: 'TIMESERIES', + content: { + series: [ + { + dataSourceId: 'airflow_max', + label: '{airflow_max}', + }, + ], + xLabel: '{x_axis}', + yLabel: '{y_axis}', + unit: '{unit}', + timeDataSourceId: 'timestamp', + }, + dataSource: { + attributes: [ + { + aggregator: 'max', + attribute: 'air_flow_rate', + id: 'airflow_max', + }, + ], + range: { + type: 'periodToDate', + count: -7, + interval: 'day', + }, + timeGrain: 'day', + }, + someNullProperty: null, + someReactSymbolProperty:

Hi I am a React symbol

, + }; + expect(getCardVariables(timeSeriesCardWithVariables)).toEqual([ + 'deviceId', + 'airflow_max', + 'x_axis', + 'y_axis', + 'unit', + ]); + }); it('getCardVariables returns empty array when no variables are given', () => { const imageCard = { content: { diff --git a/src/utils/cardUtilityFunctions.js b/src/utils/cardUtilityFunctions.js index 702682e2e4..41668948e0 100644 --- a/src/utils/cardUtilityFunctions.js +++ b/src/utils/cardUtilityFunctions.js @@ -254,13 +254,12 @@ export const replaceVariables = (variables, cardVariables, target) => { }; /** - * @param {Object} card + * @param {Object} cardProperty * @returns {Array} an array of unique variable values */ -export const getCardVariables = card => { - // for each - const propertyVariables = Object.values(card).reduce((acc, property) => { - if (typeof property === 'object' && !React.isValidElement(property)) { +export const getCardVariables = cardProperty => { + const propertyVariables = Object.values(cardProperty).reduce((acc, property) => { + if (typeof property === 'object' && !React.isValidElement(property) && !isNil(property)) { // recursively search any objects for additional string properties acc.push(...getCardVariables(property)); } else if (typeof property === 'string') {