1import { 2 hasCustomVariableSupport, 3 hasDatasourceVariableSupport, 4 hasLegacyVariableSupport, 5 hasStandardVariableSupport, 6 isLegacyQueryEditor, 7 isQueryEditor, 8} from './guard'; 9import { LegacyVariableQueryEditor } from './editor/LegacyVariableQueryEditor'; 10import { StandardVariableQueryEditor } from './editor/getVariableQueryEditor'; 11import { VariableSupportType } from '@grafana/data'; 12 13describe('type guards', () => { 14 describe('hasLegacyVariableSupport', () => { 15 describe('when called with a legacy data source', () => { 16 it('should return true', () => { 17 const datasource: any = { metricFindQuery: () => undefined }; 18 expect(hasLegacyVariableSupport(datasource)).toBe(true); 19 }); 20 }); 21 22 describe('when called with data source without metricFindQuery function', () => { 23 it('should return false', () => { 24 const datasource: any = {}; 25 expect(hasLegacyVariableSupport(datasource)).toBe(false); 26 }); 27 }); 28 29 describe('when called with a legacy data source with variable support', () => { 30 it('should return false', () => { 31 const datasource: any = { metricFindQuery: () => undefined, variables: {} }; 32 expect(hasLegacyVariableSupport(datasource)).toBe(false); 33 }); 34 }); 35 }); 36 37 describe('hasStandardVariableSupport', () => { 38 describe('when called with a data source with standard variable support', () => { 39 it('should return true', () => { 40 const datasource: any = { 41 metricFindQuery: () => undefined, 42 variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined }, 43 }; 44 expect(hasStandardVariableSupport(datasource)).toBe(true); 45 }); 46 47 describe('and with a custom query', () => { 48 it('should return true', () => { 49 const datasource: any = { 50 metricFindQuery: () => undefined, 51 variables: { 52 getType: () => VariableSupportType.Standard, 53 toDataQuery: () => undefined, 54 query: () => undefined, 55 }, 56 }; 57 expect(hasStandardVariableSupport(datasource)).toBe(true); 58 }); 59 }); 60 }); 61 62 describe('when called with a data source with partial standard variable support', () => { 63 it('should return false', () => { 64 const datasource: any = { 65 metricFindQuery: () => undefined, 66 variables: { getType: () => VariableSupportType.Standard, query: () => undefined }, 67 }; 68 expect(hasStandardVariableSupport(datasource)).toBe(false); 69 }); 70 }); 71 72 describe('when called with a data source without standard variable support', () => { 73 it('should return false', () => { 74 const datasource: any = { metricFindQuery: () => undefined }; 75 expect(hasStandardVariableSupport(datasource)).toBe(false); 76 }); 77 }); 78 }); 79 80 describe('hasCustomVariableSupport', () => { 81 describe('when called with a data source with custom variable support', () => { 82 it('should return true', () => { 83 const datasource: any = { 84 metricFindQuery: () => undefined, 85 variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} }, 86 }; 87 expect(hasCustomVariableSupport(datasource)).toBe(true); 88 }); 89 }); 90 91 describe('when called with a data source with custom variable support but without editor', () => { 92 it('should return false', () => { 93 const datasource: any = { 94 metricFindQuery: () => undefined, 95 variables: { getType: () => VariableSupportType.Custom, query: () => undefined }, 96 }; 97 expect(hasCustomVariableSupport(datasource)).toBe(false); 98 }); 99 }); 100 101 describe('when called with a data source with custom variable support but without query', () => { 102 it('should return false', () => { 103 const datasource: any = { 104 metricFindQuery: () => undefined, 105 variables: { getType: () => VariableSupportType.Custom, editor: {} }, 106 }; 107 expect(hasCustomVariableSupport(datasource)).toBe(false); 108 }); 109 }); 110 111 describe('when called with a data source without custom variable support', () => { 112 it('should return false', () => { 113 const datasource: any = { metricFindQuery: () => undefined }; 114 expect(hasCustomVariableSupport(datasource)).toBe(false); 115 }); 116 }); 117 }); 118 119 describe('hasDatasourceVariableSupport', () => { 120 describe('when called with a data source with datasource variable support', () => { 121 it('should return true', () => { 122 const datasource: any = { 123 metricFindQuery: () => undefined, 124 variables: { getType: () => VariableSupportType.Datasource }, 125 }; 126 expect(hasDatasourceVariableSupport(datasource)).toBe(true); 127 }); 128 }); 129 130 describe('when called with a data source without datasource variable support', () => { 131 it('should return false', () => { 132 const datasource: any = { metricFindQuery: () => undefined }; 133 expect(hasDatasourceVariableSupport(datasource)).toBe(false); 134 }); 135 }); 136 }); 137}); 138 139describe('isLegacyQueryEditor', () => { 140 describe('happy cases', () => { 141 describe('when called with a legacy query editor but without a legacy data source', () => { 142 it('then is should return true', () => { 143 const component: any = LegacyVariableQueryEditor; 144 const datasource: any = {}; 145 146 expect(isLegacyQueryEditor(component, datasource)).toBe(true); 147 }); 148 }); 149 150 describe('when called with a legacy data source but without a legacy query editor', () => { 151 it('then is should return true', () => { 152 const component: any = StandardVariableQueryEditor; 153 const datasource: any = { metricFindQuery: () => undefined }; 154 155 expect(isLegacyQueryEditor(component, datasource)).toBe(true); 156 }); 157 }); 158 }); 159 160 describe('negative cases', () => { 161 describe('when called without component', () => { 162 it('then is should return false', () => { 163 const component: any = null; 164 const datasource: any = { metricFindQuery: () => undefined }; 165 166 expect(isLegacyQueryEditor(component, datasource)).toBe(false); 167 }); 168 }); 169 170 describe('when called without a legacy query editor and without a legacy data source', () => { 171 it('then is should return false', () => { 172 const component: any = StandardVariableQueryEditor; 173 const datasource: any = {}; 174 175 expect(isLegacyQueryEditor(component, datasource)).toBe(false); 176 }); 177 }); 178 }); 179}); 180 181describe('isQueryEditor', () => { 182 describe('happy cases', () => { 183 describe('when called without a legacy editor and with a data source with standard variable support', () => { 184 it('then is should return true', () => { 185 const component: any = StandardVariableQueryEditor; 186 const datasource: any = { 187 variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined }, 188 }; 189 190 expect(isQueryEditor(component, datasource)).toBe(true); 191 }); 192 }); 193 194 describe('when called without a legacy editor and with a data source with custom variable support', () => { 195 it('then is should return true', () => { 196 const component: any = StandardVariableQueryEditor; 197 const datasource: any = { 198 variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} }, 199 }; 200 201 expect(isQueryEditor(component, datasource)).toBe(true); 202 }); 203 }); 204 205 describe('when called without a legacy editor and with a data source with datasource variable support', () => { 206 it('then is should return true', () => { 207 const component: any = StandardVariableQueryEditor; 208 const datasource: any = { variables: { getType: () => VariableSupportType.Datasource } }; 209 210 expect(isQueryEditor(component, datasource)).toBe(true); 211 }); 212 }); 213 }); 214 215 describe('negative cases', () => { 216 describe('when called without component', () => { 217 it('then is should return false', () => { 218 const component: any = null; 219 const datasource: any = { metricFindQuery: () => undefined }; 220 221 expect(isQueryEditor(component, datasource)).toBe(false); 222 }); 223 }); 224 225 describe('when called with a legacy query editor', () => { 226 it('then is should return false', () => { 227 const component: any = LegacyVariableQueryEditor; 228 const datasource: any = { variables: { getType: () => VariableSupportType.Datasource } }; 229 230 expect(isQueryEditor(component, datasource)).toBe(false); 231 }); 232 }); 233 234 describe('when called without a legacy query editor but with a legacy data source', () => { 235 it('then is should return false', () => { 236 const component: any = StandardVariableQueryEditor; 237 const datasource: any = { metricFindQuery: () => undefined }; 238 239 expect(isQueryEditor(component, datasource)).toBe(false); 240 }); 241 }); 242 }); 243}); 244