grafana/public/app/core/utils/explore.test.ts

189 lines
4.9 KiB
TypeScript
Raw Normal View History

2018-11-22 04:02:53 -06:00
import {
DEFAULT_RANGE,
serializeStateToUrlParam,
parseUrlState,
updateHistory,
clearHistory,
hasNonEmptyQuery,
} from './explore';
2019-01-12 16:22:28 -06:00
import { ExploreUrlState } from 'app/types/explore';
2018-11-22 04:02:53 -06:00
import store from 'app/core/store';
2018-09-28 10:39:53 -05:00
2019-01-12 16:22:28 -06:00
const DEFAULT_EXPLORE_STATE: ExploreUrlState = {
2018-09-28 10:39:53 -05:00
datasource: null,
2019-01-12 16:22:28 -06:00
queries: [],
2018-09-28 10:39:53 -05:00
range: DEFAULT_RANGE,
2019-01-31 12:38:49 -06:00
ui: {
showingGraph: true,
showingTable: true,
showingLogs: true,
}
2018-09-28 10:39:53 -05:00
};
2018-10-01 05:56:26 -05:00
describe('state functions', () => {
2018-09-28 10:39:53 -05:00
describe('parseUrlState', () => {
it('returns default state on empty string', () => {
expect(parseUrlState('')).toMatchObject({
datasource: null,
2018-11-21 09:28:30 -06:00
queries: [],
2018-09-28 10:39:53 -05:00
range: DEFAULT_RANGE,
});
});
it('returns a valid Explore state from URL parameter', () => {
const paramValue =
2018-11-21 09:28:30 -06:00
'%7B"datasource":"Local","queries":%5B%7B"expr":"metric"%7D%5D,"range":%7B"from":"now-1h","to":"now"%7D%7D';
expect(parseUrlState(paramValue)).toMatchObject({
datasource: 'Local',
2018-11-21 09:28:30 -06:00
queries: [{ expr: 'metric' }],
range: {
from: 'now-1h',
to: 'now',
},
});
});
it('returns a valid Explore state from a compact URL parameter', () => {
const paramValue = '%5B"now-1h","now","Local",%7B"expr":"metric"%7D%5D';
expect(parseUrlState(paramValue)).toMatchObject({
datasource: 'Local',
2018-11-21 09:28:30 -06:00
queries: [{ expr: 'metric' }],
range: {
from: 'now-1h',
to: 'now',
},
});
});
2018-09-28 10:39:53 -05:00
});
2018-09-28 10:39:53 -05:00
describe('serializeStateToUrlParam', () => {
it('returns url parameter value for a state object', () => {
const state = {
...DEFAULT_EXPLORE_STATE,
2019-01-12 16:22:28 -06:00
datasource: 'foo',
queries: [
2018-09-28 10:39:53 -05:00
{
expr: 'metric{test="a/b"}',
2018-09-28 10:39:53 -05:00
},
{
expr: 'super{foo="x/z"}',
2018-09-28 10:39:53 -05:00
},
],
2019-01-12 16:22:28 -06:00
range: {
from: 'now-5h',
to: 'now',
},
2018-09-28 10:39:53 -05:00
};
2019-01-31 12:38:49 -06:00
2018-09-28 10:39:53 -05:00
expect(serializeStateToUrlParam(state)).toBe(
2018-11-21 09:28:30 -06:00
'{"datasource":"foo","queries":[{"expr":"metric{test=\\"a/b\\"}"},' +
2019-01-31 12:38:49 -06:00
'{"expr":"super{foo=\\"x/z\\"}"}],"range":{"from":"now-5h","to":"now"},' +
'"ui":{"showingGraph":true,"showingTable":true,"showingLogs":true}}'
);
});
it('returns url parameter value for a state object', () => {
const state = {
...DEFAULT_EXPLORE_STATE,
2019-01-12 16:22:28 -06:00
datasource: 'foo',
queries: [
{
expr: 'metric{test="a/b"}',
},
{
expr: 'super{foo="x/z"}',
},
],
2019-01-12 16:22:28 -06:00
range: {
from: 'now-5h',
to: 'now',
},
};
expect(serializeStateToUrlParam(state, true)).toBe(
'["now-5h","now","foo",{"expr":"metric{test=\\"a/b\\"}"},{"expr":"super{foo=\\"x/z\\"}"},{"ui":[true,true,true]}]'
2018-09-28 10:39:53 -05:00
);
});
});
2018-09-28 10:39:53 -05:00
describe('interplay', () => {
it('can parse the serialized state into the original state', () => {
const state = {
...DEFAULT_EXPLORE_STATE,
2019-01-12 16:22:28 -06:00
datasource: 'foo',
queries: [
2018-09-28 10:39:53 -05:00
{
expr: 'metric{test="a/b"}',
2018-09-28 10:39:53 -05:00
},
{
expr: 'super{foo="x/z"}',
2018-09-28 10:39:53 -05:00
},
],
2019-01-12 16:22:28 -06:00
range: {
from: 'now - 5h',
to: 'now',
},
2018-09-28 10:39:53 -05:00
};
const serialized = serializeStateToUrlParam(state);
const parsed = parseUrlState(serialized);
expect(state).toMatchObject(parsed);
});
2018-09-28 10:39:53 -05:00
it('can parse the compact serialized state into the original state', () => {
const state = {
...DEFAULT_EXPLORE_STATE,
datasource: 'foo',
queries: [
{
expr: 'metric{test="a/b"}',
},
{
expr: 'super{foo="x/z"}',
},
],
range: {
from: 'now - 5h',
to: 'now',
},
};
const serialized = serializeStateToUrlParam(state, true);
const parsed = parseUrlState(serialized);
2019-01-12 16:22:28 -06:00
expect(state).toMatchObject(parsed);
2018-09-28 10:39:53 -05:00
});
});
});
2018-11-22 04:02:53 -06:00
describe('updateHistory()', () => {
const datasourceId = 'myDatasource';
const key = `grafana.explore.history.${datasourceId}`;
beforeEach(() => {
clearHistory(datasourceId);
expect(store.exists(key)).toBeFalsy();
});
test('should save history item to localStorage', () => {
const expected = [
{
query: { refId: '1', expr: 'metric' },
},
];
expect(updateHistory([], datasourceId, [{ refId: '1', expr: 'metric' }])).toMatchObject(expected);
expect(store.exists(key)).toBeTruthy();
expect(store.getObject(key)).toMatchObject(expected);
});
});
describe('hasNonEmptyQuery', () => {
test('should return true if one query is non-empty', () => {
expect(hasNonEmptyQuery([{ refId: '1', key: '2', expr: 'foo' }])).toBeTruthy();
});
test('should return false if query is empty', () => {
expect(hasNonEmptyQuery([{ refId: '1', key: '2' }])).toBeFalsy();
});
test('should return false if no queries exist', () => {
expect(hasNonEmptyQuery([])).toBeFalsy();
});
});