From 9c85793dbbdc9bb63fc7afbd1e912736d9f2bd48 Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Fri, 9 Nov 2018 10:52:55 +0100 Subject: [PATCH] stackdriver: test saved variable --- .../TemplateQueryComponent.test.tsx | 30 ++++++++++++++----- .../components/TemplateQueryComponent.tsx | 3 ++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.test.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.test.tsx index 0d9f0e6d5b4..e81037e2822 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.test.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.test.tsx @@ -2,14 +2,15 @@ import React from 'react'; import renderer from 'react-test-renderer'; import { StackdriverTemplateQueryComponent } from './TemplateQueryComponent'; import { TemplateQueryProps } from 'app/types/plugins'; +import { MetricFindQueryTypes } from '../types'; jest.mock('../functions', () => ({ - getMetricTypes: async () => ({ metricTypes: [], selectedMetricType: '' }), + getMetricTypes: () => ({ metricTypes: [], selectedMetricType: '' }), })); const props: TemplateQueryProps = { onChange: (query, definition) => {}, - query: '', + query: {}, datasource: { getMetricTypes: async p => [], }, @@ -21,11 +22,24 @@ describe('StackdriverTemplateQueryComponent', () => { expect(tree).toMatchSnapshot(); }); - it('should use the first query type in the array if no query type was saved before', done => { - props.onChange = (query, definition) => { - expect(definition).toBe('Stackdriver - Metric Types'); - done(); - }; - renderer.create().toJSON(); + describe('and a new variable is created', () => { + it('should trigger a query using the first query type in the array', done => { + props.onChange = (query, definition) => { + expect(definition).toBe('Stackdriver - Metric Types'); + done(); + }; + renderer.create().toJSON(); + }); + }); + + describe('and an existing variable is edited', () => { + it('should trigger new query using the saved query type', done => { + props.query = { selectedQueryType: MetricFindQueryTypes.MetricLabels }; + props.onChange = (query, definition) => { + expect(definition).toBe('Stackdriver - Metric Labels'); + done(); + }; + renderer.create().toJSON(); + }); }); }); diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index e9784d485dd..4769e3481b7 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -33,7 +33,9 @@ export class StackdriverTemplateQueryComponent extends PureComponent