grafana/public/app/features/variables/getAllVariableValuesForUrl.test.ts
Hugo Häggmark dbec2b02fd
Variables: move state tree into a keyed state (#44642)
* Variables: move state tree into a keyed state

* Update public/app/features/variables/state/transactionReducer.ts

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Chore: fix prettier error

* Chore: renamed slices and lastUid

* Chore: rename toUidAction

* Chore: rename dashboardVariableReducer

* Chore: rename state prop back to templating

* Chore renames variable.dashboardUid

* Chore: rename toDashboardVariableIdentifier

* Chore: rename getDashboardVariable

* Chore: rename getDashboardVariablesState

* Chore: rename getDashboardVariables

* Chore: some more renames

* Chore: small clean up

* Chore: small rename

* Chore: removes unused function

* Chore: rename VariableModel.stateKey

* Chore: rename KeyedVariableIdentifier.stateKey

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
2022-02-18 06:06:04 +01:00

114 lines
3.2 KiB
TypeScript

import { setTemplateSrv } from '@grafana/runtime';
import { variableAdapters } from './adapters';
import { createQueryVariableAdapter } from './query/adapter';
import { getVariablesUrlParams } from './getAllVariableValuesForUrl';
import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv';
const key = 'key';
describe('getAllVariableValuesForUrl', () => {
beforeAll(() => {
variableAdapters.register(createQueryVariableAdapter());
});
describe('with multi value', () => {
beforeEach(() => {
setTemplateSrv(
initTemplateSrv(key, [
{
type: 'query',
name: 'test',
rootStateKey: key,
current: { value: ['val1', 'val2'] },
getValueForUrl: function () {
return this.current.value;
},
},
])
);
});
it('should set multiple url params', () => {
let params: any = getVariablesUrlParams();
expect(params['var-test']).toMatchObject(['val1', 'val2']);
});
});
describe('skip url sync', () => {
beforeEach(() => {
setTemplateSrv(
initTemplateSrv(key, [
{
name: 'test',
rootStateKey: key,
skipUrlSync: true,
current: { value: 'value' },
getValueForUrl: function () {
return this.current.value;
},
},
])
);
});
it('should not include template variable value in url', () => {
const params = getVariablesUrlParams();
expect(params['var-test']).toBe(undefined);
});
});
describe('with multi value with skip url sync', () => {
beforeEach(() => {
setTemplateSrv(
initTemplateSrv(key, [
{
type: 'query',
name: 'test',
rootStateKey: key,
skipUrlSync: true,
current: { value: ['val1', 'val2'] },
getValueForUrl: function () {
return this.current.value;
},
},
])
);
});
it('should not include template variable value in url', () => {
const params = getVariablesUrlParams();
expect(params['var-test']).toBe(undefined);
});
});
describe('fillVariableValuesForUrl with multi value and scopedVars', () => {
beforeEach(() => {
setTemplateSrv(
initTemplateSrv(key, [{ type: 'query', name: 'test', rootStateKey: key, current: { value: ['val1', 'val2'] } }])
);
});
it('should set scoped value as url params', () => {
const params = getVariablesUrlParams({
test: { value: 'val1', text: 'val1text' },
});
expect(params['var-test']).toBe('val1');
});
});
describe('fillVariableValuesForUrl with multi value, scopedVars and skip url sync', () => {
beforeEach(() => {
setTemplateSrv(
initTemplateSrv(key, [{ type: 'query', name: 'test', rootStateKey: key, current: { value: ['val1', 'val2'] } }])
);
});
it('should not set scoped value as url params', () => {
const params = getVariablesUrlParams({
test: { name: 'test', value: 'val1', text: 'val1text', skipUrlSync: true },
});
expect(params['var-test']).toBe(undefined);
});
});
});